답안 #832855

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
832855 2023-08-21T15:59:58 Z _martynas Progression (NOI20_progression) C++11
100 / 100
1394 ms 97596 KB
#include <iostream>
#include <vector>

using namespace std;

using ll = long long;

#warning change mxn
const int mxn = 3e5+5;

struct Node {
    int len = 0;
    ll dl = 0, dr = 0;
    ll diffl = 0, diffr = 0;
    int lenl = 0, lenr = 0, best = 1;
    bool set_tag = false;
    ll s = 0, c = 0;
};

Node merge(Node lhs, Node rhs) {
    Node res{};
    res.best = max(lhs.best, rhs.best);
    ll diff = rhs.dl-lhs.dr;
    res.best = max(res.best, (diff == lhs.diffr ? lhs.lenr : 1)+(diff == rhs.diffl ? rhs.lenl : 1));
    if(lhs.len == 1) lhs.diffl = diff, lhs.lenl = 1;
    if(rhs.len == 1) rhs.diffr = diff, rhs.lenr = 1;
    res.lenl = lhs.lenl + (diff == lhs.diffl && lhs.lenl == lhs.len ? (diff == rhs.diffl ? rhs.lenl : 1) : 0);
    res.lenr = rhs.lenr + (diff == rhs.diffr && rhs.lenr == rhs.len ? (diff == lhs.diffr ? lhs.lenr : 1) : 0);
    res.best = max(res.best, max(res.lenl, res.lenr));

    res.diffl = lhs.diffl, res.diffr = rhs.diffr;
    res.dl = lhs.dl, res.dr = rhs.dr;
    res.len = lhs.len+rhs.len;
    return res;
}

struct Seg {
    vector<Node> n;

    Seg() {
        n.resize(4*mxn);
    }

    void push(Node &chi, Node par) {
        if(par.set_tag) {
            chi.dl = par.s, chi.dr = par.s+(chi.len-1)*par.c;
            chi.diffl = par.c, chi.diffr = par.c;
            chi.lenl = chi.len, chi.lenr = chi.len;
            chi.best = chi.len;
            chi.set_tag = true;
            chi.s = par.s, chi.c = par.c;
        }
        else {
            chi.dl += par.s, chi.dr += par.s+(chi.len-1)*par.c;
            chi.diffl += par.c, chi.diffr += par.c;
            chi.s += par.s, chi.c += par.c;
        }
    }

    void push(int u) {
        Node par = n[u];
        push(n[2*u], par);
        par.s += n[2*u].len*par.c;
        push(n[2*u+1], par);
        n[u].set_tag = false;
        n[u].s = n[u].c = 0;
    }

    void pull(int u) {
        n[u] = merge(n[2*u], n[2*u+1]);
        // printf("merging %d, %d: %d\n", 2*u, 2*u+1, n[u].best);
        // printf("%d: len = %d, dl = %lld, dr = %lld, diffl = %lld, diffr = %lld,\nlenl = %d, lenr = %d, best = %d\n",
        //     2*u, n[2*u].len, n[2*u].dl, n[2*u].dr, n[2*u].diffl, n[2*u].diffr, n[2*u].lenl, n[2*u].lenr, n[2*u].best);
        // printf("%d: len = %d, dl = %lld, dr = %lld, diffl = %lld, diffr = %lld,\nlenl = %d, lenr = %d, best = %d\n",
        //     2*u+1, n[2*u+1].len, n[2*u+1].dl, n[2*u+1].dr, n[2*u+1].diffl, n[2*u+1].diffr, n[2*u+1].lenl, n[2*u+1].lenr, n[2*u+1].best);
        // printf("----------\n");
    }

    void add(int u, int l, int r, int ql, int qr, ll s, ll c) {
        if(r < ql || l > qr) return;
        // printf("add: u = %d, l, r = [%d; %d], ql, qr = [%d; %d], s = %lld, c = %lld,\n", u, l, r, ql, qr, s, c);
        if(ql <= l && r <= qr) {
            if(l == r) n[u].len = 1;
            n[u].s += s, n[u].c += c;
            n[u].dl += s, n[u].dr += s+(n[u].len-1)*c;
            n[u].diffl += c, n[u].diffr += c;
            return;
        }
        push(u);
        int m = (l+r)/2;
        add(2*u, l, m, ql, qr, s, c), add(2*u+1, m+1, r, max(m+1, ql), qr, s+max(0, m-ql+1)*c, c); 
        pull(u);
    }

