답안 #265365

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
265365 2020-08-14T16:29:25 Z eohomegrownapps Progression (NOI20_progression) C++14
100 / 100
2867 ms 79596 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll deltas[300000];
 
struct Data{
    ll len = 1;
    ll maxpref=1;
    ll maxsuff=1;
    ll prefval=0;
    ll suffval=0;
    ll maxlen=1;
    ll 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(ll ind){
    Data ans;
    ans.prefval = deltas[ind];
    ans.suffval = deltas[ind];
    ans.deltasum = deltas[ind];
    return ans;
}
 
struct Node{
    ll s,e,m;
    Data v;
 
    Node *l, *r;
 
    ll lazyval = 0;
    bool doreplace = false;
 
    Node(ll _s, ll _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(ll a, ll 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(ll a, ll b, ll val, bool replace){
        //cout<<"update "<<a<<' '<<b<<' '<<val<<' '<<replace<<'\n';
        if (a>b){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);
    }
};
 
ll x0,n;
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);
    }
    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 xlm1, xr1;
    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;
        }
    }
}

Compilation message

Progression.cpp: In function 'void rewrite(ll, ll, ll, ll)':
Progression.cpp:174:8: warning: unused variable 'xlm1' [-Wunused-variable]
  174 |     ll xlm1, xr1;
      |        ^~~~
Progression.cpp:188:29: warning: 'xr1' may be used uninitialized in this function [-Wmaybe-uninitialized]
  188 |         root->update(r,r,xr1-s-(r-l)*c,true);
      |                          ~~~^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1361 ms 78684 KB Output is correct
