Submission #255291

# Submission time Handle Problem Language Result Execution time Memory
255291 2020-07-31T17:09:01 Z model_code Progression (NOI20_progression) C++17
100 / 100
1459 ms 81684 KB
#include <iostream>
#include <algorithm>
using namespace std;

int N, Q;
long long D[300010], B[300010];

struct treedata {
    int len; long long val;
    treedata() {}
    treedata(int _len, long long _val): len(_len), val(_val) {}
    static bool cmp(treedata lhs, treedata rhs) {
        return lhs.len < rhs.len;
    }
};

struct qval {
    int len; long long sum;
    treedata prefix, suffix, subarray;
    qval() {}
    qval(long long val) {
        len = 1; sum = val;
        subarray = treedata(0, 0);
        prefix = suffix = treedata(1, val);
    }
    void add(long long val) {
        prefix.val += val;
        suffix.val += val;
        subarray.val += val;
        sum += val * len;
    }
    void set(long long val) {
        prefix.val = suffix.val = subarray.val = val;
        prefix.len = suffix.len = len;
        subarray.len = max(0, len - 2);
        sum = val * len;
    }
};

struct node {
    int S, E, M;
    qval V;
    bool lset;
    long long lazy_set, lazy_add;
    node *L, *R;
    node (int _S, int _E): S(_S), E(_E), M((S + E) / 2) {
        lset = false;
        lazy_set = lazy_add = 0;
        if (S == E) {
            V = qval(B[S]);
            return;
        }
        L = new node(S, M); R = new node(M + 1, E);
        V = combine(L->V, R->V);
    }
    qval combine(qval left, qval right) {
        qval ret;
        ret.len = left.len + right.len;
        ret.sum = left.sum + right.sum;

        ret.prefix = left.prefix;
        if (left.prefix.len == left.len && left.prefix.val == right.prefix.val)
            ret.prefix = treedata(left.len + right.prefix.len, left.prefix.val);

        ret.suffix = right.suffix;
        if (right.suffix.len == right.len && left.suffix.val == right.suffix.val)
            ret.suffix = treedata(right.len + left.suffix.len, right.suffix.val);

        ret.subarray = max(left.subarray, right.subarray, treedata::cmp);
        if (min(left.len - 1, left.suffix.len) > ret.subarray.len)
            ret.subarray = treedata(min(left.len - 1, left.suffix.len), left.suffix.val);
        if (min(right.len - 1, right.prefix.len) > ret.subarray.len)
            ret.subarray = treedata(min(right.len - 1, right.prefix.len), right.prefix.val);
        if (left.suffix.val == right.prefix.val && \
            min(left.len - 1, left.suffix.len) + \
            min(right.len - 1, right.prefix.len) > ret.subarray.len)
            ret.subarray = treedata(min(left.len - 1, left.suffix.len) + \
                min(right.len - 1, right.prefix.len), left.suffix.val);
        return ret;
    }
    void push() {
        if (lset) {
            L->rset(lazy_set);
            R->rset(lazy_set);
            lset = false;
            lazy_set = 0;
        }
        if (lazy_add) {
            L->radd(lazy_add);
            R->radd(lazy_add);
            lazy_add = 0;
        }
    }
    void radd(long long QV) {
        if (lset) V.set(lazy_set += QV);
        else {
            lazy_add += QV;
            V.add(QV);
        }
    }
    void rset(long long QV) {
        lset = true;
        lazy_add = 0;
        V.set(lazy_set = QV);
    }
    qval query(int QS, int QE) {
        if (QS <= S && E <= QE) return V;
        push();
        if (QE <= M) return L->query(QS, QE);
        if (QS > M) return R->query(QS, QE);
        return combine(L->query(QS, M), R->query(M + 1, QE));
    }
    void add(int QS, int QE, long long QV) {
        if (QS <= S && E <= QE) return radd(QV);
        push();
        if (QE <= M) L->add(QS, QE, QV);
        else if (QS > M) R->add(QS, QE, QV);
        else {
            L->add(QS, M, QV);
            R->add(M + 1, QE, QV);
        }
        V = combine(L->V, R->V);
    }
    void set(int QS, int QE, long long QV) {
        if (QS <= S && E <= QE) return rset(QV);
        push();
        if (QE <= M) L->set(QS, QE, QV);
        else if (QS > M) R->set(QS, QE, QV);
        else {
            L->set(QS, M, QV);
            R->set(M + 1, QE, QV);
        }
        V = combine(L->V, R->V);
    }
} *root;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> Q;
    for (int a = 1; a <= N; ++a) {
        cin >> D[a];
        B[a] = D[a] - D[a - 1];
    }
    root = new node(1, N);
    while (Q--) {
        int op;
        cin >> op;
        if (op == 1) {
            int L, R; long long S, C;
            cin >> L >> R >> S >> C;
            root->add(L, L, S);
            if (L != R) root->add(L + 1, R, C);
            if (R != N) root->add(R + 1, R + 1, -(S + (R - L) * C));
        } else if (op == 2) {
            int L, R; long long S, C;
            cin >> L >> R >> S >> C;
            long long old_sum = root->query(L, R).sum;
            long long prefix = (L == 1 ? 0 : root->query(1, L - 1).sum);
            root->set(L, L, S - prefix);
            if (L != R) root->set(L + 1, R, C);
            if (R != N) {
                long long new_sum = (S - prefix) + (R - L) * C;
                root->add(R + 1, R + 1, old_sum - new_sum);
            }
        } else {
            int L, R;
            cin >> L >> R;
            qval ret = root->query(L, R);
            int len = max({ret.prefix.len, ret.subarray.len + 1, ret.suffix.len + 1});
            len = min(len, ret.len);
            cout << len << '\n';
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 322 ms 80876 KB Output is correct
2 Correct 126 ms 632 KB Output is correct
3 Correct 125 ms 632 KB Output is correct
4 Correct 123 ms 760 KB Output is correct
5 Correct 127 ms 760 KB Output is correct
6 Correct 122 ms 760 KB Output is correct
7 Correct 127 ms 676 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 325 ms 80896 KB Output is correct
12 Correct 325 ms 80760 KB Output is correct
13 Correct 330 ms 81016 KB Output is correct
14 Correct 328 ms 81016 KB Output is correct
15 Correct 323 ms 81016 KB Output is correct
16 Correct 328 ms 80760 KB Output is correct
17 Correct 323 ms 80760 KB Output is correct
18 Correct 326 ms 80760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 640 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 640 KB Output is correct
9 Correct 2 ms 640 KB Output is correct
10 Correct 2 ms 640 KB Output is correct
11 Correct 2 ms 640 KB Output is correct
12 Correct 2 ms 640 KB Output is correct
13 Correct 2 ms 640 KB Output is correct
14 Correct 2 ms 640 KB Output is correct
15 Correct 2 ms 640 KB Output is correct
16 Correct 2 ms 640 KB Output is correct
17 Correct 2 ms 640 KB Output is correct
18 Correct 2 ms 640 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 388 ms 81272 KB Output is correct
2 Correct 83 ms 1016 KB Output is correct
3 Correct 83 ms 1016 KB Output is correct
4 Correct 85 ms 1016 KB Output is correct
5 Correct 92 ms 1016 KB Output is correct
6 Correct 91 ms 1016 KB Output is correct
7 Correct 86 ms 1016 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 394 ms 81136 KB Output is correct
12 Correct 345 ms 81272 KB Output is correct
13 Correct 400 ms 81016 KB Output is correct
14 Correct 422 ms 81144 KB Output is correct
15 Correct 381 ms 81300 KB Output is correct
16 Correct 452 ms 81528 KB Output is correct
17 Correct 417 ms 81608 KB Output is correct
18 Correct 473 ms 81684 KB Output is correct
19 Correct 394 ms 80872 KB Output is correct
20 Correct 386 ms 80888 KB Output is correct
21 Correct 353 ms 80888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 740 ms 80504 KB Output is correct
2 Correct 142 ms 504 KB Output is correct
3 Correct 142 ms 504 KB Output is correct
4 Correct 140 ms 504 KB Output is correct
5 Correct 147 ms 632 KB Output is correct
6 Correct 147 ms 504 KB Output is correct
7 Correct 148 ms 568 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 800 ms 80604 KB Output is correct
12 Correct 759 ms 80560 KB Output is correct
13 Correct 737 ms 80632 KB Output is correct
14 Correct 999 ms 80468 KB Output is correct
15 Correct 1113 ms 80384 KB Output is correct
16 Correct 827 ms 80564 KB Output is correct
17 Correct 942 ms 80588 KB Output is correct
18 Correct 821 ms 80492 KB Output is correct
19 Correct 865 ms 80588 KB Output is correct
20 Correct 820 ms 80388 KB Output is correct
21 Correct 934 ms 80432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 388 ms 81272 KB Output is correct
2 Correct 83 ms 1016 KB Output is correct
3 Correct 83 ms 1016 KB Output is correct
4 Correct 85 ms 1016 KB Output is correct
5 Correct 92 ms 1016 KB Output is correct
6 Correct 91 ms 1016 KB Output is correct
7 Correct 86 ms 1016 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 394 ms 81136 KB Output is correct
12 Correct 345 ms 81272 KB Output is correct
13 Correct 400 ms 81016 KB Output is correct
14 Correct 422 ms 81144 KB Output is correct
15 Correct 381 ms 81300 KB Output is correct
16 Correct 452 ms 81528 KB Output is correct
17 Correct 417 ms 81608 KB Output is correct
18 Correct 473 ms 81684 KB Output is correct
19 Correct 394 ms 80872 KB Output is correct
20 Correct 386 ms 80888 KB Output is correct
21 Correct 353 ms 80888 KB Output is correct
22 Correct 1051 ms 80564 KB Output is correct
23 Correct 144 ms 632 KB Output is correct
24 Correct 156 ms 624 KB Output is correct
25 Correct 158 ms 716 KB Output is correct
26 Correct 160 ms 712 KB Output is correct
27 Correct 156 ms 688 KB Output is correct
28 Correct 153 ms 692 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1030 ms 80664 KB Output is correct
33 Correct 1216 ms 80508 KB Output is correct
34 Correct 1160 ms 80608 KB Output is correct
35 Correct 1016 ms 80548 KB Output is correct
36 Correct 824 ms 80872 KB Output is correct
37 Correct 810 ms 80812 KB Output is correct
38 Correct 888 ms 80888 KB Output is correct
39 Correct 1121 ms 80628 KB Output is correct
40 Correct 1074 ms 80696 KB Output is correct
41 Correct 1118 ms 80776 KB Output is correct
42 Correct 1327 ms 80632 KB Output is correct
43 Correct 1087 ms 80648 KB Output is correct
44 Correct 1167 ms 80520 KB Output is correct
45 Correct 1430 ms 80584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 322 ms 80876 KB Output is correct
2 Correct 126 ms 632 KB Output is correct
3 Correct 125 ms 632 KB Output is correct
4 Correct 123 ms 760 KB Output is correct
5 Correct 127 ms 760 KB Output is correct
6 Correct 122 ms 760 KB Output is correct
7 Correct 127 ms 676 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 325 ms 80896 KB Output is correct
12 Correct 325 ms 80760 KB Output is correct
13 Correct 330 ms 81016 KB Output is correct
14 Correct 328 ms 81016 KB Output is correct
15 Correct 323 ms 81016 KB Output is correct
16 Correct 328 ms 80760 KB Output is correct
17 Correct 323 ms 80760 KB Output is correct
18 Correct 326 ms 80760 KB Output is correct
19 Correct 2 ms 640 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 2 ms 384 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 640 KB Output is correct
27 Correct 2 ms 640 KB Output is correct
28 Correct 2 ms 640 KB Output is correct
29 Correct 2 ms 640 KB Output is correct
30 Correct 2 ms 640 KB Output is correct
31 Correct 2 ms 640 KB Output is correct
32 Correct 2 ms 640 KB Output is correct
33 Correct 2 ms 640 KB Output is correct
34 Correct 2 ms 640 KB Output is correct
35 Correct 2 ms 640 KB Output is correct
36 Correct 2 ms 640 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 388 ms 81272 KB Output is correct
41 Correct 83 ms 1016 KB Output is correct
42 Correct 83 ms 1016 KB Output is correct
43 Correct 85 ms 1016 KB Output is correct
44 Correct 92 ms 1016 KB Output is correct
45 Correct 91 ms 1016 KB Output is correct
46 Correct 86 ms 1016 KB Output is correct
47 Correct 1 ms 384 KB Output is correct
48 Correct 0 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 394 ms 81136 KB Output is correct
51 Correct 345 ms 81272 KB Output is correct
52 Correct 400 ms 81016 KB Output is correct
53 Correct 422 ms 81144 KB Output is correct
54 Correct 381 ms 81300 KB Output is correct
55 Correct 452 ms 81528 KB Output is correct
56 Correct 417 ms 81608 KB Output is correct
57 Correct 473 ms 81684 KB Output is correct
58 Correct 394 ms 80872 KB Output is correct
59 Correct 386 ms 80888 KB Output is correct
60 Correct 353 ms 80888 KB Output is correct
61 Correct 740 ms 80504 KB Output is correct
62 Correct 142 ms 504 KB Output is correct
63 Correct 142 ms 504 KB Output is correct
64 Correct 140 ms 504 KB Output is correct
65 Correct 147 ms 632 KB Output is correct
66 Correct 147 ms 504 KB Output is correct
67 Correct 148 ms 568 KB Output is correct
68 Correct 1 ms 384 KB Output is correct
69 Correct 1 ms 384 KB Output is correct
70 Correct 1 ms 384 KB Output is correct
71 Correct 800 ms 80604 KB Output is correct
72 Correct 759 ms 80560 KB Output is correct
73 Correct 737 ms 80632 KB Output is correct
74 Correct 999 ms 80468 KB Output is correct
75 Correct 1113 ms 80384 KB Output is correct
76 Correct 827 ms 80564 KB Output is correct
77 Correct 942 ms 80588 KB Output is correct
78 Correct 821 ms 80492 KB Output is correct
79 Correct 865 ms 80588 KB Output is correct
80 Correct 820 ms 80388 KB Output is correct
81 Correct 934 ms 80432 KB Output is correct
82 Correct 1051 ms 80564 KB Output is correct
83 Correct 144 ms 632 KB Output is correct
84 Correct 156 ms 624 KB Output is correct
85 Correct 158 ms 716 KB Output is correct
86 Correct 160 ms 712 KB Output is correct
87 Correct 156 ms 688 KB Output is correct
88 Correct 153 ms 692 KB Output is correct
89 Correct 1 ms 384 KB Output is correct
90 Correct 1 ms 384 KB Output is correct
91 Correct 1 ms 384 KB Output is correct
92 Correct 1030 ms 80664 KB Output is correct
93 Correct 1216 ms 80508 KB Output is correct
94 Correct 1160 ms 80608 KB Output is correct
95 Correct 1016 ms 80548 KB Output is correct
96 Correct 824 ms 80872 KB Output is correct
97 Correct 810 ms 80812 KB Output is correct
98 Correct 888 ms 80888 KB Output is correct
99 Correct 1121 ms 80628 KB Output is correct
100 Correct 1074 ms 80696 KB Output is correct
101 Correct 1118 ms 80776 KB Output is correct
102 Correct 1327 ms 80632 KB Output is correct
103 Correct 1087 ms 80648 KB Output is correct
104 Correct 1167 ms 80520 KB Output is correct
105 Correct 1430 ms 80584 KB Output is correct
106 Correct 1459 ms 80804 KB Output is correct
107 Correct 225 ms 504 KB Output is correct
108 Correct 221 ms 632 KB Output is correct
109 Correct 215 ms 588 KB Output is correct
110 Correct 2 ms 384 KB Output is correct
111 Correct 1 ms 384 KB Output is correct
112 Correct 1 ms 384 KB Output is correct
113 Correct 1085 ms 80768 KB Output is correct
114 Correct 1116 ms 80632 KB Output is correct
115 Correct 1078 ms 80660 KB Output is correct
116 Correct 952 ms 80760 KB Output is correct
117 Correct 1211 ms 80956 KB Output is correct
118 Correct 882 ms 80636 KB Output is correct
119 Correct 891 ms 80748 KB Output is correct
120 Correct 430 ms 81656 KB Output is correct
121 Correct 490 ms 81656 KB Output is correct
122 Correct 412 ms 81656 KB Output is correct
123 Correct 359 ms 81044 KB Output is correct
124 Correct 361 ms 81016 KB Output is correct
125 Correct 397 ms 80888 KB Output is correct
126 Correct 1136 ms 81020 KB Output is correct
127 Correct 1156 ms 80936 KB Output is correct
128 Correct 1170 ms 80940 KB Output is correct
129 Correct 1160 ms 80716 KB Output is correct
130 Correct 934 ms 81532 KB Output is correct
131 Correct 889 ms 81528 KB Output is correct
132 Correct 830 ms 81512 KB Output is correct
133 Correct 1242 ms 81088 KB Output is correct
134 Correct 1168 ms 81016 KB Output is correct
135 Correct 1238 ms 80924 KB Output is correct
136 Correct 211 ms 504 KB Output is correct
137 Correct 211 ms 632 KB Output is correct
138 Correct 208 ms 632 KB Output is correct