제출 #1202267

#제출 시각아이디문제언어결과실행 시간메모리
1202267t9unkubj3단 점프 (JOI19_jumps)C++20
100 / 100
3716 ms204908 KiB
#ifdef t9unkubj
#include"my_template.h"
//#include"my_template_no_debug.h"
#else
#define dbg(...) 199958
#pragma GCC optimize("O3")
#include"bits/stdc++.h"
using namespace std;
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using i128=__int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vc<vvc<T>>;
template<class T>using vvvvc=vc<vvvc<T>>;
template<class T>using smpq=priority_queue<T,vc<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
using vi=vc<int>;
using vvi=vvc<int>;
using vl=vc<ll>;
using vvl=vvc<ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=(ll(n)-1);i>=0;i--)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define pb push_back
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template<class T>
T sum(const vector<T>&v){
    return accumulate(all(v),T(0));
}
template<class T>
T min(const vector<T>&v){
    return *min_element(all(v));
}
template<class T>
T max(const vector<T>&v){
    return *max_element(all(v));
}
void YesNo(bool y){
    cout<<(y?"Yes":"No")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
template<class T>
void unique(vc<T>&a){
    a.erase(unique(all(a)),a.end());
}
vvi readgraph(int n,int m,int off = -1){
    vvi g(n);
    rep(i, m){
        int u,v;
        cin>>u>>v;
        u+=off,v+=off;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    return g;
}
vvi readtree(int n,int off=-1){
    return readgraph(n,n-1,off);
}
template<class T>
vc<T> presum(vc<T> &a){
    vc<T> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
ostream&operator<<(ostream&os,i128 num) {
    if(num==0){
        os<<"0";
        return os;
    }
    if(num<0){
        os<<"-";
        num=-num;
    }
    string res;
    while(num){
        res+='0'+static_cast<int>(num%10);
        num/=10;
    }
    reverse(all(res));
    os<<res;
    return os;
}

istream&operator>>(istream& is,i128&num) {
    string s;
    is>>s;
    bool neg=0;
    num=0;
    if(s[0]=='-'){
        neg=1;
        s=s.substr(1);
    }
    for(auto&c:s){
        if (c>='0'&&c<='9'){
            num=num*10+(c-'0');
        }else{
            is.setstate(ios::failbit);
            return is;
        }
    }
    if(neg){
        num=-num;
    }
    return is;
}

void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a; }
void scan(i128&a){ cin>> a; }
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define CHR(...) char __VA_ARGS__; read(__VA_ARGS__);
#define DBL(...) double __VA_ARGS__; read(__VA_ARGS__);
#define LD(...) ld __VA_ARGS__; read(__VA_ARGS__);
#define I128(...) i128 __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);

void print(int a) { cout << a; }
void print(ll a) { cout << a; }
void print(string a) { cout << a; }
void print(char a) { cout << a; }
void print(uint a) { cout << a; }
void print(bool a) { cout << a; }
void print(ull a) { cout << a; }
void print(double a) { cout << a; }
void print(ld a){ cout<< a; }
void print(i128 a){ cout<< a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
struct ioset{
    ioset(){
        cin.tie(0)->sync_with_stdio(0);
        #ifdef t9unkubj
        cout<<fixed<<setprecision(6);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
}ioset_______;
struct MY_TIMER{
    double start_time;
    MY_TIMER(){
        start_time=clock();
    }
    double out(){
        return (clock()-start_time)/double(CLOCKS_PER_SEC);
    }
}TIMER;
#endif

//https://trap.jp/post/1489
//T:=座標型
//S:=点のvalue
//F:=作用素
//Apply:=作用
//Merge:=作用素をマージ
//init:=(pair<T,T>,idx)で初期化
//op:=集約
//e:=単位元

//id:=単位元
//Find を使うなら idx が S 内で定義されている必要がある
//template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
struct kdtree{
    //葉ではvalueはそのものの値,それ以外では集約した値 
    //いい感じの演算じゃなければ葉以外の値は壊れてる
    struct Node{
        T xmin,xmax,ymin,ymax;
        S value;
        F lazy;
        int idx;
        int size;
        Node*l;
        Node*r;
        Node(vector<array<T,3>>&v,int L,int R,bool div){ 
            xmin=ymin=numeric_limits<T>::max();
            xmax=ymax=numeric_limits<T>::min();
            if constexpr(id){
                lazy=id();
            }else{
                lazy=F();
            }
            for(int i=L;i<R;i++){
                chmin(xmin,v[i][0]);
                chmax(xmax,v[i][0]);
                chmin(ymin,v[i][1]);
                chmax(ymax,v[i][1]);
            }
            size=R-L;
            if(size==0)return;
            if(size==1){
                if constexpr(init){
                    value=init(v[L][2]);
                }else{
                    value=S();
                }
                idx=v[L][2];
                l=r=nullptr;
                return;
            }else{
                idx=-1;
                int mid=size/2;
                nth_element(v.begin()+L,v.begin()+L+mid,v.begin()+R,[&](auto a,auto b){return a[div]<b[div];});
                l=new Node(v,L,L+mid,div^1);
                r=new Node(v,L+mid,R,div^1);
                this->value=op(l->value,r->value,e());
            }
        }
        void update(){
            if(l){
                if constexpr(op){
                    this->value=op(l->value,r->value,this->value);
                }
            }
        }
        void push(){
            if(l){
                if constexpr(Merge){
                    l->lazy=Merge(l->lazy,this->lazy);
                    r->lazy=Merge(r->lazy,this->lazy);
                }
                if constexpr(Apply){
                    l->value=Apply(l->value,this->lazy);
                    r->value=Apply(r->value,this->lazy);
                }
            }
            if constexpr(id){
                this->lazy=id();
            }else{
                this->lazy=F();
            }
        }
        S prod(T x1,T x2,T y1,T y2){
            if(x2<xmin||xmax<x1||y2<ymin||ymax<y1){
                if constexpr(e){
                    return e();
                }else{
                    return S();
                }
            }
            
            push();
            if(x1<=xmin&&xmax<=x2&&y1<=ymin&&ymax<=y2){
                return value;
            }
            if(l)return op(l->prod(x1,x2,y1,y2),r->prod(x1,x2,y1,y2),e());
            if constexpr(e){
                return e();
            }else{
                return S();
            }
        }
        void apply(T x1,T x2,T y1,T y2,F x){
            if(x2<xmin||xmax<x1||y2<ymin||ymax<y1){
                return;
            }
            push();
            if(x1<=xmin&&xmax<=x2&&y1<=ymin&&ymax<=y2){ 
                value=Apply(value,x);
                lazy=Merge(lazy,x);
                return;
            }
            l->apply(x1,x2,y1,y2,x);
            r->apply(x1,x2,y1,y2,x);
            update();
        }
        //点がuniqueであると仮定して (X,Y) の value を返す
        optional<S> get(T X,T Y){
            if(X<xmin||xmax<X||Y<ymin||ymax<Y)return nullopt;
            if(X==xmin&&xmax==X&&Y==ymin&&Y==ymax){
                return value;
            }
            push();
            if(l){
                auto res=l->get(X,Y);
                if(res.has_value())return res;
            }
            if(r){
                auto res=r->get(X,Y);
                if(res.has_value())return res;
            }
            return nullopt;
        }
        static bool inside(T dx,T dy,T K){
            return ll(dx)*dx+ll(dy)*dy<=ll(K)*K;
        }
        template<class Show> 
        bool find(T x,T y,T K,Show f,bool once){
            if(!inside(clamp(x,xmin,xmax)-x,clamp(y,ymin,ymax)-y,K))return 0;
            if(idx!=-1){
                f(idx);
                return 1;
            }
            if(l->find(x,y,K,f,once)&&once)return 1;
            return r->find(x,y,K,f,once);
        }
    };
    Node*root;
    kdtree(vector<pair<T,T>>&xy){
        vector<array<T,3>>X(xy.size());
        rep(i,xy.size())X[i]={xy[i].first,xy[i].second,(T)i};
        root=new Node(X,0,X.size(),0);
    }
    void apply(T x1,T x2,T y1,T y2,F x){
        static_assert(Merge);
        static_assert(Apply);
        root->apply(x1,x2,y1,y2,x);
    }
    S prod(T x1,T x2,T y1,T y2){    
        static_assert(op);
        return root->prod(x1,x2,y1,y2);
    }
    S get(T X,T Y){
        auto ret=root->get(X,Y);
        assert(ret.has_value());
        return ret.value();
    }
    template<class Show>
    void find(T x,T y,T K,Show f,bool once){
        root->find(x,y,K,f,once);
    }
};
//template<class T,class S,class F,auto op,auto e,auto Apply,auto Merge,auto id,auto init>
using NP=nullptr_t;
struct S{
    ll max_val;
    ll max_a;
    ll max_off;
};
S e(){
    return {(ll)-2e18,(ll)-2e18,(ll)-2e18};
}
S op(S a,S b,S c){
    return {max(a.max_val,b.max_val),max(a.max_a,b.max_a),c.max_off};
}
S Apply(S a,ll b){
    chmax(a.max_off,b);
    dbg(a.max_off);

    chmax(a.max_val,a.max_off+a.max_a);
    return a;
}
ll Merge(ll a,ll b){
    return max(a,b);
}
ll id(){
    return -2e18;
}
vc<int>a;
vc<pair<int,int>>ps;
S init(int i){
    return S{(ll)-2e18,a[ps[i].first]+a[(ps[i].second-ps[i].first)/2+ps[i].first],(ll)-2e18};
}
void solve(){
    INT(n);
    a=vc<int>(n);rep(i,n)cin>>a[i];
    vc<int>idx(n);
    iota(all(idx),0);
    stable_sort(all(idx),[&](int i,int j){
            return a[i]>a[j];
    });
    
    {
        set<int>st;
        for(auto&i:idx){
            auto itr=st.lower_bound(i);
            if(itr!=st.end())ps.push_back({i,*itr});
            if(itr!=st.begin())ps.push_back({*prev(itr),i});
            st.insert(i);
        }
    }
    dbg(ps);
    for(auto&x:ps){
        x.second+=x.second-x.first;
    }
    dbg(ps);
    kdtree<int,S,ll,op,e,Apply,Merge,id,init>kd(ps);
    vvc<pair<int,int>>query(n);
    INT(q);
    vc<ll>ans(q);
    rep(i,q){
        INT(l,r);
        --l,--r;
        query[r].push_back({l,i});
    }
    rep(i,n){
        if(i>=2)kd.apply(0,i-2,0,i,a[i]);
        for(auto&[l,idx]:query[i]){
            ans[idx]=kd.prod(l,i-2,l,2e9).max_val;
        }
    }
    rep(i,q){
        cout<<ans[i]<<"\n";
    }
}
signed main(){
    int t=1;
    //cin>>t;
    while(t--)solve();
    dbg(TIMER.out());
}
/*
考える絵べきものは[l,mid] で[l,mid] がごくだいなもの
大きいほうから追加していくと[l,mid] の候補が高々 O(N) 個になる
kdtreeで殴れるか・
*/
/*
15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
1
11 14

*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...