답안 #602372

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
602372 2022-07-23T01:46:37 Z verngutz Progression (NOI20_progression) C++17
100 / 100
1703 ms 71720 KB
#include <bits/stdc++.h>
#define err(args...) {}
#ifdef DEBUG
#include "_debug.cpp"
#endif
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using lim = numeric_limits<T>;
template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; }
template <typename X, typename Y> istream& operator>>(istream& is, pair<X, Y>& p) { return is >> p.first >> p.second; }
enum struct lazy { increment, set, none };
struct segtree {
    segtree* l;
    segtree* r;
    int L, R;
    lazy flag;
    ll front, back, delta;
    int longest_suff, longest_pref, longest, longest_start_index;
    ll range_sum;
    segtree(vector<ll>& a, int L, int R): L(L), R(R), flag(lazy::none), delta(0) {
        if(R - L > 1) {
            int M = (L + R) / 2;
            l = new segtree(a, L, M);
            r = new segtree(a, M, R);
            combine();
        } else {
            l = nullptr;
            r = nullptr;
            front = back = a[L];
            longest_suff = 1;
            longest_pref = 1;
            longest = 1;
            longest_start_index = L;
            range_sum = a[L];
        }
    }
    int len() {
        return R - L;
    }
    void combine() {
        front = l->get_front();
        back = r->get_back();
        bool can_cross = l->get_back() == r->get_front();
        longest_suff = r->longest_suff;
        if(r->longest_suff == r->len() && can_cross) {
            longest_suff += l->longest_suff;
        }
        longest_pref = l->longest_pref;
        if(l->longest_pref == l->len() && can_cross) {
            longest_pref += r->longest_pref;
        }
        int crossing = can_cross ? l->longest_suff + r->longest_pref : 0;
        longest = max({l->longest, r->longest, crossing});
        if(longest == r->longest) {
            longest_start_index = r->longest_start_index;
        } else if(longest == crossing) {
            int M = (L + R) / 2;
            longest_start_index = M - l->longest_suff;
        } else {
            longest_start_index = l->longest_start_index;
        }
        range_sum = l->get_range_sum() + r->get_range_sum();
    }
    void lazy_increment(ll x) {
        if(flag != lazy::set) {
            flag = lazy::increment;
        }
        delta += x;
    }
    void lazy_set(ll x) {
        flag = lazy::set;
        delta = x;
        longest_suff = longest_pref = longest = len();
        longest_start_index = L;
    }
    void unlazy() {
        switch(flag) {
            case lazy::increment:
                l->lazy_increment(delta);
                r->lazy_increment(delta);
                break;
            case lazy::set:
                l->lazy_set(delta);
                r->lazy_set(delta);
                break;
        }
        flag = lazy::none;
        delta = 0;
        combine();
    }
    ll get_front() {
        switch(flag) {
            case lazy::increment:
                return front + delta;
            case lazy::set:
                return delta;
            case lazy::none:
                return front;
        }
    }
    ll get_back() {
        switch(flag) {
            case lazy::increment:
                return back + delta;
            case lazy::set:
                return delta;
            case lazy::none:
                return back;
        }
    }
    ll get_range_sum() {
        switch(flag) {
            case lazy::increment:
                return range_sum + delta * len();
            case lazy::set:
                return delta * len();
            case lazy::none:
                return range_sum;
        }
    }
    pair<int, int> query(int s, int e) {
        if(s <= L and R <= e) {
            return {longest, longest_start_index};
        } else if(R <= s or e <= L) {
            return {0, -1};
        } else {
            unlazy();
            int M = (L + R) / 2;
            auto [L_longest, L_start_index] = l->query(s, e);
            auto [R_longest, R_start_index] = r->query(s, e);
            bool can_cross = l->get_back() == r->get_front() ;
            int crossing = can_cross ? min(l->longest_suff, M - s) + min(r->longest_pref, e - M) : 0;
            int crossing_start_index = can_cross ? max(M - l->longest_suff, s) : -1;
            int ans = max({L_longest, R_longest, crossing});
            int index;
            if(ans == R_longest) {
                index = R_start_index;
            } else if(ans == crossing) {
                index = crossing_start_index;
            } else {
                index = max(L_start_index, s);
            }
            return {ans, index};
        }
    }
    void increment(int s, int e, ll x) {
        if(s <= L and R <= e) {
            lazy_increment(x);
        } else if(R <= s or e <= L) {
            return;
        } else {
            unlazy();
            l->increment(s, e, x);
            r->increment(s, e, x);
            combine();
        }
    }
    void increment(int i, ll x) {
        increment(i, i + 1, x);
    }
    void set(int s, int e, ll x) {
        if(s <= L and R <= e) {
            lazy_set(x);
        } else if(R <= s or e <= L) {
            return;
        } else {
            unlazy();
            l->set(s, e, x);
            r->set(s, e, x);
            combine();
        }
    }
    void set(int i, ll x) {
        set(i, i + 1, x);
    }
    ll sum(int s, int e) {
        if(s <= L and R <= e) {
            return get_range_sum();
        } else if(R <= s or e <= L) {
            return 0;
        } else {
            unlazy();
            return l->sum(s, e) + r->sum(s, e);
        }
    }
    ll sum(int i) {
        return sum(0, i + 1);
    }
    ~segtree() {
        delete l;
        delete r;
    }
};
void debug(segtree& st, int n) {
    for(int i = 0; i < n; i++) {
        cerr << st.sum(i) << " ";
    }
    cerr << endl;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, q;
    cin >> n >> q;
    vector<ll> d(n), a(n);
    cin >> d;
    adjacent_difference(d.begin(), d.end(), a.begin());
    segtree st(a, 0, n);
    while(q--) {
        int t;
        cin >> t;
        if(t == 1) {
            int L, R, S, C;
            cin >> L >> R >> S >> C;
            L--, R--;
            st.increment(L, S);
            st.increment(R + 1, -S);
            st.increment(L + 1, R + 1, C);
            st.increment(R + 1, ll(R - L) * -C);
        } else if(t == 2) {
            int L, R, S, C;
            cin >> L >> R >> S >> C;
            L--, R--;
            ll orig_after = st.sum(R + 1);
            st.set(L, S - st.sum(L - 1));
            st.set(L + 1, R + 1, C);
            st.set(R + 1, orig_after - st.sum(R));
        } else {
            int L, R;
            cin >> L >> R;
            L--, R--;
            auto [longest, longest_start_index] = st.query(L, R + 1);
            cout << longest + (longest_start_index != L) << endl;
        }
    }
    return 0;
}