    void set(int u, int l, int r, int ql, int qr, ll s, ll c) {
        if(r < ql || l > qr) return;
        // printf("set: u = %d, l, r = [%d; %d], ql, qr = [%d; %d], s = %lld, c = %lld,\n", u, l, r, ql, qr, s, c);
        if(ql <= l && r <= qr) {
            if(l == r) n[u].len = 1;
            n[u].set_tag = true;
            n[u].s = s, n[u].c = c;
            n[u].dl = s, n[u].dr = s+(n[u].len-1)*c;
            n[u].diffl = c, n[u].diffr = c;
            n[u].lenl = n[u].len, n[u].lenr = n[u].len;
            n[u].best = n[u].len;
            return;
        }
        push(u);
        int m = (l+r)/2;
        set(2*u, l, m, ql, qr, s, c), set(2*u+1, m+1, r, max(m+1, ql), qr, s+max(0, m-ql+1)*c, c); 
        pull(u);
        // printf("%d: len = %d, dl = %lld, dr = %lld, diffl = %lld, diffr = %lld,\nlenl = %d, lenr = %d, best = %d\n",
        //     u, n[u].len, n[u].dl, n[u].dr, n[u].diffl, n[u].diffr, n[u].lenl, n[u].lenr, n[u].best);
    }

    Node get(int u, int l, int r, int ql, int qr) {
        if(r < ql || l > qr) return {};
        // printf("get: u = %d, l, r = [%d; %d], ql, qr = [%d; %d]\n", u, l, r, ql, qr);
        if(ql <= l && r <= qr) return n[u];
        int m = (l+r)/2;
        push(u);
        Node lhs = get(2*u, l, m, ql, qr);
        Node rhs = get(2*u+1, m+1, r, ql, qr);
        pull(u);
        return (lhs.len == 0 ? rhs : (rhs.len == 0 ? lhs : merge(lhs, rhs)));
    }
} seg;

int n, q;
ll D[mxn];

int main() {
    cin >> n >> q;
    for(int i = 1; i <= n; i++) cin >> D[i];
    for(int i = 1; i <= n; i++) {
        seg.set(1, 1, n, i, i, D[i], 0);
    }
    for(int i = 0; i < q; i++) {
        ll t, l, r, s, c; cin >> t >> l >> r;
        if(t == 1) {
            cin >> s >> c;
            // for(int j = l; j <= r; j++) D[j] += s+(j-l)*c;
            seg.add(1, 1, n, l, r, s, c);
        }
        else if(t == 2) {
            cin >> s >> c;
            // for(int j = l; j <= r; j++) D[j] = s+(j-l)*c;
            seg.set(1, 1, n, l, r, s, c);
        }
        else {
            // int ans = 1, diff = D[l+1]-D[l], cnt = 1;
            // for(int j = l+1; j <= r; j++) {
            //     if(diff == D[j]-D[j-1]) {
            //         cnt++; ans = max(ans, cnt);
            //     }
            //     else {
            //         diff = D[j]-D[j-1], cnt = 2;
            //     }
            // }
            // cout << ans << "!\n";
            cout << seg.get(1, 1, n, l, r).best << "\n";
        }
        // cerr << "actual:\n";
        // for(int i = 1; i <= n; i++) {
        //     cerr << D[i] << " ";
        // }
        // cerr << "\n";
        // cerr << "got:\n";
        // for(int i = 1; i <= n; i++) {
        //     cerr << seg.get(1, 1, n, i, i).dl << " ";
        // }
        // cerr << "\n";
    }
    return 0;
}

Compilation message

Progression.cpp:8:2: warning: #warning change mxn [-Wcpp]
    8 | #warning change mxn
      |  ^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 752 ms 91472 KB Output is correct