2 Correct 716 ms 760 KB Output is correct
3 Correct 735 ms 760 KB Output is correct
4 Correct 712 ms 760 KB Output is correct
5 Correct 723 ms 760 KB Output is correct
6 Correct 693 ms 760 KB Output is correct
7 Correct 700 ms 760 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 3 ms 256 KB Output is correct
11 Correct 1402 ms 78712 KB Output is correct
12 Correct 1377 ms 78596 KB Output is correct
13 Correct 1378 ms 79096 KB Output is correct
14 Correct 1390 ms 78976 KB Output is correct
15 Correct 1404 ms 78776 KB Output is correct
16 Correct 1437 ms 78728 KB Output is correct
17 Correct 1384 ms 78700 KB Output is correct
18 Correct 1391 ms 78504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 640 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 256 KB Output is correct
6 Correct 3 ms 256 KB Output is correct
7 Correct 4 ms 256 KB Output is correct
8 Correct 5 ms 512 KB Output is correct
9 Correct 5 ms 640 KB Output is correct
10 Correct 5 ms 512 KB Output is correct
11 Correct 5 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 5 ms 512 KB Output is correct
14 Correct 5 ms 640 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 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 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1718 ms 79224 KB Output is correct
2 Correct 1063 ms 1016 KB Output is correct
3 Correct 998 ms 1016 KB Output is correct
4 Correct 967 ms 1144 KB Output is correct
5 Correct 965 ms 1144 KB Output is correct
6 Correct 975 ms 1144 KB Output is correct
7 Correct 982 ms 1144 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 1809 ms 78972 KB Output is correct
12 Correct 1712 ms 79104 KB Output is correct
13 Correct 1669 ms 78616 KB Output is correct
14 Correct 1664 ms 78708 KB Output is correct
15 Correct 1701 ms 78792 KB Output is correct
16 Correct 1845 ms 79352 KB Output is correct
17 Correct 1830 ms 79476 KB Output is correct
18 Correct 1848 ms 79372 KB Output is correct
19 Correct 1715 ms 78628 KB Output is correct
20 Correct 1762 ms 78584 KB Output is correct
21 Correct 1724 ms 78840 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2166 ms 78236 KB Output is correct
2 Correct 727 ms 504 KB Output is correct
3 Correct 712 ms 504 KB Output is correct
4 Correct 732 ms 504 KB Output is correct
5 Correct 710 ms 760 KB Output is correct
6 Correct 735 ms 504 KB Output is correct
7 Correct 741 ms 632 KB Output is correct
8 Correct 3 ms 256 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 3 ms 256 KB Output is correct
11 Correct 2063 ms 78176 KB Output is correct
12 Correct 2243 ms 78396 KB Output is correct
13 Correct 1931 ms 78308 KB Output is correct
14 Correct 1909 ms 78200 KB Output is correct
15 Correct 2079 ms 78172 KB Output is correct
16 Correct 2303 ms 78336 KB Output is correct
17 Correct 2199 ms 78252 KB Output is correct
18 Correct 2180 ms 78244 KB Output is correct
19 Correct 2143 ms 78088 KB Output is correct
20 Correct 2252 ms 78204 KB Output is correct
21 Correct 2212 ms 78116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1718 ms 79224 KB Output is correct
2 Correct 1063 ms 1016 KB Output is correct
3 Correct 998 ms 1016 KB Output is correct
4 Correct 967 ms 1144 KB Output is correct
5 Correct 965 ms 1144 KB Output is correct
6 Correct 975 ms 1144 KB Output is correct
7 Correct 982 ms 1144 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 256 KB Output is correct
10 Correct 4 ms 256 KB Output is correct
11 Correct 1809 ms 78972 KB Output is correct
12 Correct 1712 ms 79104 KB Output is correct
13 Correct 1669 ms 78616 KB Output is correct
14 Correct 1664 ms 78708 KB Output is correct
15 Correct 1701 ms 78792 KB Output is correct
16 Correct 1845 ms 79352 KB Output is correct
17 Correct 1830 ms 79476 KB Output is correct
18 Correct 1848 ms 79372 KB Output is correct
19 Correct 1715 ms 78628 KB Output is correct
20 Correct 1762 ms 78584 KB Output is correct
21 Correct 1724 ms 78840 KB Output is correct
22 Correct 2452 ms 78468 KB Output is correct
23 Correct 811 ms 760 KB Output is correct
24 Correct 802 ms 768 KB Output is correct
25 Correct 828 ms 760 KB Output is correct
26 Correct 835 ms 632 KB Output is correct
27 Correct 825 ms 760 KB Output is correct
28 Correct 810 ms 576 KB Output is correct
29 Correct 3 ms 256 KB Output is correct
30 Correct 3 ms 256 KB Output is correct
31 Correct 3 ms 256 KB Output is correct
32 Correct 2301 ms 78328 KB Output is correct
33 Correct 2372 ms 78200 KB Output is correct
34 Correct 2430 ms 78320 KB Output is correct
35 Correct 2358 ms 78316 KB Output is correct
36 Correct 2128 ms 78708 KB Output is correct
37 Correct 2124 ms 78788 KB Output is correct
38 Correct 2097 ms 78840 KB Output is correct
39 Correct 2524 ms 78372 KB Output is correct
40 Correct 2523 ms 78516 KB Output is correct
41 Correct 2486 ms 78584 KB Output is correct
42 Correct 2566 ms 78384 KB Output is correct
43 Correct 2446 ms 78416 KB Output is correct
44 Correct 2447 ms 78312 KB Output is correct
45 Correct 2449 ms 78416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1361 ms 78684 KB Output is correct
2 Correct 716 ms 760 KB Output is correct
3 Correct 735 ms 760 KB Output is correct
4 Correct 712 ms 760 KB Output is correct
5 Correct 723 ms 760 KB Output is correct
6 Correct 693 ms 760 KB Output is correct
7 Correct 700 ms 760 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 256 KB Output is correct
10 Correct 3 ms 256 KB Output is correct
11 Correct 1402 ms 78712 KB Output is correct
12 Correct 1377 ms 78596 KB Output is correct
13 Correct 1378 ms 79096 KB Output is correct
14 Correct 1390 ms 78976 KB Output is correct
15 Correct 1404 ms 78776 KB Output is correct
16 Correct 1437 ms 78728 KB Output is correct
17 Correct 1384 ms 78700 KB Output is correct
18 Correct 1391 ms 78504 KB Output is correct
19 Correct 7 ms 640 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 5 ms 384 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 3 ms 256 KB Output is correct
24 Correct 3 ms 256 KB Output is correct
25 Correct 4 ms 256 KB Output is correct
26 Correct 5 ms 512 KB Output is correct
27 Correct 5 ms 640 KB Output is correct
28 Correct 5 ms 512 KB Output is correct
29 Correct 5 ms 512 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 5 ms 512 KB Output is correct
32 Correct 5 ms 640 KB Output is correct
33 Correct 6 ms 512 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 6 ms 512 KB Output is correct
36 Correct 6 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 384 KB Output is correct
40 Correct 1718 ms 79224 KB Output is correct
41 Correct 1063 ms 1016 KB Output is correct
42 Correct 998 ms 1016 KB Output is correct
43 Correct 967 ms 1144 KB Output is correct
44 Correct 965 ms 1144 KB Output is correct
45 Correct 975 ms 1144 KB Output is correct
46 Correct 982 ms 1144 KB Output is correct
47 Correct 4 ms 384 KB Output is correct
48 Correct 4 ms 256 KB Output is correct
49 Correct 4 ms 256 KB Output is correct
50 Correct 1809 ms 78972 KB Output is correct
51 Correct 1712 ms 79104 KB Output is correct
52 Correct 1669 ms 78616 KB Output is correct
53 Correct 1664 ms 78708 KB Output is correct
54 Correct 1701 ms 78792 KB Output is correct
55 Correct 1845 ms 79352 KB Output is correct
56 Correct 1830 ms 79476 KB Output is correct
57 Correct 1848 ms 79372 KB Output is correct
58 Correct 1715 ms 78628 KB Output is correct
59 Correct 1762 ms 78584 KB Output is correct
60 Correct 1724 ms 78840 KB Output is correct
61 Correct 2166 ms 78236 KB Output is correct
62 Correct 727 ms 504 KB Output is correct
63 Correct 712 ms 504 KB Output is correct
64 Correct 732 ms 504 KB Output is correct
65 Correct 710 ms 760 KB Output is correct
66 Correct 735 ms 504 KB Output is correct
67 Correct 741 ms 632 KB Output is correct
68 Correct 3 ms 256 KB Output is correct
69 Correct 3 ms 256 KB Output is correct
70 Correct 3 ms 256 KB Output is correct
71 Correct 2063 ms 78176 KB Output is correct
72 Correct 2243 ms 78396 KB Output is correct
73 Correct 1931 ms 78308 KB Output is correct
74 Correct 1909 ms 78200 KB Output is correct
75 Correct 2079 ms 78172 KB Output is correct
76 Correct 2303 ms 78336 KB Output is correct
77 Correct 2199 ms 78252 KB Output is correct
78 Correct 2180 ms 78244 KB Output is correct
79 Correct 2143 ms 78088 KB Output is correct
80 Correct 2252 ms 78204 KB Output is correct
81 Correct 2212 ms 78116 KB Output is correct
82 Correct 2452 ms 78468 KB Output is correct
83 Correct 811 ms 760 KB Output is correct
84 Correct 802 ms 768 KB Output is correct
85 Correct 828 ms 760 KB Output is correct
86 Correct 835 ms 632 KB Output is correct
87 Correct 825 ms 760 KB Output is correct
88 Correct 810 ms 576 KB Output is correct
89 Correct 3 ms 256 KB Output is correct
90 Correct 3 ms 256 KB Output is correct
91 Correct 3 ms 256 KB Output is correct
92 Correct 2301 ms 78328 KB Output is correct
93 Correct 2372 ms 78200 KB Output is correct
94 Correct 2430 ms 78320 KB Output is correct
95 Correct 2358 ms 78316 KB Output is correct
96 Correct 2128 ms 78708 KB Output is correct
97 Correct 2124 ms 78788 KB Output is correct
98 Correct 2097 ms 78840 KB Output is correct
99 Correct 2524 ms 78372 KB Output is correct
100 Correct 2523 ms 78516 KB Output is correct
101 Correct 2486 ms 78584 KB Output is correct
102 Correct 2566 ms 78384 KB Output is correct
103 Correct 2446 ms 78416 KB Output is correct
104 Correct 2447 ms 78312 KB Output is correct
105 Correct 2449 ms 78416 KB Output is correct
106 Correct 2831 ms 78640 KB Output is correct
107 Correct 781 ms 760 KB Output is correct
108 Correct 774 ms 632 KB Output is correct
109 Correct 778 ms 760 KB Output is correct
110 Correct 3 ms 256 KB Output is correct
111 Correct 3 ms 256 KB Output is correct
112 Correct 3 ms 256 KB Output is correct
113 Correct 2496 ms 78640 KB Output is correct
114 Correct 2582 ms 78432 KB Output is correct
115 Correct 2619 ms 78632 KB Output is correct
116 Correct 2449 ms 78556 KB Output is correct
117 Correct 2772 ms 78444 KB Output is correct
118 Correct 2438 ms 78328 KB Output is correct
119 Correct 2577 ms 78352 KB Output is correct
120 Correct 1831 ms 79596 KB Output is correct
121 Correct 1816 ms 79224 KB Output is correct
122 Correct 1844 ms 79472 KB Output is correct
123 Correct 1677 ms 78736 KB Output is correct
124 Correct 1697 ms 78644 KB Output is correct
125 Correct 1645 ms 78592 KB Output is correct
126 Correct 2550 ms 78616 KB Output is correct
127 Correct 2553 ms 78576 KB Output is correct
128 Correct 2867 ms 78772 KB Output is correct
129 Correct 2539 ms 78672 KB Output is correct
130 Correct 2230 ms 79268 KB Output is correct
131 Correct 2250 ms 79388 KB Output is correct
132 Correct 2249 ms 79496 KB Output is correct
133 Correct 2827 ms 78616 KB Output is correct
134 Correct 2770 ms 78584 KB Output is correct
135 Correct 2753 ms 78612 KB Output is correct
136 Correct 783 ms 764 KB Output is correct
137 Correct 806 ms 760 KB Output is correct
138 Correct 779 ms 888 KB Output is correct