Submission #673312

# Submission time Handle Problem Language Result Execution time Memory
673312 2022-12-20T07:14:58 Z Cyanmond Progression (NOI20_progression) C++17
100 / 100
2089 ms 77104 KB
#include <bits/stdc++.h>

using i64 = long long;
constexpr i64 inf = 1ll << 60;

template <class M>
class segtree {
    int n, seg_size;
    using T = typename M::T;
    std::vector<T> tree;

    void update(int i) {
        tree[i] = M::operate(tree[2 * i], tree[2 * i + 1]);
    }

  public:
    segtree(int n_) : n(n_) {
        seg_size = 1;
        while (seg_size < n) {
            seg_size *= 2;
        }
        tree.assign(2 * seg_size, M::identity());
    }

    void set(int i, T v) {
        i += seg_size;
        tree[i] = v;
        while (i != 1) {
            i /= 2;
            update(i);
        }
    }

    T fold(int l, int r) {
        T ret_l = M::identity(), ret_r = M::identity();
        for (l += seg_size, r += seg_size; l < r; l /= 2, r /= 2) {
            if (l % 2 == 1) {
                ret_l = M::operate(ret_l, tree[l++]);
            }
            if (r % 2 == 1) {
                ret_r = M::operate(tree[--r], ret_r);
            }
        }
        return M::operate(ret_l, ret_r);
    }

    T get(int i) {
        return tree[i + seg_size];
    }
};

template <class M>
class lazy_segtree {
    int n, seg_size, logn;
    using T = typename M::T;
    using E = typename M::E;
    std::vector<T> tree;
    std::vector<E> lazy;

    void update(int i) {
        tree[i] = M::operate(tree[2 * i], tree[2 * i + 1]);
    }

    void apply(int i, const E &v) {
        tree[i] = M::map(tree[i], v);
        if (i < seg_size) {
            lazy[i] = M::composite(lazy[i], v);
        }
    }

    void flush(int i) {
        apply(2 * i, lazy[i]);
        apply(2 * i + 1, lazy[i]);
        lazy[i] = M::e_identity();
    }

  public:
    lazy_segtree(int n_) : n(n_) {
        logn = 1;
        while ((1 << logn) < n) {
            ++logn;
        }
        seg_size = 1 << logn;
        tree.assign(2 * seg_size, M::t_identity());
        lazy.assign(seg_size, M::e_identity());
    }

    void assign(int i, const T &v) {
        i += seg_size;
        for (int d = logn; d >= 1; --d) {
            flush(i >> d);
        }
        tree[i] = v;
        for (int d = 1; d <= logn; ++d) {
            update(i >> d);
        }
    }

    void operate_range(int l, int r, const E &v) {
        l += seg_size;
        r += seg_size;
        for (int d = logn; d >= 1; --d) {
            if (((l >> d) << d) != l) {
                flush(l >> d);
            }
            if (((r >> d) << d) != r) {
                flush((r - 1) >> d);
            }
        }

        for (int l2 = l, r2 = r; l2 < r2; l2 /= 2, r2 /= 2) {
            if (l2 % 2 == 1) {
                apply(l2++, v);
            }
            if (r2 % 2 == 1) {
                apply(--r2, v);
            }
        }

        for (int d = 1; d <= logn; ++d) {
            if (((l >> d) << d) != l) {
                update(l >> d);
            }
            if (((r >> d) << d) != r) {
                update((r - 1) >> d);
            }
        }
    }

    void operate_point(int i, const E &v) {
        operate_range(i, i + 1, v);
    }

    T fold(int l, int r) {
        l += seg_size;
        r += seg_size;
        for (int d = logn; d >= 1; --d) {
            if (((l >> d) << d) != l) {
                flush(l >> d);
            }
            if (((r >> d) << d) != r) {
                flush((r - 1) >> d);
            }
        }

        T ret_l = M::t_identity(), ret_r = M::t_identity();
        while (l < r) {
            if (l % 2 == 1) {
                ret_l = M::operate(ret_l, tree[l++]);
            }
            if (r % 2 == 1) {
                ret_r = M::operate(tree[--r], ret_r);
            }
            l /= 2;
            r /= 2;
        }
        return M::operate(ret_l, ret_r);
    }
};

struct M1 {
    // range sum
    struct T {
        i64 value;
        int width;
    };

    static T operate(T a, T b) {
        return {a.value + b.value, a.width + b.width};
    }

    static T t_identity() {
        return {0, 0};
    }

    struct E {
        i64 init;
        i64 add;
    };

