#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--;
st.set(L, S - (L == 0 ? 0 : st.sum(L - 1)));
st.set(L + 1, R + 1, C);
st.set(R + 1, (R + 1 < n ? st.sum(R + 1) : 0) - (S + ll(R - L) * C));
} 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 | }
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
455 ms |
65300 KB |
Output is correct |
2 |
Correct |
277 ms |
3528 KB |
Output is correct |
3 |
Correct |
253 ms |
3456 KB |
Output is correct |
4 |
Correct |
282 ms |
3552 KB |
Output is correct |
5 |
Correct |
253 ms |
3532 KB |
Output is correct |
6 |
Correct |
316 ms |
3660 KB |
Output is correct |
7 |
Correct |
251 ms |
3424 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
478 ms |
69988 KB |
Output is correct |
12 |
Correct |
488 ms |
70060 KB |
Output is correct |
13 |
Correct |
477 ms |
70160 KB |
Output is correct |
14 |
Correct |
458 ms |
70288 KB |
Output is correct |
15 |
Correct |
468 ms |
70216 KB |
Output is correct |
16 |
Correct |
488 ms |
69888 KB |
Output is correct |
17 |
Correct |
484 ms |
69812 KB |
Output is correct |
18 |
Correct |
465 ms |
69928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
741 ms |
62560 KB |
Output is correct |
2 |
Correct |
452 ms |
1092 KB |
Output is correct |
3 |
Correct |
427 ms |
1080 KB |
Output is correct |
4 |
Correct |
442 ms |
1176 KB |
Output is correct |
5 |
Correct |
452 ms |
1124 KB |
Output is correct |
6 |
Correct |
447 ms |
1208 KB |
Output is correct |
7 |
Correct |
458 ms |
1136 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 |
844 ms |
62412 KB |
Output is correct |
12 |
Correct |
724 ms |
62716 KB |
Output is correct |
13 |
Correct |
825 ms |
62460 KB |
Output is correct |
14 |
Correct |
837 ms |
62308 KB |
Output is correct |
15 |
Correct |
745 ms |
62824 KB |
Output is correct |
16 |
Correct |
820 ms |
63004 KB |
Output is correct |
17 |
Correct |
800 ms |
63012 KB |
Output is correct |
18 |
Correct |
842 ms |
63184 KB |
Output is correct |
19 |
Correct |
759 ms |
62284 KB |
Output is correct |
20 |
Correct |
790 ms |
62268 KB |
Output is correct |
21 |
Correct |
764 ms |
62316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1150 ms |
68988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
741 ms |
62560 KB |
Output is correct |
2 |
Correct |
452 ms |
1092 KB |
Output is correct |
3 |
Correct |
427 ms |
1080 KB |
Output is correct |
4 |
Correct |
442 ms |
1176 KB |
Output is correct |
5 |
Correct |
452 ms |
1124 KB |
Output is correct |
6 |
Correct |
447 ms |
1208 KB |
Output is correct |
7 |
Correct |
458 ms |
1136 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 |
844 ms |
62412 KB |
Output is correct |
12 |
Correct |
724 ms |
62716 KB |
Output is correct |
13 |
Correct |
825 ms |
62460 KB |
Output is correct |
14 |
Correct |
837 ms |
62308 KB |
Output is correct |
15 |
Correct |
745 ms |
62824 KB |
Output is correct |
16 |
Correct |
820 ms |
63004 KB |
Output is correct |
17 |
Correct |
800 ms |
63012 KB |
Output is correct |
18 |
Correct |
842 ms |
63184 KB |
Output is correct |
19 |
Correct |
759 ms |
62284 KB |
Output is correct |
20 |
Correct |
790 ms |
62268 KB |
Output is correct |
21 |
Correct |
764 ms |
62316 KB |
Output is correct |
22 |
Correct |
1438 ms |
62080 KB |
Output is correct |
23 |
Correct |
372 ms |
1540 KB |
Output is correct |
24 |
Correct |
359 ms |
1476 KB |
Output is correct |
25 |
Correct |
410 ms |
1452 KB |
Output is correct |
26 |
Correct |
375 ms |
1612 KB |
Output is correct |
27 |
Correct |
364 ms |
1392 KB |
Output is correct |
28 |
Correct |
377 ms |
1524 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
1406 ms |
62808 KB |
Output is correct |
33 |
Correct |
1408 ms |
62704 KB |
Output is correct |
34 |
Correct |
1435 ms |
62652 KB |
Output is correct |
35 |
Correct |
1431 ms |
62896 KB |
Output is correct |
36 |
Correct |
1292 ms |
62940 KB |
Output is correct |
37 |
Correct |
1342 ms |
62972 KB |
Output is correct |
38 |
Correct |
1273 ms |
62916 KB |
Output is correct |
39 |
Correct |
1386 ms |
62676 KB |
Output is correct |
40 |
Correct |
1557 ms |
62736 KB |
Output is correct |
41 |
Correct |
1423 ms |
62624 KB |
Output is correct |
42 |
Correct |
1455 ms |
62808 KB |
Output is correct |
43 |
Correct |
1375 ms |
62652 KB |
Output is correct |
44 |
Correct |
1359 ms |
62632 KB |
Output is correct |
45 |
Correct |
1400 ms |
62808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
455 ms |
65300 KB |
Output is correct |
2 |
Correct |
277 ms |
3528 KB |
Output is correct |
3 |
Correct |
253 ms |
3456 KB |
Output is correct |
4 |
Correct |
282 ms |
3552 KB |
Output is correct |
5 |
Correct |
253 ms |
3532 KB |
Output is correct |
6 |
Correct |
316 ms |
3660 KB |
Output is correct |
7 |
Correct |
251 ms |
3424 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
478 ms |
69988 KB |
Output is correct |
12 |
Correct |
488 ms |
70060 KB |
Output is correct |
13 |
Correct |
477 ms |
70160 KB |
Output is correct |
14 |
Correct |
458 ms |
70288 KB |
Output is correct |
15 |
Correct |
468 ms |
70216 KB |
Output is correct |
16 |
Correct |
488 ms |
69888 KB |
Output is correct |
17 |
Correct |
484 ms |
69812 KB |
Output is correct |
18 |
Correct |
465 ms |
69928 KB |
Output is correct |
19 |
Incorrect |
3 ms |
468 KB |
Output isn't correct |
20 |
Halted |
0 ms |
0 KB |
- |