library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub beet-aizu/library

:heavy_check_mark: test/aoj/3086.test.cpp

Depends on

Code

// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3086

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#include "../../io/single.cpp"
#include "../../algorithm/offlineonline.cpp"
#include "../../datastructure/sparsetable.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,l;
  cin>>n>>l;
  auto as=read(n);

  auto f=[](int a,int b){return max(a,b);};
  SparseTable<int> st(f);
  st.build(as);

  using ll = long long;
  const ll INF = 1e18;
  auto dist=[&](int i,int j)->ll{
    return j-i<l?INF-ll(2e9)*(j-i):-st.query(i,j);
  };

  cout<<-OfflineOnline::solve<ll>(n,dist)<<endl;
  return 0;
}
#line 1 "test/aoj/3086.test.cpp"
// verification-helper: PROBLEM http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=3086

#include<bits/stdc++.h>
using namespace std;

#define call_from_test
#line 1 "io/single.cpp"

#line 3 "io/single.cpp"
using namespace std;
#endif

//BEGIN CUT HERE
template<typename T=int>
vector<T> read(size_t n){
  vector<T> ts(n);
  for(size_t i=0;i<n;i++) cin>>ts[i];
  return ts;
}
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 1 "algorithm/offlineonline.cpp"

#line 3 "algorithm/offlineonline.cpp"
using namespace std;
#endif

// https://qiita.com/tmaehara/items/0687af2cfb807cde7860
//BEGIN CUT HERE
namespace OfflineOnline{
  vector<int> used;

  template<typename T>
  void update(vector<T> &dp,int k,T val){
    if(!used[k]) dp[k]=val;
    dp[k]=min(dp[k],val);
    used[k]=1;
  }

  // [l, r), [a, b]
  template<typename T,typename F>
  void induce(int l,int r,int a,int b,vector<T> &dp,F dist){
    if(l==r) return;
    int m=(l+r)>>1;
    assert(m<a);
    int idx=a;
    T res=dist(m,idx)+dp[idx];
    for(int i=a;i<=b;i++){
      T tmp=dist(m,i)+dp[i];
      if(tmp<res) res=tmp,idx=i;
    }
    update(dp,m,res);
    induce(l,m+0,a,idx,dp,dist);
    induce(m+1,r,idx,b,dp,dist);
  }

  template<typename T,typename F>
  void solve(int l,int r,vector<T> &dp,F dist){
    if(l+1==r) return update(dp,l,dist(l,r)+dp[r]);
    int m=(l+r)>>1;
    solve(m,r,dp,dist);
    induce(l,m,m,r,dp,dist);
    solve(l,m,dp,dist);
  }

  // dp[i] = min_{i<j} dist(i,j) + dp[j]
  template<typename T,typename F>
  T solve(int n,F dist){
    vector<T> dp(n+1,0);
    used.assign(n+1,0);
    used[n]=1;
    solve(0,n,dp,dist);
    return dp[0];
  }
};
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed main(){
  return 0;
}
#endif
#line 1 "datastructure/sparsetable.cpp"

#line 3 "datastructure/sparsetable.cpp"
using namespace std;
#endif
//BEGIN CUT HERE
template<typename T>
struct SparseTable{
  using F = function<T(T, T)>;
  vector< vector<T> > dat;
  vector<int> ht;
  const F f;

  SparseTable(F f):f(f){}

  void build(const vector<T> &v){
    int n=v.size(),h=1;
    while((1<<h)<=n) h++;
    dat.assign(h,vector<T>(n));
    ht.assign(n+1,0);
    for(int j=2;j<=n;j++) ht[j]=ht[j>>1]+1;

    for(int j=0;j<n;j++) dat[0][j]=v[j];
    for(int i=1,p=1;i<h;i++,p<<=1)
      for(int j=0;j<n;j++)
        dat[i][j]=f(dat[i-1][j],dat[i-1][min(j+p,n-1)]);
  };

  T query(int a,int b){
    int l=b-a;
    return f(dat[ht[l]][a],dat[ht[l]][b-(1<<ht[l])]);
  }
};
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif
#line 10 "test/aoj/3086.test.cpp"
#undef call_from_test

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,l;
  cin>>n>>l;
  auto as=read(n);

  auto f=[](int a,int b){return max(a,b);};
  SparseTable<int> st(f);
  st.build(as);

  using ll = long long;
  const ll INF = 1e18;
  auto dist=[&](int i,int j)->ll{
    return j-i<l?INF-ll(2e9)*(j-i):-st.query(i,j);
  };

  cout<<-OfflineOnline::solve<ll>(n,dist)<<endl;
  return 0;
}
Back to top page