    static T map(T a, E b) {
        if (b.init == inf) {
            return {a.value + a.width * b.add, a.width};
        } else {
            return {a.width * (b.init + b.add), a.width};
        }
    }

    static E composite(E a, E b) {
        if (b.init == inf) {
            return {a.init, a.add + b.add};
        } else {
            return b;
        }
    }

    static E e_identity() {
        return {inf, 0};
    }
};

struct M2 {
    // on off max
    struct T {
        int l;
        int r;
        int ma;
        int width;
    };

    static T operate(T l, T r) {
        if (l.width == -1) {
            return r;
        }
        if (r.width == -1) {
            return l;
        }
        T ret;
        ret.width = l.width + r.width;
        ret.l = l.l;
        ret.r = r.r;
        if (l.l == l.width) {
            ret.l = l.l + r.l;
        }
        if (r.r == r.width) {
            ret.r = l.r + r.r;
        }
        ret.ma = std::max({ret.l, ret.r, l.ma, r.ma, l.r + r.l});
        return ret;
    }

    static T identity() {
        return {0, 0, 0, -1};
    }
};

int main() {
    int N, Q;
    std::cin >> N >> Q;
    std::vector<i64> D(N);
    for (auto &e : D) {
        std::cin >> e;
    }
    std::vector<int> T(Q), L(Q), R(Q);
    std::vector<i64> S(Q), C(Q);
    for (int i = 0; i < Q; ++i) {
        std::cin >> T[i] >> L[i] >> R[i];
        --L[i];
        if (T[i] == 1 or T[i] == 2) {
            std::cin >> S[i] >> C[i];
        }
    }

    // diff of diff
    std::vector<i64> diffs(N - 1);
    for (int i = 1; i < N; ++i) {
        diffs[i - 1] = D[i] - D[i - 1];
    }
    diffs.insert(diffs.begin(), D[0]);

    lazy_segtree<M1> seg(N);
    for (int i = 0; i < N; ++i) {
        seg.assign(i, {diffs[i], 1});
    }

    auto access = [&](const int i) {
        return seg.fold(0, i + 1).value;
    };

    segtree<M2> oz(N - 2);
    std::set<int> zeros;
    for (int i = 2; i < N; ++i) {
        if (D[i] - D[i - 1] == D[i - 1] - D[i - 2]) {
            oz.set(i - 2, {1, 1, 1, 1});
        } else {
            oz.set(i - 2, {0, 0, 0, 1});
            zeros.insert(i - 2);
        }
    }

    auto update = [&](int i) {
        if (i >= N - 2 or i < 0) {
            return;
        }
        const auto a = access(i), b = access(i + 1), c = access(i + 2);
        if (b - a == c - b) {
            if (oz.get(i).ma == 1) {
                return;
            }
            oz.set(i, {1, 1, 1, 1});
            zeros.erase(i);
        } else {
            if (oz.get(i).ma == 0) {
                return;
            }
            oz.set(i, {0, 0, 0, 1});
            zeros.insert(i);
        }
    };

    auto range_set_one = [&](int l, int r) {
        // [l, r - 2)
        auto itr = zeros.lower_bound(l);
        while (itr != zeros.end()) {
            if (*itr >= r - 2) {
                break;
            }
            oz.set(*itr, {1, 1, 1, 1});
            itr = zeros.erase(itr);
        }  
    };

    for (int q = 0; q < Q; ++q) {
        if (T[q] == 1) {
            seg.operate_point(L[q], {inf, S[q]});
            seg.operate_range(L[q] + 1, R[q], {inf, C[q]});
            if (R[q] != N) {
                seg.operate_point(R[q], {inf, -(S[q] + (R[q] - L[q] - 1) * C[q])});
            }
            update(L[q] - 2);
            update(L[q] - 1);
            update(R[q] - 2);
            update(R[q] - 1);
        }
        if (T[q] == 2) {
            const auto lt = R[q] == N ? 0 : access(R[q]);
            const auto fa = access(L[q] - 1);
            seg.operate_point(L[q], {S[q] - fa, 0});
            seg.operate_range(L[q] + 1, R[q], {C[q], 0});
            if (R[q] != N) {
                seg.operate_point(R[q], {lt - (S[q] + (R[q] - L[q] - 1) * C[q]), 0});
            }
            update(L[q] - 2);
            update(L[q] - 1);
            update(R[q] - 2);
            update(R[q] - 1);
            range_set_one(L[q], R[q]);
        }

        if (T[q] == 3) {
            if (R[q] - L[q] <= 2) {
                std::cout << R[q] - L[q] << std::endl;
                continue;
            }
            const auto res = oz.fold(L[q], R[q] - 2).ma;
            std::cout << res + 2 << std::endl;
        }        
    }
}
# Verdict Execution time Memory Grader output
1 Correct 782 ms 55304 KB Output is correct
2 Correct 379 ms 8776 KB Output is correct
3 Correct 375 ms 8788 KB Output is correct
4 Correct 398 ms 8760 KB Output is correct
5 Correct 367 ms 8856 KB Output is correct
6 Correct 390 ms 8892 KB Output is correct
7 Correct 375 ms 8800 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 762 ms 54984 KB Output is correct
12 Correct 811 ms 58072 KB Output is correct
13 Correct 790 ms 55052 KB Output is correct
14 Correct 768 ms 55044 KB Output is correct
15 Correct 762 ms 55192 KB Output is correct
16 Correct 786 ms 59900 KB Output is correct
17 Correct 810 ms 59772 KB Output is correct
18 Correct 800 ms 59596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 2 ms 308 KB Output is correct
3 Correct 3 ms 308 KB Output is correct
4 Correct 2 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 3 ms 340 KB Output is correct
7 Correct 2 ms 340 KB Output is correct
8 Correct 4 ms 340 KB Output is correct
9 Correct 5 ms 340 KB Output is correct
10 Correct 4 ms 340 KB Output is correct
11 Correct 3 ms 340 KB Output is correct
12 Correct 5 ms 360 KB Output is correct
13 Correct 4 ms 440 KB Output is correct
14 Correct 4 ms 340 KB Output is correct
15 Correct 7 ms 356 KB Output is correct
16 Correct 5 ms 340 KB Output is correct
17 Correct 5 ms 340 KB Output is correct
18 Correct 5 ms 436 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 3 ms 308 KB Output is correct
21 Correct 2 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 757 ms 55432 KB Output is correct
2 Correct 514 ms 9112 KB Output is correct
3 Correct 478 ms 9084 KB Output is correct
4 Correct 493 ms 9088 KB Output is correct
5 Correct 480 ms 9088 KB Output is correct
6 Correct 475 ms 9164 KB Output is correct
7 Correct 496 ms 9184 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 802 ms 57868 KB Output is correct
12 Correct 766 ms 55584 KB Output is correct
13 Correct 748 ms 57644 KB Output is correct
14 Correct 764 ms 57784 KB Output is correct
15 Correct 777 ms 55420 KB Output is correct
16 Correct 784 ms 55540 KB Output is correct
17 Correct 827 ms 55644 KB Output is correct
18 Correct 800 ms 55740 KB Output is correct
19 Correct 770 ms 59728 KB Output is correct
20 Correct 810 ms 59812 KB Output is correct
21 Correct 830 ms 59976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1831 ms 66872 KB Output is correct
2 Correct 513 ms 11832 KB Output is correct
3 Correct 491 ms 11900 KB Output is correct
4 Correct 485 ms 11824 KB Output is correct
5 Correct 496 ms 12112 KB Output is correct
6 Correct 522 ms 12120 KB Output is correct
7 Correct 530 ms 11828 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 312 KB Output is correct
10 Correct 2 ms 340 KB Output is correct
11 Correct 1701 ms 72944 KB Output is correct
12 Correct 1761 ms 76424 KB Output is correct
13 Correct 1715 ms 73056 KB Output is correct
14 Correct 1738 ms 73020 KB Output is correct
15 Correct 1778 ms 76844 KB Output is correct
16 Correct 1797 ms 76496 KB Output is correct
17 Correct 1765 ms 76416 KB Output is correct
18 Correct 1780 ms 76492 KB Output is correct
19 Correct 1767 ms 76920 KB Output is correct
20 Correct 1728 ms 77104 KB Output is correct
21 Correct 1763 ms 77028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 757 ms 55432 KB Output is correct
2 Correct 514 ms 9112 KB Output is correct
3 Correct 478 ms 9084 KB Output is correct
4 Correct 493 ms 9088 KB Output is correct
5 Correct 480 ms 9088 KB Output is correct
6 Correct 475 ms 9164 KB Output is correct
7 Correct 496 ms 9184 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 802 ms 57868 KB Output is correct
12 Correct 766 ms 55584 KB Output is correct
13 Correct 748 ms 57644 KB Output is correct
14 Correct 764 ms 57784 KB Output is correct
15 Correct 777 ms 55420 KB Output is correct
16 Correct 784 ms 55540 KB Output is correct
17 Correct 827 ms 55644 KB Output is correct
18 Correct 800 ms 55740 KB Output is correct
19 Correct 770 ms 59728 KB Output is correct
20 Correct 810 ms 59812 KB Output is correct
21 Correct 830 ms 59976 KB Output is correct
22 Correct 1625 ms 66132 KB Output is correct
23 Correct 497 ms 8908 KB Output is correct
24 Correct 476 ms 8760 KB Output is correct
25 Correct 538 ms 8764 KB Output is correct
26 Correct 478 ms 8772 KB Output is correct
27 Correct 491 ms 8724 KB Output is correct
28 Correct 486 ms 8764 KB Output is correct
29 Correct 2 ms 212 KB Output is correct
30 Correct 2 ms 212 KB Output is correct
31 Correct 2 ms 212 KB Output is correct
32 Correct 1532 ms 66484 KB Output is correct
33 Correct 1588 ms 66208 KB Output is correct
34 Correct 1530 ms 66492 KB Output is correct
35 Correct 1525 ms 66500 KB Output is correct
36 Correct 1228 ms 64236 KB Output is correct
37 Correct 1223 ms 64224 KB Output is correct
38 Correct 1203 ms 64320 KB Output is correct
39 Correct 1611 ms 66244 KB Output is correct
40 Correct 1641 ms 66192 KB Output is correct
41 Correct 1608 ms 66232 KB Output is correct
42 Correct 1660 ms 66240 KB Output is correct
43 Correct 1578 ms 67032 KB Output is correct
44 Correct 1581 ms 67116 KB Output is correct
45 Correct 1591 ms 67044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 782 ms 55304 KB Output is correct
2 Correct 379 ms 8776 KB Output is correct
3 Correct 375 ms 8788 KB Output is correct
4 Correct 398 ms 8760 KB Output is correct
5 Correct 367 ms 8856 KB Output is correct
6 Correct 390 ms 8892 KB Output is correct
7 Correct 375 ms 8800 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 2 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 762 ms 54984 KB Output is correct
12 Correct 811 ms 58072 KB Output is correct
13 Correct 790 ms 55052 KB Output is correct
14 Correct 768 ms 55044 KB Output is correct
15 Correct 762 ms 55192 KB Output is correct
16 Correct 786 ms 59900 KB Output is correct
17 Correct 810 ms 59772 KB Output is correct
18 Correct 800 ms 59596 KB Output is correct
19 Correct 5 ms 340 KB Output is correct
20 Correct 2 ms 308 KB Output is correct
21 Correct 3 ms 308 KB Output is correct
22 Correct 2 ms 340 KB Output is correct
23 Correct 2 ms 340 KB Output is correct
24 Correct 3 ms 340 KB Output is correct
25 Correct 2 ms 340 KB Output is correct
26 Correct 4 ms 340 KB Output is correct
27 Correct 5 ms 340 KB Output is correct
28 Correct 4 ms 340 KB Output is correct
29 Correct 3 ms 340 KB Output is correct
30 Correct 5 ms 360 KB Output is correct
31 Correct 4 ms 440 KB Output is correct
32 Correct 4 ms 340 KB Output is correct
33 Correct 7 ms 356 KB Output is correct
34 Correct 5 ms 340 KB Output is correct
35 Correct 5 ms 340 KB Output is correct
36 Correct 5 ms 436 KB Output is correct
37 Correct 2 ms 340 KB Output is correct
38 Correct 3 ms 308 KB Output is correct
39 Correct 2 ms 308 KB Output is correct
40 Correct 757 ms 55432 KB Output is correct
41 Correct 514 ms 9112 KB Output is correct
42 Correct 478 ms 9084 KB Output is correct
43 Correct 493 ms 9088 KB Output is correct
44 Correct 480 ms 9088 KB Output is correct
45 Correct 475 ms 9164 KB Output is correct
46 Correct 496 ms 9184 KB Output is correct
47 Correct 2 ms 212 KB Output is correct
48 Correct 2 ms 212 KB Output is correct
49 Correct 2 ms 212 KB Output is correct
50 Correct 802 ms 57868 KB Output is correct
51 Correct 766 ms 55584 KB Output is correct
52 Correct 748 ms 57644 KB Output is correct
53 Correct 764 ms 57784 KB Output is correct
54 Correct 777 ms 55420 KB Output is correct
55 Correct 784 ms 55540 KB Output is correct
56 Correct 827 ms 55644 KB Output is correct
57 Correct 800 ms 55740 KB Output is correct
58 Correct 770 ms 59728 KB Output is correct
59 Correct 810 ms 59812 KB Output is correct
60 Correct 830 ms 59976 KB Output is correct
61 Correct 1831 ms 66872 KB Output is correct
62 Correct 513 ms 11832 KB Output is correct
63 Correct 491 ms 11900 KB Output is correct
64 Correct 485 ms 11824 KB Output is correct
65 Correct 496 ms 12112 KB Output is correct
66 Correct 522 ms 12120 KB Output is correct
67 Correct 530 ms 11828 KB Output is correct
68 Correct 2 ms 340 KB Output is correct
69 Correct 2 ms 312 KB Output is correct
70 Correct 2 ms 340 KB Output is correct
71 Correct 1701 ms 72944 KB Output is correct
72 Correct 1761 ms 76424 KB Output is correct
73 Correct 1715 ms 73056 KB Output is correct
74 Correct 1738 ms 73020 KB Output is correct
75 Correct 1778 ms 76844 KB Output is correct
76 Correct 1797 ms 76496 KB Output is correct
77 Correct 1765 ms 76416 KB Output is correct
78 Correct 1780 ms 76492 KB Output is correct
79 Correct 1767 ms 76920 KB Output is correct
80 Correct 1728 ms 77104 KB Output is correct
81 Correct 1763 ms 77028 KB Output is correct
82 Correct 1625 ms 66132 KB Output is correct
83 Correct 497 ms 8908 KB Output is correct
84 Correct 476 ms 8760 KB Output is correct
85 Correct 538 ms 8764 KB Output is correct
86 Correct 478 ms 8772 KB Output is correct
87 Correct 491 ms 8724 KB Output is correct
88 Correct 486 ms 8764 KB Output is correct
89 Correct 2 ms 212 KB Output is correct
90 Correct 2 ms 212 KB Output is correct
91 Correct 2 ms 212 KB Output is correct
92 Correct 1532 ms 66484 KB Output is correct
93 Correct 1588 ms 66208 KB Output is correct
94 Correct 1530 ms 66492 KB Output is correct
95 Correct 1525 ms 66500 KB Output is correct
96 Correct 1228 ms 64236 KB Output is correct
97 Correct 1223 ms 64224 KB Output is correct
98 Correct 1203 ms 64320 KB Output is correct
99 Correct 1611 ms 66244 KB Output is correct
100 Correct 1641 ms 66192 KB Output is correct
101 Correct 1608 ms 66232 KB Output is correct
102 Correct 1660 ms 66240 KB Output is correct
103 Correct 1578 ms 67032 KB Output is correct
104 Correct 1581 ms 67116 KB Output is correct
105 Correct 1591 ms 67044 KB Output is correct
106 Correct 2050 ms 65416 KB Output is correct
107 Correct 565 ms 11852 KB Output is correct
108 Correct 604 ms 11824 KB Output is correct
109 Correct 592 ms 11996 KB Output is correct
110 Correct 2 ms 340 KB Output is correct
111 Correct 2 ms 340 KB Output is correct
112 Correct 2 ms 340 KB Output is correct
113 Correct 1679 ms 75252 KB Output is correct
114 Correct 1646 ms 75192 KB Output is correct
115 Correct 1628 ms 75040 KB Output is correct
116 Correct 1576 ms 75944 KB Output is correct
117 Correct 2037 ms 64756 KB Output is correct
118 Correct 1587 ms 76044 KB Output is correct
119 Correct 1566 ms 75944 KB Output is correct
120 Correct 777 ms 62272 KB Output is correct
121 Correct 781 ms 62068 KB Output is correct
122 Correct 765 ms 62220 KB Output is correct
123 Correct 777 ms 66276 KB Output is correct
124 Correct 779 ms 66128 KB Output is correct
125 Correct 760 ms 66204 KB Output is correct
126 Correct 1937 ms 64000 KB Output is correct
127 Correct 1949 ms 63992 KB Output is correct
128 Correct 2021 ms 65324 KB Output is correct
129 Correct 1991 ms 63908 KB Output is correct
130 Correct 1249 ms 64164 KB Output is correct
131 Correct 1250 ms 64036 KB Output is correct
132 Correct 1257 ms 64092 KB Output is correct
133 Correct 2016 ms 64864 KB Output is correct
134 Correct 2044 ms 64648 KB Output is correct
135 Correct 2089 ms 64580 KB Output is correct
136 Correct 560 ms 11980 KB Output is correct
137 Correct 562 ms 11852 KB Output is correct
138 Correct 556 ms 11948 KB Output is correct