제출 #1294658

#제출 시각아이디문제언어결과실행 시간메모리
1294658trandangquangTwo Antennas (JOI19_antennas)C++20
100 / 100
552 ms41928 KiB
#include<bits/stdc++.h>
using namespace std;

#define foru(i,a,b) for(int i=(a); i<=(b); ++i)
#define ford(i,a,b) for(int i=(a); i>=(b); --i)
#define rep(i,a) for(int i=0; i<(a); ++i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(),(a).end()
#define bit(s,i) (((s)>>(i))&1)
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
#define _ << " " <<

template <class X, class Y> bool maxi(X &x, Y y){return x<y?x=y,true:false;}
template <class X, class Y> bool mini(X &x, Y y){return x>y?x=y,true:false;}

const int N=2e5+5;
const int INF=1e9;

int n,q,h[N],a[N],b[N],ans[N],lq[N],rq[N];
vi upd[N];
vii que[N];

struct{
    #define lc id<<1
    #define rc id<<1|1

    struct Node{
        int c,d,lz;
        Node(){
            c=-INF, d=-INF, lz=INF;
        }
    } st[N<<2];

    Node combine(Node a, Node b){
        Node c;
        c.c=max(a.c,b.c);
        c.d=max(a.d,b.d);
        return c;
    }
    void apply(int id, int val){
        maxi(st[id].d,st[id].c-val);
        mini(st[id].lz,val);
    }
    void down(int id){
        if(st[id].lz==INF) return;
        apply(lc,st[id].lz);
        apply(rc,st[id].lz);
        st[id].lz=INF;
    }
    void updMx(int u, int v, int h, int id=1, int l=1, int r=n){
        if(u>r||v<l) return;
        if(u<=l&&r<=v){
            apply(id,h);
            return;
        }
        int mid=(l+r)>>1;
        down(id);
        updMx(u,v,h,lc,l,mid);
        updMx(u,v,h,rc,mid+1,r);
        st[id]=combine(st[lc],st[rc]);
    }
    void setc(int x, int h){
        int id=1, l=1, r=n;
        while(l<r){
            int mid=(l+r)>>1;
            down(id);
            if(x<=mid) r=mid, id=lc;
            else l=mid+1, id=rc;
        }
        st[id].c=h;
        while(id>1){
            id/=2;
            st[id]=combine(st[lc],st[rc]);
        }
    }
    int getMx(int u, int v, int id=1, int l=1, int r=n){
        if(u>r||v<l) return -INF;
        if(u<=l&&r<=v) return st[id].d;
        down(id);
        int mid=(l+r)>>1;
        return max(getMx(u,v,lc,l,mid), getMx(u,v,rc,mid+1,r));
    }
} smt;

struct{
    #define lc id<<1
    #define rc id<<1|1

    struct Node{
        int c,d,lz;
        Node(){
            c=INF, d=-INF, lz=-INF;
        }
    } st[N<<2];

    Node combine(Node a, Node b){
        Node c;
        c.c=min(a.c,b.c);
        c.d=max(a.d,b.d);
        return c;
    }
    void apply(int id, int val){
        maxi(st[id].d,val-st[id].c);
        maxi(st[id].lz,val);
    }
    void down(int id){
        if(st[id].lz==-INF) return;
        apply(lc,st[id].lz);
        apply(rc,st[id].lz);
        st[id].lz=-INF;
    }
    void updMi(int u, int v, int h, int id=1, int l=1, int r=n){
        if(u>r||v<l) return;
        if(u<=l&&r<=v){
            apply(id,h);
            return;
        }
        int mid=(l+r)>>1;
        down(id);
        updMi(u,v,h,lc,l,mid);
        updMi(u,v,h,rc,mid+1,r);
        st[id]=combine(st[lc],st[rc]);
    }
    void setc(int x, int h){
        int id=1, l=1, r=n;
        while(l<r){
            int mid=(l+r)>>1;
            down(id);
            if(x<=mid) r=mid, id=lc;
            else l=mid+1, id=rc;
        }
        st[id].c=h;
        while(id>1){
            id/=2;
            st[id]=combine(st[lc],st[rc]);
        }
    }
    int getMx(int u, int v, int id=1, int l=1, int r=n){
        if(u>r||v<l) return -INF;
        if(u<=l&&r<=v) return st[id].d;
        down(id);
        int mid=(l+r)>>1;
        return max(getMx(u,v,lc,l,mid), getMx(u,v,rc,mid+1,r));
    }
} smt2;

void solve(){
    cin>>n;
    foru(i,1,n){
        cin>>h[i]>>a[i]>>b[i];
    }
    cin>>q;
    foru(i,1,q){
        cin>>lq[i]>>rq[i];
    }

    memset(ans,-1,sizeof(ans));

    foru(i,1,q){
        que[rq[i]].eb(lq[i],i);
    }
    foru(i,1,n){
        int L=i+a[i], R=min(i+b[i],n);
        if(L<=n){
            upd[L].eb(i);
            upd[R+1].eb(-i);
        }
    }
    foru(i,1,n+1){
        for(int j:upd[i]){
            if(j>0){
                smt.setc(j,h[j]);
                smt2.setc(j,h[j]);
            } else{
                j=-j;
                smt.setc(j,-INF);
                smt2.setc(j,INF);
            }
        }
        if(i<=n){
            smt.updMx(i-b[i],i-a[i],h[i]);
            smt2.updMi(i-b[i],i-a[i],h[i]);
            for(auto [l,id]:que[i]){
                maxi(ans[id],max(smt.getMx(l,i),smt2.getMx(l,i)));
            }
        }
    }

    foru(i,1,q) cout<<ans[i]<<'\n';
}

int32_t main(){
    #define task "test"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin.tie(0)->sync_with_stdio(0);

    int tc=1; //cin>>tc;
    foru(i,1,tc){
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

antennas.cpp: In function 'int32_t main()':
antennas.cpp:203:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  203 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:204:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  204 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...