답안 #255289

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
255289 2020-07-31T17:08:57 Z model_code Progression (NOI20_progression) C++17
100 / 100
793 ms 67576 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
struct node {
    i64 left_s;
    i64 left_c;
    i64 right_s; // the value of the rightmost elem
    i64 right_c;
    int left_len;
    int right_len;
    int curr_len;
    // these have already been pre-applied to the above info
    bool is_override;
    i64 push_s;
    i64 push_c;
};
constexpr int DEPTH = 19;
node stree[1 << (DEPTH + 1)];
void construct(int i){
    node& me = stree[i];
    node& left=stree[i<<1];
    node& right=stree[(i<<1)+1];
    const int mylen = 1 << (__builtin_clz(i) - (31 - DEPTH));
    const int childlen = mylen >> 1;

    if (childlen==1) {
        right.right_c = left.left_c = right.left_c = left.right_c = right.left_s - left.right_s;
    }
    me.curr_len=max(left.curr_len,right.curr_len);
    int lmergelen =
        (right.left_s - left.right_s == left.right_c)
            ? ((right.left_c == left.right_c) ? right.left_len : 1)
            : 0;
    int rmergelen =
        (right.left_s - left.right_s == right.left_c)
            ? ((right.left_c == left.right_c) ? left.right_len : 1)
            : 0;
    me.curr_len = max(me.curr_len, max(left.right_len + lmergelen, right.left_len + rmergelen));
    me.left_s = left.left_s;
    me.left_c = left.left_c;
    if(left.left_len == childlen) me.left_len = childlen + lmergelen;
    else me.left_len = left.left_len;
    me.right_s = right.right_s;
    me.right_c = right.right_c;
    if(right.right_len == childlen) me.right_len = childlen + rmergelen;
    else me.right_len = right.right_len;
}
void pushdown(int i){
    node& me = stree[i];
    if(me.is_override||me.push_s!=0||me.push_c!=0){
        node& left=stree[i<<1];
        node& right=stree[(i<<1)+1];
        const int mylen = 1 << (__builtin_clz(i) - (31 - DEPTH));
        const int childlen = mylen >> 1;

        if(me.is_override){
            me.is_override=false;
            left.curr_len=childlen;
            left.left_s=me.push_s;
            left.left_c=me.push_c;
            left.left_len=childlen;
            left.right_s=me.push_s+me.push_c*(childlen-1);
            left.right_c=me.push_c;
            left.right_len=childlen;
            left.push_s=me.push_s;
            left.push_c=me.push_c;
            left.is_override=true;
            const i64 tmp_s=me.push_s+me.push_c*childlen;
            right.curr_len=childlen;
            right.left_s=tmp_s;
            right.left_c=me.push_c;
            right.left_len=childlen;
            right.right_s=tmp_s+me.push_c*(childlen-1);
            right.right_c=me.push_c;
            right.right_len=childlen;
            right.push_s=tmp_s;
            right.push_c=me.push_c;
            right.is_override=true;
        }
        else{
            left.left_s+=me.push_s;
            left.left_c+=me.push_c;
            left.right_s+=me.push_s+me.push_c*(childlen-1);
            left.right_c+=me.push_c;
            left.push_s+=me.push_s;
            left.push_c+=me.push_c;
            const i64 tmp_s=me.push_s+me.push_c*childlen;
            right.left_s+=tmp_s;
            right.left_c+=me.push_c;
            right.right_s+=tmp_s+me.push_c*(childlen-1);
            right.right_c+=me.push_c;
            right.push_s+=tmp_s;
            right.push_c+=me.push_c;
        }
        me.push_s=0;
        me.push_c=0;
    }
}
void increment(int i, int l, int r, i64 s, int c){
    node& me = stree[i];
    const int mylen = 1 << (__builtin_clz(i) - (31 - DEPTH));
    const int childlen = mylen >> 1;

    if(l==0&&r==mylen) {
        me.left_s += s;
        me.left_c += c;
        me.right_s += s + static_cast<i64>(c) * (mylen - 1);
        me.right_c += c;
        me.push_s += s;
        me.push_c += c;
        return;
    }
    pushdown(i);
    if (l<childlen) increment(i<<1, l, min(r,childlen), s, c);
    if (r>childlen) increment((i<<1)+1, max(l-childlen,0), r-childlen, s+static_cast<i64>(c)*max(childlen - l, 0), c);
    construct(i);
}
void overwrite(int i, int l, int r, i64 s, int c){
    node& me = stree[i];
    const int mylen = 1 << (__builtin_clz(i) - (31 - DEPTH));
    const int childlen = mylen >> 1;

    if(l==0&&r==mylen) {
        me.curr_len = mylen;
        me.left_s = s;
        me.left_c = c;
        me.left_len = mylen;
        me.right_s = s + static_cast<i64>(c) * (mylen - 1);
        me.right_c = c;
        me.right_len = mylen;
        me.push_s = s;
        me.push_c = c;
        me.is_override = true;
        return;
    }
    pushdown(i);
    if (l<childlen) overwrite(i<<1, l, min(r,childlen), s, c);
    if (r>childlen) overwrite((i<<1)+1, max(l-childlen,0), r-childlen, s+static_cast<i64>(c)*max(childlen - l, 0), c);
    construct(i);
}
int query(int i, int l, int r){
    const node& me = stree[i];
    const int mylen = 1 << (__builtin_clz(i) - (31 - DEPTH));
    const int childlen = mylen >> 1;

    if(l==0&&r==mylen) {
        return me.curr_len;
    }
    const node& left=stree[i<<1];
    const node& right=stree[(i<<1)+1];
    pushdown(i);
    int ans = 0;
    if (l<childlen) ans = max(ans, query(i<<1, l, min(r,childlen)));
    if (r>childlen) ans = max(ans, query((i<<1)+1, max(l-childlen,0), r-childlen));
    if (l<childlen && r>childlen) {
        ans = max(2, ans);
        int lmergelen =
            (right.left_s - left.right_s == left.right_c)
                ? ((right.left_c == left.right_c) ? min(r-childlen,right.left_len) : 1)
                : 0;
        int rmergelen =
            (right.left_s - left.right_s == right.left_c)
                ? ((right.left_c == left.right_c) ? min(childlen-l,left.right_len) : 1)
                : 0;
        ans = max(ans, max(min(childlen-l,left.right_len) + lmergelen, min(r-childlen,right.left_len) + rmergelen));
    }
    return ans;
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n,q;
    cin>>n>>q;
    for(int i=0;i<n;++i){
        int tmp;
        cin>>tmp;
        stree[(1 << DEPTH) + i].push_s = tmp;
    }
    for(int i=0;i<(1<<DEPTH);++i){
        stree[(1 << DEPTH) + i].is_override = true;
        stree[(1 << DEPTH) + i].curr_len = 1;
        stree[(1 << DEPTH) + i].left_s = stree[(1 << DEPTH) + i].push_s;
        stree[(1 << DEPTH) + i].left_len = 1;
        stree[(1 << DEPTH) + i].right_s = stree[(1 << DEPTH) + i].push_s;
        stree[(1 << DEPTH) + i].right_len = 1;
    }
    for(int i = (1<<DEPTH) - 1; i > 0; --i){
        construct(i);
        //cout<<"DBG "<<i<<endl;
        //cout<< stree[i].curr_len<<' '<<stree[i].left_s<<' '<<stree[i].left_c<<' '<<stree[i].left_len<<' '<<stree[i].right_s<<' '<<stree[i].right_c<<' '<<stree[i].right_len<<endl;
    }
    for(int i=0;i<q;++i){
        int type;
        cin>>type;
        if(type==1){
            int l,r,s,c;
            cin>>l>>r>>s>>c;
            --l;
            increment(1, l, r, s, c);
        }
        else if(type==2){
            int l,r,s,c;
            cin>>l>>r>>s>>c;
            --l;
            overwrite(1, l, r, s, c);
        }
        else if(type==3){
            int l,r;
            cin>>l>>r;
            --l;
            cout<<query(1, l,r)<<'\n';
        }
        /*for(int i = (1<<DEPTH) - 1; i > 0; --i){
            cout<<"DBG "<<i<<endl;
            cout<< stree[i].curr_len<<' '<<stree[i].left_s<<' '<<stree[i].left_c<<' '<<stree[i].left_len<<' '<<stree[i].right_s<<' '<<stree[i].right_c<<' '<<stree[i].right_len<<endl;
        }*/
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 66652 KB Output is correct
2 Correct 228 ms 66296 KB Output is correct
3 Correct 216 ms 66364 KB Output is correct
4 Correct 224 ms 66340 KB Output is correct
5 Correct 232 ms 66428 KB Output is correct
6 Correct 245 ms 66424 KB Output is correct
7 Correct 225 ms 66424 KB Output is correct
8 Correct 69 ms 66040 KB Output is correct
9 Correct 52 ms 66040 KB Output is correct
10 Correct 54 ms 66040 KB Output is correct
11 Correct 323 ms 66676 KB Output is correct
12 Correct 295 ms 66612 KB Output is correct
13 Correct 305 ms 66940 KB Output is correct
14 Correct 311 ms 66856 KB Output is correct
15 Correct 366 ms 66880 KB Output is correct
16 Correct 321 ms 66552 KB Output is correct
17 Correct 298 ms 66552 KB Output is correct
18 Correct 290 ms 66600 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 51 ms 66040 KB Output is correct
2 Correct 62 ms 66044 KB Output is correct
3 Correct 57 ms 66040 KB Output is correct
4 Correct 50 ms 66040 KB Output is correct
5 Correct 54 ms 66040 KB Output is correct
6 Correct 54 ms 66040 KB Output is correct
7 Correct 49 ms 66048 KB Output is correct
8 Correct 47 ms 66040 KB Output is correct
9 Correct 47 ms 66040 KB Output is correct
10 Correct 47 ms 66040 KB Output is correct
11 Correct 48 ms 66040 KB Output is correct
12 Correct 49 ms 65976 KB Output is correct
13 Correct 54 ms 66044 KB Output is correct
14 Correct 49 ms 66040 KB Output is correct
15 Correct 52 ms 66040 KB Output is correct
16 Correct 49 ms 66040 KB Output is correct
17 Correct 47 ms 66040 KB Output is correct
18 Correct 56 ms 66040 KB Output is correct
19 Correct 49 ms 66040 KB Output is correct
20 Correct 48 ms 66040 KB Output is correct
21 Correct 58 ms 66040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 332 ms 67064 KB Output is correct
2 Correct 187 ms 66628 KB Output is correct
3 Correct 179 ms 66552 KB Output is correct
4 Correct 185 ms 66688 KB Output is correct
5 Correct 210 ms 66716 KB Output is correct
6 Correct 202 ms 66748 KB Output is correct
7 Correct 187 ms 66684 KB Output is correct
8 Correct 45 ms 66040 KB Output is correct
9 Correct 46 ms 66040 KB Output is correct
10 Correct 46 ms 65964 KB Output is correct
11 Correct 389 ms 66808 KB Output is correct
12 Correct 327 ms 67192 KB Output is correct
13 Correct 363 ms 66860 KB Output is correct
14 Correct 320 ms 66808 KB Output is correct
15 Correct 289 ms 67068 KB Output is correct
16 Correct 334 ms 67576 KB Output is correct
17 Correct 332 ms 67576 KB Output is correct
18 Correct 324 ms 67448 KB Output is correct
19 Correct 327 ms 66940 KB Output is correct
20 Correct 337 ms 66668 KB Output is correct
21 Correct 331 ms 66760 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 477 ms 66424 KB Output is correct
2 Correct 255 ms 66168 KB Output is correct
3 Correct 227 ms 66296 KB Output is correct
4 Correct 247 ms 66300 KB Output is correct
5 Correct 230 ms 66168 KB Output is correct
6 Correct 237 ms 66296 KB Output is correct
7 Correct 235 ms 66168 KB Output is correct
8 Correct 47 ms 66040 KB Output is correct
9 Correct 46 ms 66040 KB Output is correct
10 Correct 46 ms 66044 KB Output is correct
11 Correct 476 ms 66424 KB Output is correct
12 Correct 459 ms 66296 KB Output is correct
13 Correct 484 ms 66296 KB Output is correct
14 Correct 476 ms 66320 KB Output is correct
15 Correct 454 ms 66296 KB Output is correct
16 Correct 474 ms 66264 KB Output is correct
17 Correct 491 ms 66296 KB Output is correct
18 Correct 544 ms 66296 KB Output is correct
19 Correct 560 ms 66268 KB Output is correct
20 Correct 739 ms 66236 KB Output is correct
21 Correct 558 ms 66168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 332 ms 67064 KB Output is correct
2 Correct 187 ms 66628 KB Output is correct
3 Correct 179 ms 66552 KB Output is correct
4 Correct 185 ms 66688 KB Output is correct
5 Correct 210 ms 66716 KB Output is correct
6 Correct 202 ms 66748 KB Output is correct
7 Correct 187 ms 66684 KB Output is correct
8 Correct 45 ms 66040 KB Output is correct
9 Correct 46 ms 66040 KB Output is correct
10 Correct 46 ms 65964 KB Output is correct
11 Correct 389 ms 66808 KB Output is correct
12 Correct 327 ms 67192 KB Output is correct
13 Correct 363 ms 66860 KB Output is correct
14 Correct 320 ms 66808 KB Output is correct
15 Correct 289 ms 67068 KB Output is correct
16 Correct 334 ms 67576 KB Output is correct
17 Correct 332 ms 67576 KB Output is correct
18 Correct 324 ms 67448 KB Output is correct
19 Correct 327 ms 66940 KB Output is correct
20 Correct 337 ms 66668 KB Output is correct
21 Correct 331 ms 66760 KB Output is correct
22 Correct 793 ms 66432 KB Output is correct
23 Correct 258 ms 66296 KB Output is correct
24 Correct 234 ms 66296 KB Output is correct
25 Correct 258 ms 66296 KB Output is correct
26 Correct 229 ms 66296 KB Output is correct
27 Correct 233 ms 66424 KB Output is correct
28 Correct 228 ms 66296 KB Output is correct
29 Correct 44 ms 66040 KB Output is correct
30 Correct 47 ms 66040 KB Output is correct
31 Correct 45 ms 66040 KB Output is correct
32 Correct 644 ms 66552 KB Output is correct
33 Correct 651 ms 66424 KB Output is correct
34 Correct 670 ms 66424 KB Output is correct
35 Correct 610 ms 66552 KB Output is correct
36 Correct 591 ms 66708 KB Output is correct
37 Correct 614 ms 66680 KB Output is correct
38 Correct 555 ms 66680 KB Output is correct
39 Correct 647 ms 66404 KB Output is correct
40 Correct 639 ms 66432 KB Output is correct
41 Correct 630 ms 66552 KB Output is correct
42 Correct 623 ms 66552 KB Output is correct
43 Correct 626 ms 66424 KB Output is correct
44 Correct 619 ms 66552 KB Output is correct
45 Correct 608 ms 66424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 277 ms 66652 KB Output is correct
2 Correct 228 ms 66296 KB Output is correct
3 Correct 216 ms 66364 KB Output is correct
4 Correct 224 ms 66340 KB Output is correct
5 Correct 232 ms 66428 KB Output is correct
6 Correct 245 ms 66424 KB Output is correct
7 Correct 225 ms 66424 KB Output is correct
8 Correct 69 ms 66040 KB Output is correct
9 Correct 52 ms 66040 KB Output is correct
10 Correct 54 ms 66040 KB Output is correct
11 Correct 323 ms 66676 KB Output is correct
12 Correct 295 ms 66612 KB Output is correct
13 Correct 305 ms 66940 KB Output is correct
14 Correct 311 ms 66856 KB Output is correct
15 Correct 366 ms 66880 KB Output is correct
16 Correct 321 ms 66552 KB Output is correct
17 Correct 298 ms 66552 KB Output is correct
18 Correct 290 ms 66600 KB Output is correct
19 Correct 51 ms 66040 KB Output is correct
20 Correct 62 ms 66044 KB Output is correct
21 Correct 57 ms 66040 KB Output is correct
22 Correct 50 ms 66040 KB Output is correct
23 Correct 54 ms 66040 KB Output is correct
24 Correct 54 ms 66040 KB Output is correct
25 Correct 49 ms 66048 KB Output is correct
26 Correct 47 ms 66040 KB Output is correct
27 Correct 47 ms 66040 KB Output is correct
28 Correct 47 ms 66040 KB Output is correct
29 Correct 48 ms 66040 KB Output is correct
30 Correct 49 ms 65976 KB Output is correct
31 Correct 54 ms 66044 KB Output is correct
32 Correct 49 ms 66040 KB Output is correct
33 Correct 52 ms 66040 KB Output is correct
34 Correct 49 ms 66040 KB Output is correct
35 Correct 47 ms 66040 KB Output is correct
36 Correct 56 ms 66040 KB Output is correct
37 Correct 49 ms 66040 KB Output is correct
38 Correct 48 ms 66040 KB Output is correct
39 Correct 58 ms 66040 KB Output is correct
40 Correct 332 ms 67064 KB Output is correct
41 Correct 187 ms 66628 KB Output is correct
42 Correct 179 ms 66552 KB Output is correct
43 Correct 185 ms 66688 KB Output is correct
44 Correct 210 ms 66716 KB Output is correct
45 Correct 202 ms 66748 KB Output is correct
46 Correct 187 ms 66684 KB Output is correct
47 Correct 45 ms 66040 KB Output is correct
48 Correct 46 ms 66040 KB Output is correct
49 Correct 46 ms 65964 KB Output is correct
50 Correct 389 ms 66808 KB Output is correct
51 Correct 327 ms 67192 KB Output is correct
52 Correct 363 ms 66860 KB Output is correct
53 Correct 320 ms 66808 KB Output is correct
54 Correct 289 ms 67068 KB Output is correct
55 Correct 334 ms 67576 KB Output is correct
56 Correct 332 ms 67576 KB Output is correct
57 Correct 324 ms 67448 KB Output is correct
58 Correct 327 ms 66940 KB Output is correct
59 Correct 337 ms 66668 KB Output is correct
60 Correct 331 ms 66760 KB Output is correct
61 Correct 477 ms 66424 KB Output is correct
62 Correct 255 ms 66168 KB Output is correct
63 Correct 227 ms 66296 KB Output is correct
64 Correct 247 ms 66300 KB Output is correct
65 Correct 230 ms 66168 KB Output is correct
66 Correct 237 ms 66296 KB Output is correct
67 Correct 235 ms 66168 KB Output is correct
68 Correct 47 ms 66040 KB Output is correct
69 Correct 46 ms 66040 KB Output is correct
70 Correct 46 ms 66044 KB Output is correct
71 Correct 476 ms 66424 KB Output is correct
72 Correct 459 ms 66296 KB Output is correct
73 Correct 484 ms 66296 KB Output is correct
74 Correct 476 ms 66320 KB Output is correct
75 Correct 454 ms 66296 KB Output is correct
76 Correct 474 ms 66264 KB Output is correct
77 Correct 491 ms 66296 KB Output is correct
78 Correct 544 ms 66296 KB Output is correct
79 Correct 560 ms 66268 KB Output is correct
80 Correct 739 ms 66236 KB Output is correct
81 Correct 558 ms 66168 KB Output is correct
82 Correct 793 ms 66432 KB Output is correct
83 Correct 258 ms 66296 KB Output is correct
84 Correct 234 ms 66296 KB Output is correct
85 Correct 258 ms 66296 KB Output is correct
86 Correct 229 ms 66296 KB Output is correct
87 Correct 233 ms 66424 KB Output is correct
88 Correct 228 ms 66296 KB Output is correct
89 Correct 44 ms 66040 KB Output is correct
90 Correct 47 ms 66040 KB Output is correct
91 Correct 45 ms 66040 KB Output is correct
92 Correct 644 ms 66552 KB Output is correct
93 Correct 651 ms 66424 KB Output is correct
94 Correct 670 ms 66424 KB Output is correct
95 Correct 610 ms 66552 KB Output is correct
96 Correct 591 ms 66708 KB Output is correct
97 Correct 614 ms 66680 KB Output is correct
98 Correct 555 ms 66680 KB Output is correct
99 Correct 647 ms 66404 KB Output is correct
100 Correct 639 ms 66432 KB Output is correct
101 Correct 630 ms 66552 KB Output is correct
102 Correct 623 ms 66552 KB Output is correct
103 Correct 626 ms 66424 KB Output is correct
104 Correct 619 ms 66552 KB Output is correct
105 Correct 608 ms 66424 KB Output is correct
106 Correct 689 ms 66760 KB Output is correct
107 Correct 246 ms 66360 KB Output is correct
108 Correct 264 ms 66168 KB Output is correct
109 Correct 249 ms 66296 KB Output is correct
110 Correct 50 ms 66040 KB Output is correct
111 Correct 62 ms 66040 KB Output is correct
112 Correct 46 ms 66040 KB Output is correct
113 Correct 706 ms 66460 KB Output is correct
114 Correct 683 ms 66552 KB Output is correct
115 Correct 638 ms 66424 KB Output is correct
116 Correct 589 ms 66456 KB Output is correct
117 Correct 665 ms 66552 KB Output is correct
118 Correct 670 ms 66392 KB Output is correct
119 Correct 602 ms 66552 KB Output is correct
120 Correct 334 ms 67556 KB Output is correct
121 Correct 351 ms 67576 KB Output is correct
122 Correct 382 ms 67576 KB Output is correct
123 Correct 314 ms 66680 KB Output is correct
124 Correct 360 ms 66808 KB Output is correct
125 Correct 349 ms 66680 KB Output is correct
126 Correct 667 ms 66620 KB Output is correct
127 Correct 650 ms 66692 KB Output is correct
128 Correct 725 ms 66716 KB Output is correct
129 Correct 619 ms 66680 KB Output is correct
130 Correct 609 ms 67320 KB Output is correct
131 Correct 562 ms 67448 KB Output is correct
132 Correct 559 ms 67320 KB Output is correct
133 Correct 677 ms 66680 KB Output is correct
134 Correct 740 ms 66692 KB Output is correct
135 Correct 707 ms 66552 KB Output is correct
136 Correct 260 ms 66296 KB Output is correct
137 Correct 261 ms 66168 KB Output is correct
138 Correct 249 ms 66168 KB Output is correct