답안 #892548

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
892548 2023-12-25T13:35:38 Z CDuong Progression (NOI20_progression) C++17
100 / 100
640 ms 100044 KB
/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/

#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;

const int mxN = 3e5 + 5;
const int mod = 1e9 + 7;
const i64 oo = 1e18;

struct SegmentTree {
    struct node {
        i64 val = 0;
        int same = 0, len = 0;
        pair<int, int> pfx, sfx;
        i64 lz_mul = 1, lz_add = 0;

        node() {}
        node(int val) : val(val), same(1), len(1), pfx({val, 1}), sfx({val, 1}) {}
        
        node operator + (const node &rhs) {
            node res;
            res.val = val + rhs.val;
            res.len = len + rhs.len;
            res.same = max(same, rhs.same);
            if (sfx.ff == rhs.pfx.ff) res.same = max(res.same, sfx.ss + rhs.pfx.ss);
            res.pfx = pfx, res.sfx = rhs.sfx;
            if (pfx.ss == len and pfx.ff == rhs.pfx.ff) res.pfx = {pfx.ff, pfx.ss + rhs.pfx.ss};
            if (rhs.sfx.ss == rhs.len and sfx.ff == rhs.sfx.ff) res.sfx = {rhs.sfx.ff, rhs.sfx.ss + sfx.ss};
            // cout << *this << endl << rhs << endl << res << endl << endl;
            return res;
        }

        friend ostream & operator << (ostream &out, node o) {
            return out << o.pfx.ff << " " << o.pfx.ss << " " << o.sfx.ff << " " << o.sfx.ss << " " << o.same;
        }
    };

    int n;
    vector<node> data;

    SegmentTree(int n, vector<int> &diff) : n(n), data(4 * n + 10) {
        auto build = [&](auto build, int l, int r, int idx) -> void {
            if (l == r) {
                data[idx] = node(diff[l]);
                return;
            }
            int mid = (l + r) >> 1;
            build(build, l, mid, idx << 1);
            build(build, mid + 1, r, idx << 1 | 1);
            data[idx] = data[idx << 1] + data[idx << 1 | 1];
        };
        build(build, 1, n, 1);
    };

    void apply(int idx, i64 mul, i64 add) {
        if (mul == 0) {
            data[idx].val = add * data[idx].len;
            data[idx].same = data[idx].len;
            data[idx].pfx = data[idx].sfx = {add, data[idx].len};
            data[idx].lz_mul = mul, data[idx].lz_add = add;
        }
        else {
            data[idx].val += add * data[idx].len;
            data[idx].pfx.ff += add, data[idx].sfx.ff += add;
            data[idx].lz_add += add;
        }
    }

    void down(int idx) {
        apply(idx << 1, data[idx].lz_mul, data[idx].lz_add);
        apply(idx << 1 | 1, data[idx].lz_mul, data[idx].lz_add);
        data[idx].lz_add = 0, data[idx].lz_mul = 1;
    }

    void update(int l, int r, int idx, int u, int v, i64 mul, i64 add) {
        if (u <= l && r <= v) {
            apply(idx, mul, add);
            return;
        }
        down(idx);
        int mid = (l + r) >> 1;
        if (u <= mid) update(l, mid, idx << 1, u, v, mul, add);
        if (mid + 1 <= v) update(mid + 1, r, idx << 1 | 1, u, v, mul, add);
        data[idx] = data[idx << 1] + data[idx << 1 | 1];
    }

    void update(int u, int v, i64 mul, i64 add) {
        update(1, n, 1, u, v, mul, add);
    }

    node get(int l, int r, int idx, int u, int v) {
        if (u <= l && r <= v) return data[idx];
        down(idx);
        int mid = (l + r) >> 1;
        if (v <= mid) return get(l, mid, idx << 1, u, v);
        if (mid + 1 <= u) return get(mid + 1, r, idx << 1 | 1, u, v);
        return get(l, mid, idx << 1, u, v) + get(mid + 1, r, idx << 1 | 1, u, v);
    }

    node get(int u, int v) {
        return get(1, n, 1, u, v);
    }
};

int n, q;
int a[mxN];
vector<int> diff;

