답안 #265291

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
265291 2020-08-14T15:01:09 Z eohomegrownapps Progression (NOI20_progression) C++14
0 / 100
755 ms 1048580 KB
#include <bits/stdc++.h>
using namespace std;

int deltas[300000];

struct Data{
    int len = 1;
    int maxpref=1;
    int maxsuff=1;
    int prefval=0;
    int suffval=0;
    int maxlen=1;
    int deltasum = 0;
};

Data combine(const Data &l, const Data &r){
    Data ans;
    ans.len = l.len + r.len;
    ans.deltasum = l.deltasum + r.deltasum;
    ans.maxpref = l.maxpref;
    ans.prefval = l.prefval;
    ans.maxsuff = r.maxsuff;
    ans.suffval = r.suffval;
    if (l.maxpref==l.len&&l.prefval==r.prefval){
        ans.maxpref=l.len+r.maxpref;
    }
    if (r.maxpref==r.len&&r.prefval==l.suffval){
        ans.maxsuff=r.len+l.maxsuff;
    }
    ans.maxlen=max(l.maxlen,r.maxlen);
    if (l.suffval==r.prefval){
        ans.maxlen=max(ans.maxlen,l.maxsuff+r.maxpref);
    }
    return ans;
}

Data init(int ind){
    Data ans;
    ans.prefval = deltas[ind];
    ans.suffval = deltas[ind];
    ans.deltasum = deltas[ind];
    return ans;
}

struct Node{
    int s,e,m;
    Data v;

    Node *l, *r;

    int lazyval = 0;
    bool doreplace = false;

    Node(int _s, int _e){
        s=_s;e=_e;
        //cout<<s<<' '<<e<<'\n';
        if (s==e){
            v = init(s);
            return;
        }
        m = (s+e)/2;
        l = new Node(s,m);
        r = new Node(m+1,e);
        v = combine(l->v, r->v);
    }

    void propagate(){
        //cout<<"propagate "<<s<<' '<<e<<'\n';
        //cout<<"lazy: "<<lazyval<<' '<<doreplace<<'\n';
        if (doreplace){
            v.prefval = lazyval;
            v.suffval = lazyval;
            v.deltasum = v.len * lazyval;
            v.maxpref = v.len;
            v.maxsuff = v.len;
            v.maxlen = v.len;
        } else {
            v.prefval+=lazyval;
            v.suffval+=lazyval;
            v.deltasum+=v.len * lazyval;
        }
        if (s!=e){
            if (doreplace){
                l->doreplace = true;
                l->lazyval = lazyval;
                r->doreplace = true;
                r->lazyval = lazyval;
            } else {
                l->lazyval+=lazyval;
                r->lazyval+=lazyval;
            }
        }
        lazyval=0;
        doreplace=false;
    }

    Data query(int a, int b){
        propagate();
        if (a==s&&b==e){
            return v;
        }
        if (b<=m){
            return l->query(a,b);
        } else if (m<a){
            return r->query(a,b);
        } else {
            Data dl = l->query(a,m);
            Data dr = r->query(m+1,b);
            //cout<<"combine "<<a<<' '<<m<<' '<<dl.deltasum<<'\n';
            //cout<<"combine "<<m+1<<' '<<b<<' '<<dr.deltasum<<'\n';
            Data c = combine(dl,dr);
            //cout<<"ans: "<<c.deltasum<<'\n';
            return c;
        }
    }

    void update(int a, int b, int val, bool replace){
        //cout<<"update "<<a<<' '<<b<<' '<<val<<' '<<replace<<'\n';
        //if (b<a){return;}
        if (a==s&&b==e){
            if (replace){
                doreplace=true;
                lazyval=val;
            } else {
                lazyval+=val;
            }
            return;
        }
        propagate();
        if (b<=m){
            l->update(a,b,val,replace);
        } else if (m<a){
            r->update(a,b,val,replace);
        } else {
            l->update(a,m,val,replace);
            r->update(m+1,b,val,replace);
        }
        l->propagate();
        r->propagate();
        v = combine(l->v,r->v);
    }
};

int x0,n;
Node *root;

void patch(int l, int r, int s, int c){
    if (l==0){
        x0+=s;
    } else {
        root->update(l-1,l-1,s,false);
    }
    root->update(l,r-1,c,false);
    if (r!=n-1){
        root->update(r,r,-s-(r-l)*c,false);
    }
}

int getDelta(int ind){
    Data dxl = root->query(ind,ind);
    return dxl.deltasum;
}

int getVal(int ind){
    if (ind==0){
        return x0;
    }
    Data dxl = root->query(0,ind-1);
    int ans = x0+dxl.deltasum;
    return ans;
}

void rewrite(int l, int r, int s, int c){
    if (l==0){
        x0=s;
    } else {
        int xlm1 = getVal(l-1);
        root->update(l-1,l-1,s-xlm1,true);
    }
    root->update(l,r-1,c,true);
    if (r!=n-1){
        int xr1 = getVal(r+1);
        root->update(r,r,xr1-s-(r-l)*c,true);
    }
}

int evaluate(int l, int r){
    Data d = root->query(l,r-1);
    return d.maxlen+1;
}

int main(){
    cin.tie(0);
    ios_base::sync_with_stdio(0);
    int q;
    cin>>n>>q;
    cin>>x0;
    int xprev = x0;
    for (int i = 1; i<n; i++){
        int xnew;
        cin>>xnew;
        deltas[i-1]=xnew-xprev;
        xprev=xnew;
    }
    root = new Node(0,n-2);
    /*for (int i = 0; i<n-1; i++){
        int d = getDelta(i);
        cout<<"d at "<<i<<": "<<d<<'\n';
    }
    for (int i = 0; i<n; i++){
        int v = getVal(i);
        cout<<"v at "<<i<<": "<<v<<'\n';
    }*/
    for (int i = 0; i<q; i++){
        int t;
        cin>>t;
        int l,r,s,c;
        if (t==1){
            cin>>l>>r>>s>>c;
            patch(l-1,r-1,s,c);
        } else if (t==2){
            cin>>l>>r>>s>>c;
            rewrite(l-1,r-1,s,c);
        } else if (t==3){
            cin>>l>>r;
            cout<<evaluate(l-1,r-1)<<'\n';
        } else {
            continue;
        }
        /*for (int i = 0; i<n-1; i++){
            int d = getDelta(i);
            cout<<"d at "<<i<<": "<<d<<'\n';
        }
        for (int i = 0; i<n; i++){
            int v = getVal(i);
            cout<<"v at "<<i<<": "<<v<<'\n';
        }*/
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 57208 KB Output is correct
2 Correct 98 ms 3576 KB Output is correct
3 Correct 101 ms 3576 KB Output is correct
4 Correct 98 ms 3576 KB Output is correct
5 Correct 97 ms 3704 KB Output is correct
6 Correct 99 ms 3704 KB Output is correct
7 Correct 98 ms 3576 KB Output is correct
8 Runtime error 755 ms 1048580 KB Execution killed with signal 9
9 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2 ms 896 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 147 ms 100472 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 142 ms 100472 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 147 ms 100472 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 200 ms 57208 KB Output is correct
2 Correct 98 ms 3576 KB Output is correct
3 Correct 101 ms 3576 KB Output is correct
4 Correct 98 ms 3576 KB Output is correct
5 Correct 97 ms 3704 KB Output is correct
6 Correct 99 ms 3704 KB Output is correct
7 Correct 98 ms 3576 KB Output is correct
8 Runtime error 755 ms 1048580 KB Execution killed with signal 9
9 Halted 0 ms 0 KB -