답안 #265360

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
265360 2020-08-14T16:24:40 Z eohomegrownapps Progression (NOI20_progression) C++14
100 / 100
2805 ms 68440 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int deltas[400000];

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

int n;
int ncnt=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 initv(ll ind){
    Data ans;
    ans.prefval = deltas[ind];
    ans.suffval = deltas[ind];
    ans.deltasum = deltas[ind];
    return ans;
}

struct Node{
    int s,e;
    Data v;

    Node *l, *r;

    ll lazyval = 0;
    bool doreplace = false;

    Node(ll _s, ll _e){
        ncnt++;
        if (ncnt>2*n){
            while (true){continue;}
        }
        s=_s;e=_e;
        //cout<<s<<' '<<e<<'\n';
        if (s==e){
            v = initv(s);
            return;
        }
        int 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(ll a, ll b){
        assert(a<=b);
        assert(b<=n-2);
        int m = (s+e)/2;
        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 {
            return combine(l->query(a,m),r->query(m+1,b));
        }
    }

    void update(ll a, ll b, ll val, bool replace){
        assert(b<=n-2);
        int m = (s+e)/2;
        //cout<<"update "<<a<<' '<<b<<' '<<val<<' '<<replace<<'\n';
        assert(a<=b);
        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);
    }
};

ll x0;
Node *root;

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

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

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

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

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