void solve() {
    cin >> n >> q;
    diff.resize(n + 1);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        diff[i] = a[i] - a[i - 1];
    }
    SegmentTree st(n, diff);
    // cout << st.data[1].same << endl;
    while (q--) {
        int tp;
        cin >> tp;
        if (tp == 1) {
            int l, r, s, c;
            cin >> l >> r >> s >> c;
            st.update(l, l, 1, s);
            if (l < r) st.update(l + 1, r, 1, c);
            if (r + 1 <= n) st.update(r + 1, r + 1, 1, -(1ll * c * (r - l) + s));
        }
        else if (tp == 2) {
            int l, r, s, c;
            cin >> l >> r >> s >> c;
            i64 cl = st.get(1, l).val;
            if (r + 1 <= n) {
                i64 cr1 = st.get(1, r + 1).val;
                st.update(r + 1, r + 1, 0, cr1 - (s + (r - l) * c));
            }
            st.update(l, l, 1, s - cl);
            if (l < r) st.update(l + 1, r, 0, c);
        }
        else {
            int l, r;
            cin >> l >> r;
            if (l == r) cout << 1 << "\n";
            else cout << st.get(l + 1, r).same + 1 << "\n";
        }
    }
    // for (int i = 1; i <= n; ++i)
    //     cout << st.get(1, i).val << " \n"[i == n];
}