2 Correct 391 ms 87992 KB Output is correct
3 Correct 393 ms 88048 KB Output is correct
4 Correct 307 ms 88040 KB Output is correct
5 Correct 311 ms 88068 KB Output is correct
6 Correct 311 ms 88096 KB Output is correct
7 Correct 308 ms 88044 KB Output is correct
8 Correct 31 ms 84816 KB Output is correct
9 Correct 32 ms 84824 KB Output is correct
10 Correct 34 ms 84840 KB Output is correct
11 Correct 710 ms 95824 KB Output is correct
12 Correct 645 ms 95664 KB Output is correct
13 Correct 697 ms 96044 KB Output is correct
14 Correct 646 ms 96156 KB Output is correct
15 Correct 644 ms 96064 KB Output is correct
16 Correct 633 ms 95660 KB Output is correct
17 Correct 687 ms 95660 KB Output is correct
18 Correct 636 ms 95688 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 84812 KB Output is correct
2 Correct 32 ms 84740 KB Output is correct
3 Correct 30 ms 84764 KB Output is correct
4 Correct 30 ms 84796 KB Output is correct
5 Correct 31 ms 84816 KB Output is correct
6 Correct 30 ms 84832 KB Output is correct
7 Correct 30 ms 84756 KB Output is correct
8 Correct 31 ms 84820 KB Output is correct
9 Correct 31 ms 84792 KB Output is correct
10 Correct 31 ms 84824 KB Output is correct
11 Correct 31 ms 84796 KB Output is correct
12 Correct 31 ms 84812 KB Output is correct
13 Correct 31 ms 84764 KB Output is correct
14 Correct 32 ms 84856 KB Output is correct
15 Correct 31 ms 84820 KB Output is correct
16 Correct 31 ms 84820 KB Output is correct
17 Correct 36 ms 84816 KB Output is correct
18 Correct 32 ms 84808 KB Output is correct
19 Correct 31 ms 84884 KB Output is correct
20 Correct 30 ms 84820 KB Output is correct
21 Correct 31 ms 84824 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1147 ms 94056 KB Output is correct
2 Correct 534 ms 87540 KB Output is correct
3 Correct 519 ms 87540 KB Output is correct
4 Correct 513 ms 87484 KB Output is correct
5 Correct 519 ms 87736 KB Output is correct
6 Correct 502 ms 87640 KB Output is correct
7 Correct 523 ms 87600 KB Output is correct
8 Correct 29 ms 84820 KB Output is correct
9 Correct 29 ms 84852 KB Output is correct
10 Correct 29 ms 84728 KB Output is correct
11 Correct 1122 ms 93016 KB Output is correct
12 Correct 1027 ms 94224 KB Output is correct
13 Correct 1084 ms 92852 KB Output is correct
14 Correct 1176 ms 92904 KB Output is correct
15 Correct 1038 ms 94220 KB Output is correct
16 Correct 1117 ms 94840 KB Output is correct
17 Correct 1115 ms 94744 KB Output is correct
18 Correct 1130 ms 94796 KB Output is correct
19 Correct 1085 ms 94216 KB Output is correct
20 Correct 1068 ms 94292 KB Output is correct
21 Correct 1061 ms 94148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1031 ms 96944 KB Output is correct
2 Correct 343 ms 88140 KB Output is correct
3 Correct 346 ms 88072 KB Output is correct
4 Correct 350 ms 88168 KB Output is correct
5 Correct 402 ms 88176 KB Output is correct
6 Correct 347 ms 88264 KB Output is correct
7 Correct 346 ms 88200 KB Output is correct
8 Correct 30 ms 84812 KB Output is correct
9 Correct 33 ms 84924 KB Output is correct
10 Correct 31 ms 84788 KB Output is correct
11 Correct 998 ms 93728 KB Output is correct
12 Correct 1072 ms 97056 KB Output is correct
13 Correct 979 ms 93736 KB Output is correct
14 Correct 1010 ms 93784 KB Output is correct
15 Correct 1010 ms 97000 KB Output is correct
16 Correct 1134 ms 97108 KB Output is correct
17 Correct 1056 ms 97116 KB Output is correct
18 Correct 1067 ms 97108 KB Output is correct
19 Correct 1126 ms 97128 KB Output is correct
20 Correct 1020 ms 97040 KB Output is correct
21 Correct 1058 ms 97000 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1147 ms 94056 KB Output is correct
2 Correct 534 ms 87540 KB Output is correct
3 Correct 519 ms 87540 KB Output is correct
4 Correct 513 ms 87484 KB Output is correct
5 Correct 519 ms 87736 KB Output is correct
6 Correct 502 ms 87640 KB Output is correct
7 Correct 523 ms 87600 KB Output is correct
8 Correct 29 ms 84820 KB Output is correct
9 Correct 29 ms 84852 KB Output is correct
10 Correct 29 ms 84728 KB Output is correct
11 Correct 1122 ms 93016 KB Output is correct
12 Correct 1027 ms 94224 KB Output is correct
13 Correct 1084 ms 92852 KB Output is correct
14 Correct 1176 ms 92904 KB Output is correct
15 Correct 1038 ms 94220 KB Output is correct
16 Correct 1117 ms 94840 KB Output is correct
17 Correct 1115 ms 94744 KB Output is correct
18 Correct 1130 ms 94796 KB Output is correct
19 Correct 1085 ms 94216 KB Output is correct
20 Correct 1068 ms 94292 KB Output is correct
21 Correct 1061 ms 94148 KB Output is correct
22 Correct 1202 ms 96524 KB Output is correct
23 Correct 403 ms 88120 KB Output is correct
24 Correct 398 ms 88032 KB Output is correct
25 Correct 398 ms 88064 KB Output is correct
26 Correct 397 ms 88048 KB Output is correct
27 Correct 415 ms 88048 KB Output is correct
28 Correct 394 ms 88096 KB Output is correct
29 Correct 31 ms 84960 KB Output is correct
30 Correct 29 ms 84764 KB Output is correct
31 Correct 30 ms 84780 KB Output is correct
32 Correct 1238 ms 93796 KB Output is correct
33 Correct 1192 ms 96472 KB Output is correct
34 Correct 1191 ms 93624 KB Output is correct
35 Correct 1162 ms 93716 KB Output is correct
36 Correct 1223 ms 93568 KB Output is correct
37 Correct 1236 ms 93604 KB Output is correct
38 Correct 1249 ms 93600 KB Output is correct
39 Correct 1176 ms 96508 KB Output is correct
40 Correct 1315 ms 96644 KB Output is correct
41 Correct 1271 ms 96744 KB Output is correct
42 Correct 1317 ms 96428 KB Output is correct
43 Correct 1190 ms 96372 KB Output is correct
44 Correct 1257 ms 96372 KB Output is correct
45 Correct 1296 ms 96552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 752 ms 91472 KB Output is correct
2 Correct 391 ms 87992 KB Output is correct
3 Correct 393 ms 88048 KB Output is correct
4 Correct 307 ms 88040 KB Output is correct
5 Correct 311 ms 88068 KB Output is correct
6 Correct 311 ms 88096 KB Output is correct
7 Correct 308 ms 88044 KB Output is correct
8 Correct 31 ms 84816 KB Output is correct
9 Correct 32 ms 84824 KB Output is correct
10 Correct 34 ms 84840 KB Output is correct
11 Correct 710 ms 95824 KB Output is correct
12 Correct 645 ms 95664 KB Output is correct
13 Correct 697 ms 96044 KB Output is correct
14 Correct 646 ms 96156 KB Output is correct
15 Correct 644 ms 96064 KB Output is correct
16 Correct 633 ms 95660 KB Output is correct
17 Correct 687 ms 95660 KB Output is correct
18 Correct 636 ms 95688 KB Output is correct
19 Correct 31 ms 84812 KB Output is correct
20 Correct 32 ms 84740 KB Output is correct
21 Correct 30 ms 84764 KB Output is correct
22 Correct 30 ms 84796 KB Output is correct
23 Correct 31 ms 84816 KB Output is correct
24 Correct 30 ms 84832 KB Output is correct
25 Correct 30 ms 84756 KB Output is correct
26 Correct 31 ms 84820 KB Output is correct
27 Correct 31 ms 84792 KB Output is correct
28 Correct 31 ms 84824 KB Output is correct
29 Correct 31 ms 84796 KB Output is correct
30 Correct 31 ms 84812 KB Output is correct
31 Correct 31 ms 84764 KB Output is correct
32 Correct 32 ms 84856 KB Output is correct
33 Correct 31 ms 84820 KB Output is correct
34 Correct 31 ms 84820 KB Output is correct
35 Correct 36 ms 84816 KB Output is correct
36 Correct 32 ms 84808 KB Output is correct
37 Correct 31 ms 84884 KB Output is correct
38 Correct 30 ms 84820 KB Output is correct
39 Correct 31 ms 84824 KB Output is correct
40 Correct 1147 ms 94056 KB Output is correct
41 Correct 534 ms 87540 KB Output is correct
42 Correct 519 ms 87540 KB Output is correct
43 Correct 513 ms 87484 KB Output is correct
44 Correct 519 ms 87736 KB Output is correct
45 Correct 502 ms 87640 KB Output is correct
46 Correct 523 ms 87600 KB Output is correct
47 Correct 29 ms 84820 KB Output is correct
48 Correct 29 ms 84852 KB Output is correct
49 Correct 29 ms 84728 KB Output is correct
50 Correct 1122 ms 93016 KB Output is correct
51 Correct 1027 ms 94224 KB Output is correct
52 Correct 1084 ms 92852 KB Output is correct
53 Correct 1176 ms 92904 KB Output is correct
54 Correct 1038 ms 94220 KB Output is correct
55 Correct 1117 ms 94840 KB Output is correct
56 Correct 1115 ms 94744 KB Output is correct
57 Correct 1130 ms 94796 KB Output is correct
58 Correct 1085 ms 94216 KB Output is correct
59 Correct 1068 ms 94292 KB Output is correct
60 Correct 1061 ms 94148 KB Output is correct
61 Correct 1031 ms 96944 KB Output is correct
62 Correct 343 ms 88140 KB Output is correct
63 Correct 346 ms 88072 KB Output is correct
64 Correct 350 ms 88168 KB Output is correct
65 Correct 402 ms 88176 KB Output is correct
66 Correct 347 ms 88264 KB Output is correct
67 Correct 346 ms 88200 KB Output is correct
68 Correct 30 ms 84812 KB Output is correct
69 Correct 33 ms 84924 KB Output is correct
70 Correct 31 ms 84788 KB Output is correct
71 Correct 998 ms 93728 KB Output is correct
72 Correct 1072 ms 97056 KB Output is correct
73 Correct 979 ms 93736 KB Output is correct
74 Correct 1010 ms 93784 KB Output is correct
75 Correct 1010 ms 97000 KB Output is correct
76 Correct 1134 ms 97108 KB Output is correct
77 Correct 1056 ms 97116 KB Output is correct
78 Correct 1067 ms 97108 KB Output is correct
79 Correct 1126 ms 97128 KB Output is correct
80 Correct 1020 ms 97040 KB Output is correct
81 Correct 1058 ms 97000 KB Output is correct
82 Correct 1202 ms 96524 KB Output is correct
83 Correct 403 ms 88120 KB Output is correct
84 Correct 398 ms 88032 KB Output is correct
85 Correct 398 ms 88064 KB Output is correct
86 Correct 397 ms 88048 KB Output is correct
87 Correct 415 ms 88048 KB Output is correct
88 Correct 394 ms 88096 KB Output is correct
89 Correct 31 ms 84960 KB Output is correct
90 Correct 29 ms 84764 KB Output is correct
91 Correct 30 ms 84780 KB Output is correct
92 Correct 1238 ms 93796 KB Output is correct
93 Correct 1192 ms 96472 KB Output is correct
94 Correct 1191 ms 93624 KB Output is correct
95 Correct 1162 ms 93716 KB Output is correct
96 Correct 1223 ms 93568 KB Output is correct
97 Correct 1236 ms 93604 KB Output is correct
98 Correct 1249 ms 93600 KB Output is correct
99 Correct 1176 ms 96508 KB Output is correct
100 Correct 1315 ms 96644 KB Output is correct
101 Correct 1271 ms 96744 KB Output is correct
102 Correct 1317 ms 96428 KB Output is correct
103 Correct 1190 ms 96372 KB Output is correct
104 Correct 1257 ms 96372 KB Output is correct
105 Correct 1296 ms 96552 KB Output is correct
106 Correct 1193 ms 97456 KB Output is correct
107 Correct 358 ms 88268 KB Output is correct
108 Correct 364 ms 88320 KB Output is correct
109 Correct 363 ms 88180 KB Output is correct
110 Correct 28 ms 84840 KB Output is correct
111 Correct 28 ms 84820 KB Output is correct
112 Correct 29 ms 84840 KB Output is correct
113 Correct 1234 ms 96628 KB Output is correct
114 Correct 1236 ms 96580 KB Output is correct
115 Correct 1265 ms 96444 KB Output is correct
116 Correct 1188 ms 96452 KB Output is correct
117 Correct 1228 ms 97448 KB Output is correct
118 Correct 1172 ms 96436 KB Output is correct
119 Correct 1156 ms 96476 KB Output is correct
120 Correct 1124 ms 95028 KB Output is correct
121 Correct 1134 ms 94892 KB Output is correct
122 Correct 1145 ms 95100 KB Output is correct
123 Correct 1094 ms 94132 KB Output is correct
124 Correct 1106 ms 94160 KB Output is correct
125 Correct 1103 ms 94216 KB Output is correct
126 Correct 1156 ms 94112 KB Output is correct
127 Correct 1165 ms 94144 KB Output is correct
128 Correct 1241 ms 97352 KB Output is correct
129 Correct 1151 ms 94040 KB Output is correct
130 Correct 1309 ms 94152 KB Output is correct
131 Correct 1394 ms 94084 KB Output is correct
132 Correct 1285 ms 94108 KB Output is correct
133 Correct 1246 ms 97596 KB Output is correct
134 Correct 1220 ms 97516 KB Output is correct
135 Correct 1204 ms 97560 KB Output is correct
136 Correct 371 ms 88200 KB Output is correct
137 Correct 364 ms 88316 KB Output is correct
138 Correct 372 ms 88188 KB Output is correct