int main(){
    //cin.tie(0);
    //ios_base::sync_with_stdio(0);
    ll q;
    cin>>n>>q;
    cin>>x0;
    ll xprev = x0;
    for (ll i = 1; i<n; i++){
        ll xnew;
        cin>>xnew;
        deltas[i-1]=xnew-xprev;
        xprev=xnew;
    }
    root = new Node(0,n-1);
    /*for (ll i = 0; i<n-1; i++){
        cout<<getDelta(i)<<' ';
    }cout<<'\n';
    for (ll i = 0; i<n; i++){
        cout<<getVal(i)<<' ';
    }cout<<'\n';*/
    for (ll i = 0; i<q; i++){
        ll t;
        cin>>t;
        ll l,r,s,c;
        if (t==1){
            cin>>l>>r>>s>>c;
            patch(l-1,r-1,s,c);
            /*for (ll i = 0; i<n-1; i++){
                cout<<getDelta(i)<<' ';
            }cout<<'\n';
            for (ll i = 0; i<n; i++){
                cout<<getVal(i)<<' ';
            }cout<<'\n';*/
        } else if (t==2){
            cin>>l>>r>>s>>c;
            rewrite(l-1,r-1,s,c);
            /*for (ll i = 0; i<n-1; i++){
                cout<<getDelta(i)<<' ';
            }cout<<'\n';
            for (ll i = 0; i<n; i++){
                cout<<getVal(i)<<' ';
            }cout<<'\n';*/
        } else if (t==3){
            cin>>l>>r;
            cout<<evaluate(l-1,r-1)<<'\n';
        } else {
            continue;
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1404 ms 58556 KB Output is correct
2 Correct 691 ms 760 KB Output is correct
3 Correct 706 ms 760 KB Output is correct
4 Correct 711 ms 616 KB Output is correct
5 Correct 730 ms 772 KB Output is correct
6 Correct 698 ms 784 KB Output is correct
7 Correct 681 ms 632 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 1369 ms 66552 KB Output is correct
12 Correct 1362 ms 66692 KB Output is correct
13 Correct 1367 ms 66808 KB Output is correct
14 Correct 1370 ms 67064 KB Output is correct
15 Correct 1341 ms 66728 KB Output is correct
16 Correct 1476 ms 66452 KB Output is correct
17 Correct 1463 ms 66460 KB Output is correct
18 Correct 1394 ms 66424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 4 ms 256 KB Output is correct
3 Correct 4 ms 256 KB Output is correct
4 Correct 3 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 4 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 512 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 7 ms 512 KB Output is correct
13 Correct 6 ms 512 KB Output is correct
14 Correct 5 ms 512 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 8 ms 512 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 3 ms 292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1746 ms 58896 KB Output is correct
2 Correct 974 ms 1016 KB Output is correct
3 Correct 981 ms 1056 KB Output is correct
4 Correct 1006 ms 916 KB Output is correct
5 Correct 1031 ms 1024 KB Output is correct
6 Correct 1068 ms 984 KB Output is correct
7 Correct 1006 ms 1076 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 1711 ms 63760 KB Output is correct
12 Correct 1693 ms 65072 KB Output is correct
13 Correct 1704 ms 63608 KB Output is correct
14 Correct 1720 ms 63608 KB Output is correct
15 Correct 1721 ms 64984 KB Output is correct
16 Correct 1809 ms 65700 KB Output is correct
17 Correct 1802 ms 65640 KB Output is correct
18 Correct 1805 ms 65836 KB Output is correct
19 Correct 1652 ms 65080 KB Output is correct
20 Correct 1640 ms 64960 KB Output is correct
21 Correct 1689 ms 65200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2135 ms 58384 KB Output is correct
2 Correct 696 ms 508 KB Output is correct
3 Correct 701 ms 504 KB Output is correct
4 Correct 697 ms 632 KB Output is correct
5 Correct 718 ms 584 KB Output is correct
6 Correct 724 ms 760 KB Output is correct
7 Correct 714 ms 632 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 2005 ms 64888 KB Output is correct
12 Correct 2198 ms 68044 KB Output is correct
13 Correct 1934 ms 64760 KB Output is correct
14 Correct 1914 ms 64772 KB Output is correct
15 Correct 2037 ms 67876 KB Output is correct
16 Correct 2198 ms 67912 KB Output is correct
17 Correct 2315 ms 67880 KB Output is correct
18 Correct 2176 ms 68108 KB Output is correct
19 Correct 2106 ms 67960 KB Output is correct
20 Correct 2123 ms 68088 KB Output is correct
21 Correct 2185 ms 68088 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1746 ms 58896 KB Output is correct
2 Correct 974 ms 1016 KB Output is correct
3 Correct 981 ms 1056 KB Output is correct
4 Correct 1006 ms 916 KB Output is correct
5 Correct 1031 ms 1024 KB Output is correct
6 Correct 1068 ms 984 KB Output is correct
7 Correct 1006 ms 1076 KB Output is correct
8 Correct 4 ms 256 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 1711 ms 63760 KB Output is correct
12 Correct 1693 ms 65072 KB Output is correct
13 Correct 1704 ms 63608 KB Output is correct
14 Correct 1720 ms 63608 KB Output is correct
15 Correct 1721 ms 64984 KB Output is correct
16 Correct 1809 ms 65700 KB Output is correct
17 Correct 1802 ms 65640 KB Output is correct
18 Correct 1805 ms 65836 KB Output is correct
19 Correct 1652 ms 65080 KB Output is correct
20 Correct 1640 ms 64960 KB Output is correct
21 Correct 1689 ms 65200 KB Output is correct
22 Correct 2437 ms 67524 KB Output is correct
23 Correct 808 ms 3704 KB Output is correct
24 Correct 799 ms 3704 KB Output is correct
25 Correct 792 ms 3704 KB Output is correct
26 Correct 800 ms 3836 KB Output is correct
27 Correct 789 ms 3704 KB Output is correct
28 Correct 793 ms 3704 KB Output is correct
29 Correct 3 ms 384 KB Output is correct
30 Correct 3 ms 384 KB Output is correct
31 Correct 3 ms 384 KB Output is correct
32 Correct 2306 ms 64372 KB Output is correct
33 Correct 2495 ms 67448 KB Output is correct
34 Correct 2252 ms 64608 KB Output is correct
35 Correct 2219 ms 64632 KB Output is correct
36 Correct 2142 ms 64292 KB Output is correct
37 Correct 2180 ms 64376 KB Output is correct
38 Correct 2195 ms 64380 KB Output is correct
39 Correct 2489 ms 67360 KB Output is correct
40 Correct 2462 ms 67576 KB Output is correct
41 Correct 2495 ms 67572 KB Output is correct
42 Correct 2497 ms 67184 KB Output is correct
43 Correct 2356 ms 67320 KB Output is correct
44 Correct 2323 ms 67320 KB Output is correct
45 Correct 2458 ms 67132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1404 ms 58556 KB Output is correct
2 Correct 691 ms 760 KB Output is correct
3 Correct 706 ms 760 KB Output is correct
4 Correct 711 ms 616 KB Output is correct
5 Correct 730 ms 772 KB Output is correct
6 Correct 698 ms 784 KB Output is correct
7 Correct 681 ms 632 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 1369 ms 66552 KB Output is correct
12 Correct 1362 ms 66692 KB Output is correct
13 Correct 1367 ms 66808 KB Output is correct
14 Correct 1370 ms 67064 KB Output is correct
15 Correct 1341 ms 66728 KB Output is correct
16 Correct 1476 ms 66452 KB Output is correct
17 Correct 1463 ms 66460 KB Output is correct
18 Correct 1394 ms 66424 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 4 ms 256 KB Output is correct
21 Correct 4 ms 256 KB Output is correct
22 Correct 3 ms 384 KB Output is correct
23 Correct 3 ms 256 KB Output is correct
24 Correct 4 ms 384 KB Output is correct
25 Correct 4 ms 384 KB Output is correct
26 Correct 5 ms 512 KB Output is correct
27 Correct 5 ms 512 KB Output is correct
28 Correct 5 ms 512 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 7 ms 512 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 5 ms 512 KB Output is correct
33 Correct 6 ms 512 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 7 ms 512 KB Output is correct
36 Correct 8 ms 512 KB Output is correct
37 Correct 3 ms 384 KB Output is correct
38 Correct 3 ms 384 KB Output is correct
39 Correct 3 ms 292 KB Output is correct
40 Correct 1746 ms 58896 KB Output is correct
41 Correct 974 ms 1016 KB Output is correct
42 Correct 981 ms 1056 KB Output is correct
43 Correct 1006 ms 916 KB Output is correct
44 Correct 1031 ms 1024 KB Output is correct
45 Correct 1068 ms 984 KB Output is correct
46 Correct 1006 ms 1076 KB Output is correct
47 Correct 4 ms 256 KB Output is correct
48 Correct 4 ms 256 KB Output is correct
49 Correct 4 ms 384 KB Output is correct
50 Correct 1711 ms 63760 KB Output is correct
51 Correct 1693 ms 65072 KB Output is correct
52 Correct 1704 ms 63608 KB Output is correct
53 Correct 1720 ms 63608 KB Output is correct
54 Correct 1721 ms 64984 KB Output is correct
55 Correct 1809 ms 65700 KB Output is correct
56 Correct 1802 ms 65640 KB Output is correct
57 Correct 1805 ms 65836 KB Output is correct
58 Correct 1652 ms 65080 KB Output is correct
59 Correct 1640 ms 64960 KB Output is correct
60 Correct 1689 ms 65200 KB Output is correct
61 Correct 2135 ms 58384 KB Output is correct
62 Correct 696 ms 508 KB Output is correct
63 Correct 701 ms 504 KB Output is correct
64 Correct 697 ms 632 KB Output is correct
65 Correct 718 ms 584 KB Output is correct
66 Correct 724 ms 760 KB Output is correct
67 Correct 714 ms 632 KB Output is correct
68 Correct 3 ms 256 KB Output is correct
69 Correct 3 ms 384 KB Output is correct
70 Correct 3 ms 384 KB Output is correct
71 Correct 2005 ms 64888 KB Output is correct
72 Correct 2198 ms 68044 KB Output is correct
73 Correct 1934 ms 64760 KB Output is correct
74 Correct 1914 ms 64772 KB Output is correct
75 Correct 2037 ms 67876 KB Output is correct
76 Correct 2198 ms 67912 KB Output is correct
77 Correct 2315 ms 67880 KB Output is correct
78 Correct 2176 ms 68108 KB Output is correct
79 Correct 2106 ms 67960 KB Output is correct
80 Correct 2123 ms 68088 KB Output is correct
81 Correct 2185 ms 68088 KB Output is correct
82 Correct 2437 ms 67524 KB Output is correct
83 Correct 808 ms 3704 KB Output is correct
84 Correct 799 ms 3704 KB Output is correct
85 Correct 792 ms 3704 KB Output is correct
86 Correct 800 ms 3836 KB Output is correct
87 Correct 789 ms 3704 KB Output is correct
88 Correct 793 ms 3704 KB Output is correct
89 Correct 3 ms 384 KB Output is correct
90 Correct 3 ms 384 KB Output is correct
91 Correct 3 ms 384 KB Output is correct
92 Correct 2306 ms 64372 KB Output is correct
93 Correct 2495 ms 67448 KB Output is correct
94 Correct 2252 ms 64608 KB Output is correct
95 Correct 2219 ms 64632 KB Output is correct
96 Correct 2142 ms 64292 KB Output is correct
97 Correct 2180 ms 64376 KB Output is correct
98 Correct 2195 ms 64380 KB Output is correct
99 Correct 2489 ms 67360 KB Output is correct
100 Correct 2462 ms 67576 KB Output is correct
101 Correct 2495 ms 67572 KB Output is correct
102 Correct 2497 ms 67184 KB Output is correct
103 Correct 2356 ms 67320 KB Output is correct
104 Correct 2323 ms 67320 KB Output is correct
105 Correct 2458 ms 67132 KB Output is correct
106 Correct 2748 ms 68140 KB Output is correct
107 Correct 787 ms 3960 KB Output is correct
108 Correct 776 ms 3960 KB Output is correct
109 Correct 779 ms 3960 KB Output is correct
110 Correct 3 ms 384 KB Output is correct
111 Correct 3 ms 384 KB Output is correct
112 Correct 3 ms 384 KB Output is correct
113 Correct 2445 ms 67356 KB Output is correct
114 Correct 2496 ms 67320 KB Output is correct
115 Correct 2400 ms 67364 KB Output is correct
116 Correct 2430 ms 67420 KB Output is correct
117 Correct 2776 ms 68224 KB Output is correct
118 Correct 2371 ms 67408 KB Output is correct
119 Correct 2381 ms 67148 KB Output is correct
120 Correct 1750 ms 66024 KB Output is correct
121 Correct 1758 ms 65912 KB Output is correct
122 Correct 1805 ms 65776 KB Output is correct
123 Correct 1682 ms 65012 KB Output is correct
124 Correct 1637 ms 65044 KB Output is correct
125 Correct 1624 ms 65096 KB Output is correct
126 Correct 2441 ms 65272 KB Output is correct
127 Correct 2629 ms 65004 KB Output is correct
128 Correct 2757 ms 68324 KB Output is correct
129 Correct 2571 ms 64876 KB Output is correct
130 Correct 2264 ms 65012 KB Output is correct
131 Correct 2206 ms 65052 KB Output is correct
132 Correct 2345 ms 64948 KB Output is correct
133 Correct 2791 ms 68332 KB Output is correct
134 Correct 2710 ms 68440 KB Output is correct
135 Correct 2805 ms 68208 KB Output is correct
136 Correct 788 ms 3996 KB Output is correct
137 Correct 780 ms 3732 KB Output is correct
138 Correct 787 ms 3964 KB Output is correct