Compilation message

Progression.cpp: In member function 'void segtree::unlazy()':
Progression.cpp:78:15: warning: enumeration value 'none' not handled in switch [-Wswitch]
   78 |         switch(flag) {
      |               ^
Progression.cpp: In member function 'll segtree::get_range_sum()':
Progression.cpp:121:5: warning: control reaches end of non-void function [-Wreturn-type]
  121 |     }
      |     ^
Progression.cpp: In member function 'll segtree::get_back()':
Progression.cpp:111:5: warning: control reaches end of non-void function [-Wreturn-type]
  111 |     }
      |     ^
Progression.cpp: In member function 'll segtree::get_front()':
Progression.cpp:101:5: warning: control reaches end of non-void function [-Wreturn-type]
  101 |     }
      |     ^
# 결과 실행 시간 메모리 Grader output
1 Correct 452 ms 61964 KB Output is correct
2 Correct 275 ms 492 KB Output is correct
3 Correct 245 ms 488 KB Output is correct
4 Correct 260 ms 588 KB Output is correct
5 Correct 308 ms 600 KB Output is correct
6 Correct 253 ms 668 KB Output is correct
7 Correct 257 ms 476 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 469 ms 61956 KB Output is correct
12 Correct 488 ms 61840 KB Output is correct
13 Correct 528 ms 62232 KB Output is correct
14 Correct 457 ms 62164 KB Output is correct
15 Correct 483 ms 62252 KB Output is correct
16 Correct 503 ms 61928 KB Output is correct
17 Correct 478 ms 61912 KB Output is correct
18 Correct 456 ms 61916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 468 KB Output is correct
2 Correct 2 ms 212 KB Output is correct
3 Correct 2 ms 212 KB Output is correct
4 Correct 2 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 2 ms 340 KB Output is correct
7 Correct 2 ms 212 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 3 ms 468 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 4 ms 468 KB Output is correct
14 Correct 3 ms 456 KB Output is correct
15 Correct 5 ms 452 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 3 ms 468 KB Output is correct
18 Correct 4 ms 468 KB Output is correct
19 Correct 2 ms 212 KB Output is correct
20 Correct 2 ms 340 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 749 ms 62444 KB Output is correct
2 Correct 463 ms 844 KB Output is correct
3 Correct 444 ms 872 KB Output is correct
4 Correct 451 ms 872 KB Output is correct
5 Correct 452 ms 1004 KB Output is correct
6 Correct 436 ms 972 KB Output is correct
7 Correct 473 ms 1096 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 904 ms 62248 KB Output is correct
12 Correct 806 ms 62400 KB Output is correct
13 Correct 857 ms 62124 KB Output is correct
14 Correct 816 ms 62088 KB Output is correct
15 Correct 727 ms 62392 KB Output is correct
16 Correct 825 ms 62664 KB Output is correct
17 Correct 821 ms 62664 KB Output is correct
18 Correct 911 ms 62772 KB Output is correct
19 Correct 816 ms 62044 KB Output is correct
20 Correct 812 ms 62136 KB Output is correct
21 Correct 779 ms 61976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1318 ms 61640 KB Output is correct
2 Correct 349 ms 3760 KB Output is correct
3 Correct 336 ms 3664 KB Output is correct
4 Correct 329 ms 3628 KB Output is correct
5 Correct 331 ms 3640 KB Output is correct
6 Correct 337 ms 3708 KB Output is correct
7 Correct 338 ms 3544 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 1234 ms 68004 KB Output is correct
12 Correct 1172 ms 71304 KB Output is correct
13 Correct 1209 ms 67980 KB Output is correct
14 Correct 1258 ms 67932 KB Output is correct
15 Correct 1088 ms 71120 KB Output is correct
16 Correct 1214 ms 71328 KB Output is correct
17 Correct 1211 ms 71452 KB Output is correct
18 Correct 1265 ms 71352 KB Output is correct
19 Correct 1149 ms 71336 KB Output is correct
20 Correct 1123 ms 71316 KB Output is correct
21 Correct 1129 ms 71216 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 749 ms 62444 KB Output is correct
2 Correct 463 ms 844 KB Output is correct
3 Correct 444 ms 872 KB Output is correct
4 Correct 451 ms 872 KB Output is correct
5 Correct 452 ms 1004 KB Output is correct
6 Correct 436 ms 972 KB Output is correct
7 Correct 473 ms 1096 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 904 ms 62248 KB Output is correct
12 Correct 806 ms 62400 KB Output is correct
13 Correct 857 ms 62124 KB Output is correct
14 Correct 816 ms 62088 KB Output is correct
15 Correct 727 ms 62392 KB Output is correct
16 Correct 825 ms 62664 KB Output is correct
17 Correct 821 ms 62664 KB Output is correct
18 Correct 911 ms 62772 KB Output is correct
19 Correct 816 ms 62044 KB Output is correct
20 Correct 812 ms 62136 KB Output is correct
21 Correct 779 ms 61976 KB Output is correct
22 Correct 1447 ms 61740 KB Output is correct
23 Correct 374 ms 600 KB Output is correct
24 Correct 400 ms 548 KB Output is correct
25 Correct 352 ms 592 KB Output is correct
26 Correct 393 ms 572 KB Output is correct
27 Correct 356 ms 576 KB Output is correct
28 Correct 381 ms 580 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 2 ms 212 KB Output is correct
31 Correct 1 ms 212 KB Output is correct
32 Correct 1483 ms 61736 KB Output is correct
33 Correct 1415 ms 61712 KB Output is correct
34 Correct 1441 ms 61736 KB Output is correct
35 Correct 1424 ms 61772 KB Output is correct
36 Correct 1312 ms 62052 KB Output is correct
37 Correct 1300 ms 62076 KB Output is correct
38 Correct 1304 ms 61928 KB Output is correct
39 Correct 1400 ms 61664 KB Output is correct
40 Correct 1495 ms 61860 KB Output is correct
41 Correct 1473 ms 61708 KB Output is correct
42 Correct 1509 ms 61896 KB Output is correct
43 Correct 1350 ms 61656 KB Output is correct
44 Correct 1450 ms 61860 KB Output is correct
45 Correct 1374 ms 61672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 452 ms 61964 KB Output is correct
2 Correct 275 ms 492 KB Output is correct
3 Correct 245 ms 488 KB Output is correct
4 Correct 260 ms 588 KB Output is correct
5 Correct 308 ms 600 KB Output is correct
6 Correct 253 ms 668 KB Output is correct
7 Correct 257 ms 476 KB Output is correct
8 Correct 2 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 2 ms 212 KB Output is correct
11 Correct 469 ms 61956 KB Output is correct
12 Correct 488 ms 61840 KB Output is correct
13 Correct 528 ms 62232 KB Output is correct
14 Correct 457 ms 62164 KB Output is correct
15 Correct 483 ms 62252 KB Output is correct
16 Correct 503 ms 61928 KB Output is correct
17 Correct 478 ms 61912 KB Output is correct
18 Correct 456 ms 61916 KB Output is correct
19 Correct 4 ms 468 KB Output is correct
20 Correct 2 ms 212 KB Output is correct
21 Correct 2 ms 212 KB Output is correct
22 Correct 2 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 2 ms 340 KB Output is correct
25 Correct 2 ms 212 KB Output is correct
26 Correct 5 ms 468 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 468 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 4 ms 468 KB Output is correct
31 Correct 4 ms 468 KB Output is correct
32 Correct 3 ms 456 KB Output is correct
33 Correct 5 ms 452 KB Output is correct
34 Correct 4 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 2 ms 212 KB Output is correct
38 Correct 2 ms 340 KB Output is correct
39 Correct 2 ms 212 KB Output is correct
40 Correct 749 ms 62444 KB Output is correct
41 Correct 463 ms 844 KB Output is correct
42 Correct 444 ms 872 KB Output is correct
43 Correct 451 ms 872 KB Output is correct
44 Correct 452 ms 1004 KB Output is correct
45 Correct 436 ms 972 KB Output is correct
46 Correct 473 ms 1096 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 904 ms 62248 KB Output is correct
51 Correct 806 ms 62400 KB Output is correct
52 Correct 857 ms 62124 KB Output is correct
53 Correct 816 ms 62088 KB Output is correct
54 Correct 727 ms 62392 KB Output is correct
55 Correct 825 ms 62664 KB Output is correct
56 Correct 821 ms 62664 KB Output is correct
57 Correct 911 ms 62772 KB Output is correct
58 Correct 816 ms 62044 KB Output is correct
59 Correct 812 ms 62136 KB Output is correct
60 Correct 779 ms 61976 KB Output is correct
61 Correct 1318 ms 61640 KB Output is correct
62 Correct 349 ms 3760 KB Output is correct
63 Correct 336 ms 3664 KB Output is correct
64 Correct 329 ms 3628 KB Output is correct
65 Correct 331 ms 3640 KB Output is correct
66 Correct 337 ms 3708 KB Output is correct
67 Correct 338 ms 3544 KB Output is correct
68 Correct 2 ms 212 KB Output is correct
69 Correct 1 ms 212 KB Output is correct
70 Correct 2 ms 212 KB Output is correct
71 Correct 1234 ms 68004 KB Output is correct
72 Correct 1172 ms 71304 KB Output is correct
73 Correct 1209 ms 67980 KB Output is correct
74 Correct 1258 ms 67932 KB Output is correct
75 Correct 1088 ms 71120 KB Output is correct
76 Correct 1214 ms 71328 KB Output is correct
77 Correct 1211 ms 71452 KB Output is correct
78 Correct 1265 ms 71352 KB Output is correct
79 Correct 1149 ms 71336 KB Output is correct
80 Correct 1123 ms 71316 KB Output is correct
81 Correct 1129 ms 71216 KB Output is correct
82 Correct 1447 ms 61740 KB Output is correct
83 Correct 374 ms 600 KB Output is correct
84 Correct 400 ms 548 KB Output is correct
85 Correct 352 ms 592 KB Output is correct
86 Correct 393 ms 572 KB Output is correct
87 Correct 356 ms 576 KB Output is correct
88 Correct 381 ms 580 KB Output is correct
89 Correct 1 ms 212 KB Output is correct
90 Correct 2 ms 212 KB Output is correct
91 Correct 1 ms 212 KB Output is correct
92 Correct 1483 ms 61736 KB Output is correct
93 Correct 1415 ms 61712 KB Output is correct
94 Correct 1441 ms 61736 KB Output is correct
95 Correct 1424 ms 61772 KB Output is correct
96 Correct 1312 ms 62052 KB Output is correct
97 Correct 1300 ms 62076 KB Output is correct
98 Correct 1304 ms 61928 KB Output is correct
99 Correct 1400 ms 61664 KB Output is correct
100 Correct 1495 ms 61860 KB Output is correct
101 Correct 1473 ms 61708 KB Output is correct
102 Correct 1509 ms 61896 KB Output is correct
103 Correct 1350 ms 61656 KB Output is correct
104 Correct 1450 ms 61860 KB Output is correct
105 Correct 1374 ms 61672 KB Output is correct
106 Correct 1683 ms 71656 KB Output is correct
107 Correct 432 ms 3648 KB Output is correct
108 Correct 450 ms 3672 KB Output is correct
109 Correct 444 ms 3660 KB Output is correct
110 Correct 1 ms 212 KB Output is correct
111 Correct 1 ms 212 KB Output is correct
112 Correct 2 ms 324 KB Output is correct
113 Correct 1443 ms 70736 KB Output is correct
114 Correct 1483 ms 70952 KB Output is correct
115 Correct 1429 ms 70780 KB Output is correct
116 Correct 1429 ms 70796 KB Output is correct
117 Correct 1689 ms 71612 KB Output is correct
118 Correct 1372 ms 70676 KB Output is correct
119 Correct 1358 ms 70704 KB Output is correct
120 Correct 818 ms 69248 KB Output is correct
121 Correct 784 ms 69196 KB Output is correct
122 Correct 921 ms 69108 KB Output is correct
123 Correct 749 ms 68360 KB Output is correct
124 Correct 815 ms 68328 KB Output is correct
125 Correct 827 ms 68416 KB Output is correct
126 Correct 1575 ms 68344 KB Output is correct
127 Correct 1694 ms 68448 KB Output is correct
128 Correct 1703 ms 71656 KB Output is correct
129 Correct 1626 ms 68300 KB Output is correct
130 Correct 1318 ms 68268 KB Output is correct
131 Correct 1232 ms 68320 KB Output is correct
132 Correct 1287 ms 68452 KB Output is correct
133 Correct 1650 ms 71680 KB Output is correct
134 Correct 1595 ms 71708 KB Output is correct
135 Correct 1670 ms 71720 KB Output is correct
136 Correct 430 ms 3736 KB Output is correct
137 Correct 447 ms 3776 KB Output is correct
138 Correct 447 ms 3680 KB Output is correct