signed main() {

#ifndef CDuongg
    if(fopen(taskname".inp", "r"))
        assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
    freopen("bai3.inp", "r", stdin);
    freopen("bai3.out", "w", stdout);
    auto start = chrono::high_resolution_clock::now();
#endif

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1; //cin >> t;
    while(t--) solve();

#ifdef CDuongg
   // auto end = chrono::high_resolution_clock::now();
   // cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
   // cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
   // cout << "Check array size pls sir" << endl;
#endif

}
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 90196 KB Output is correct
2 Correct 97 ms 600 KB Output is correct
3 Correct 96 ms 596 KB Output is correct
4 Correct 97 ms 596 KB Output is correct
5 Correct 97 ms 592 KB Output is correct
6 Correct 99 ms 764 KB Output is correct
7 Correct 97 ms 592 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 273 ms 90196 KB Output is correct
12 Correct 266 ms 90180 KB Output is correct
13 Correct 268 ms 90488 KB Output is correct
14 Correct 267 ms 90448 KB Output is correct
15 Correct 272 ms 90448 KB Output is correct
16 Correct 277 ms 90192 KB Output is correct
17 Correct 263 ms 90196 KB Output is correct
18 Correct 269 ms 90196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 2 ms 604 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 1 ms 604 KB Output is correct
13 Correct 1 ms 604 KB Output is correct
14 Correct 1 ms 604 KB Output is correct
15 Correct 2 ms 604 KB Output is correct
16 Correct 2 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 1 ms 604 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 90616 KB Output is correct
2 Correct 65 ms 884 KB Output is correct
3 Correct 65 ms 1020 KB Output is correct
4 Correct 57 ms 852 KB Output is correct
5 Correct 68 ms 1104 KB Output is correct
6 Correct 68 ms 1108 KB Output is correct
7 Correct 72 ms 1016 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 241 ms 90364 KB Output is correct
12 Correct 195 ms 90648 KB Output is correct
13 Correct 233 ms 90448 KB Output is correct
14 Correct 237 ms 90272 KB Output is correct
15 Correct 200 ms 90760 KB Output is correct
16 Correct 236 ms 90960 KB Output is correct
17 Correct 253 ms 91220 KB Output is correct
18 Correct 244 ms 90960 KB Output is correct
19 Correct 211 ms 90192 KB Output is correct
20 Correct 208 ms 90144 KB Output is correct
21 Correct 211 ms 90192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 409 ms 89952 KB Output is correct
2 Correct 99 ms 3680 KB Output is correct
3 Correct 102 ms 3628 KB Output is correct
4 Correct 104 ms 3664 KB Output is correct
5 Correct 103 ms 3664 KB Output is correct
6 Correct 102 ms 3812 KB Output is correct
7 Correct 102 ms 3668 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 416 ms 96232 KB Output is correct
12 Correct 409 ms 99848 KB Output is correct
13 Correct 407 ms 96084 KB Output is correct
14 Correct 430 ms 96080 KB Output is correct
15 Correct 389 ms 99440 KB Output is correct
16 Correct 427 ms 99408 KB Output is correct
17 Correct 432 ms 99744 KB Output is correct
18 Correct 421 ms 99664 KB Output is correct
19 Correct 411 ms 99480 KB Output is correct
20 Correct 398 ms 99412 KB Output is correct
21 Correct 419 ms 99320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 207 ms 90616 KB Output is correct
2 Correct 65 ms 884 KB Output is correct
3 Correct 65 ms 1020 KB Output is correct
4 Correct 57 ms 852 KB Output is correct
5 Correct 68 ms 1104 KB Output is correct
6 Correct 68 ms 1108 KB Output is correct
7 Correct 72 ms 1016 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 241 ms 90364 KB Output is correct
12 Correct 195 ms 90648 KB Output is correct
13 Correct 233 ms 90448 KB Output is correct
14 Correct 237 ms 90272 KB Output is correct
15 Correct 200 ms 90760 KB Output is correct
16 Correct 236 ms 90960 KB Output is correct
17 Correct 253 ms 91220 KB Output is correct
18 Correct 244 ms 90960 KB Output is correct
19 Correct 211 ms 90192 KB Output is correct
20 Correct 208 ms 90144 KB Output is correct
21 Correct 211 ms 90192 KB Output is correct
22 Correct 466 ms 90520 KB Output is correct
23 Correct 96 ms 648 KB Output is correct
24 Correct 94 ms 564 KB Output is correct
25 Correct 94 ms 568 KB Output is correct
26 Correct 104 ms 868 KB Output is correct
27 Correct 98 ms 592 KB Output is correct
28 Correct 98 ms 524 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 477 ms 90324 KB Output is correct
33 Correct 444 ms 89944 KB Output is correct
34 Correct 467 ms 90048 KB Output is correct
35 Correct 477 ms 90192 KB Output is correct
36 Correct 379 ms 90272 KB Output is correct
37 Correct 385 ms 90196 KB Output is correct
38 Correct 391 ms 90192 KB Output is correct
39 Correct 476 ms 90464 KB Output is correct
40 Correct 491 ms 90216 KB Output is correct
41 Correct 469 ms 89984 KB Output is correct
42 Correct 496 ms 89960 KB Output is correct
43 Correct 454 ms 90004 KB Output is correct
44 Correct 461 ms 89940 KB Output is correct
45 Correct 460 ms 90016 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 270 ms 90196 KB Output is correct
2 Correct 97 ms 600 KB Output is correct
3 Correct 96 ms 596 KB Output is correct
4 Correct 97 ms 596 KB Output is correct
5 Correct 97 ms 592 KB Output is correct
6 Correct 99 ms 764 KB Output is correct
7 Correct 97 ms 592 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 273 ms 90196 KB Output is correct
12 Correct 266 ms 90180 KB Output is correct
13 Correct 268 ms 90488 KB Output is correct
14 Correct 267 ms 90448 KB Output is correct
15 Correct 272 ms 90448 KB Output is correct
16 Correct 277 ms 90192 KB Output is correct
17 Correct 263 ms 90196 KB Output is correct
18 Correct 269 ms 90196 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 604 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 2 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 344 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 207 ms 90616 KB Output is correct
41 Correct 65 ms 884 KB Output is correct
42 Correct 65 ms 1020 KB Output is correct
43 Correct 57 ms 852 KB Output is correct
44 Correct 68 ms 1104 KB Output is correct
45 Correct 68 ms 1108 KB Output is correct
46 Correct 72 ms 1016 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 241 ms 90364 KB Output is correct
51 Correct 195 ms 90648 KB Output is correct
52 Correct 233 ms 90448 KB Output is correct
53 Correct 237 ms 90272 KB Output is correct
54 Correct 200 ms 90760 KB Output is correct
55 Correct 236 ms 90960 KB Output is correct
56 Correct 253 ms 91220 KB Output is correct
57 Correct 244 ms 90960 KB Output is correct
58 Correct 211 ms 90192 KB Output is correct
59 Correct 208 ms 90144 KB Output is correct
60 Correct 211 ms 90192 KB Output is correct
61 Correct 409 ms 89952 KB Output is correct
62 Correct 99 ms 3680 KB Output is correct
63 Correct 102 ms 3628 KB Output is correct
64 Correct 104 ms 3664 KB Output is correct
65 Correct 103 ms 3664 KB Output is correct
66 Correct 102 ms 3812 KB Output is correct
67 Correct 102 ms 3668 KB Output is correct
68 Correct 1 ms 344 KB Output is correct
69 Correct 1 ms 348 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 416 ms 96232 KB Output is correct
72 Correct 409 ms 99848 KB Output is correct
73 Correct 407 ms 96084 KB Output is correct
74 Correct 430 ms 96080 KB Output is correct
75 Correct 389 ms 99440 KB Output is correct
76 Correct 427 ms 99408 KB Output is correct
77 Correct 432 ms 99744 KB Output is correct
78 Correct 421 ms 99664 KB Output is correct
79 Correct 411 ms 99480 KB Output is correct
80 Correct 398 ms 99412 KB Output is correct
81 Correct 419 ms 99320 KB Output is correct
82 Correct 466 ms 90520 KB Output is correct
83 Correct 96 ms 648 KB Output is correct
84 Correct 94 ms 564 KB Output is correct
85 Correct 94 ms 568 KB Output is correct
86 Correct 104 ms 868 KB Output is correct
87 Correct 98 ms 592 KB Output is correct
88 Correct 98 ms 524 KB Output is correct
89 Correct 1 ms 348 KB Output is correct
90 Correct 1 ms 348 KB Output is correct
91 Correct 0 ms 348 KB Output is correct
92 Correct 477 ms 90324 KB Output is correct
93 Correct 444 ms 89944 KB Output is correct
94 Correct 467 ms 90048 KB Output is correct
95 Correct 477 ms 90192 KB Output is correct
96 Correct 379 ms 90272 KB Output is correct
97 Correct 385 ms 90196 KB Output is correct
98 Correct 391 ms 90192 KB Output is correct
99 Correct 476 ms 90464 KB Output is correct
100 Correct 491 ms 90216 KB Output is correct
101 Correct 469 ms 89984 KB Output is correct
102 Correct 496 ms 89960 KB Output is correct
103 Correct 454 ms 90004 KB Output is correct
104 Correct 461 ms 89940 KB Output is correct
105 Correct 460 ms 90016 KB Output is correct
106 Correct 636 ms 99900 KB Output is correct
107 Correct 135 ms 3668 KB Output is correct
108 Correct 134 ms 3664 KB Output is correct
109 Correct 135 ms 3668 KB Output is correct
110 Correct 1 ms 344 KB Output is correct
111 Correct 1 ms 348 KB Output is correct
112 Correct 1 ms 348 KB Output is correct
113 Correct 483 ms 98944 KB Output is correct
114 Correct 492 ms 99068 KB Output is correct
115 Correct 507 ms 99160 KB Output is correct
116 Correct 465 ms 98736 KB Output is correct
117 Correct 640 ms 99988 KB Output is correct
118 Correct 467 ms 98888 KB Output is correct
119 Correct 461 ms 98764 KB Output is correct
120 Correct 268 ms 97528 KB Output is correct
121 Correct 242 ms 97364 KB Output is correct
122 Correct 252 ms 97312 KB Output is correct
123 Correct 214 ms 96544 KB Output is correct
124 Correct 212 ms 96596 KB Output is correct
125 Correct 220 ms 96796 KB Output is correct
126 Correct 593 ms 96448 KB Output is correct
127 Correct 600 ms 96576 KB Output is correct
128 Correct 609 ms 99992 KB Output is correct
129 Correct 599 ms 96628 KB Output is correct
130 Correct 405 ms 96496 KB Output is correct
131 Correct 414 ms 96628 KB Output is correct
132 Correct 413 ms 96592 KB Output is correct
133 Correct 601 ms 99920 KB Output is correct
134 Correct 609 ms 99944 KB Output is correct
135 Correct 632 ms 100044 KB Output is correct
136 Correct 134 ms 3668 KB Output is correct
137 Correct 135 ms 3660 KB Output is correct
138 Correct 137 ms 3940 KB Output is correct