#include <bits/stdc++.h>
#define lf (h << 1)
#define mf ((l + r) >> 1)
#define rf ((h << 1) | 1)
#define tree int h, int l, int r
#define left lf, l, mf
#define right rf, mf + 1, r
#define ll long long
#define int long long
using namespace std;
const int N = 3e5 + 5;
int n, m, q, L, R, id;
ll vl, a[N], b[N], vd[4 * N];
bool dv[4 * N], tp;
struct node {
ll ans;
int pr;
int sf;
ll fr;
ll ls;
int len;
bool add;
node() {
ans = 0;
pr = 0;
sf = 0;
fr = 0;
ls = 0;
len = 0;
add = 0;
}
};
node v[4 * N], bs;
node mrg(node a, node b) {
node c;
c.add = 0;
c.len = a.len + b.len;
c.ans = a.ans + b.ans;
c.fr = a.fr, c.ls = b.ls;
c.sf = b.sf, c.pr = a.pr;
if ((a.ls < 0 && b.fr > 0) || (a.ls > 0 && b.fr < 0)) {
if (b.sf == b.len) c.sf += a.sf;
if (a.pr == a.len) c.pr += b.pr;
c.ans += a.sf * b.pr;
}
return c;
}
void build(tree) {
if (l > r) return ;
if (l == r) {
v[h].add = 0;
v[h].len = 1;
v[h].fr = b[l];
v[h].ls = b[l];
if (v[h].fr) {
v[h].pr = v[h].sf = v[h].ans = 1;
}
else {
v[h].pr = v[h].sf = v[h].ans = 0;
}
return ;
}
build(left), build(right);
v[h] = mrg(v[lf], v[rf]);
}
void shift(tree) {
if (v[h].add) {
v[lf].add ^= 1, v[lf].fr *= -1; v[lf].ls *= -1;
v[rf].add ^= 1, v[rf].fr *= -1; v[rf].ls *= -1;
v[h].add = 0;
return ;
}
}
node get(tree) {
if (r < L || R < l) return bs;
if (L <= l && r <= R) return v[h];
shift(h, l, r);
return mrg(get(left), get(right));
}
void upd(tree) {
if (r < id || id < l) return ;
if (l == id && id == r) {
v[h].fr += vl;
v[h].ls += vl;
if (v[h].fr) {
v[h].pr = v[h].sf = v[h].ans = 1;
}
else {
v[h].pr = v[h].sf = v[h].ans = 0;
}
return ;
}
shift(h, l, r);
upd(left), upd(right);
v[h] = mrg(v[lf], v[rf]);
}
void upd_(tree) {
if (r < L || R < l) return ;
if (L <= l && r <= R) {
v[h].add ^= 1;
v[h].fr *= -1;
v[h].ls *= -1;
return ;
}
shift(h, l, r);
upd_(left), upd_(right);
v[h] = mrg(v[lf], v[rf]);
}
void bld(tree) {
if (l == r) { vd[h] = a[l]; return ; }
bld(left); bld(right);
}
void shf(tree) {
if (dv[h]) {
vd[lf] = vd[lf] * -1 + vd[h];
vd[rf] = vd[rf] * -1 + vd[h];
dv[lf] ^= 1, dv[rf] ^= 1;
}
else {
vd[lf] += vd[h];
vd[rf] += vd[h];
}
vd[h] = dv[h] = 0;
return ;
}
void up(tree) {
if (r < L || R < l) return ;
if (L <= l && r <= R) {
if (tp == 0) {
vd[h] += vl;
}
else
if (tp == 1) {
vd[h] *= -1;
dv[h] ^= 1;
}
return ;
}
shf(h, l, r);
up(left), up(right);
}
ll G(tree) {
if (l == r) return vd[h];
shf(h, l, r);
if (id <= mf)
return G(left);
return G(right);
}
main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
if (1 < i) {
b[i - 1] = a[i - 1] - a[i];
}
}
m = n - 1, build(1, 1, m), bld(1, 1, n);
for (int i = 1; i <= q; ++i) {
char c;
cin >> c;
if (c == '+') {
cin >> L >> R >> vl; tp = 0, up(1, 1, n), --L;
vl = -vl, id = L; if (1 <= L) upd(1, 1, m);
vl = -vl, id = R; if (R <= m) upd(1, 1, m);
}
else
if (c == '*') {
vl = 0;
cin >> L >> R;
if (1 < L) {
id = L;
ll Al = G(1, 1, n);
id = L - 1;
vl = 2 * Al;
upd(1, 1, m);
}
if (R < n) {
id = R;
ll Ar = G(1, 1, n);
vl = -2 * Ar;
upd(1, 1, m);
}
tp = 1, up(1, 1, n);
--R, upd_(1, 1, m);
}
else
if (c == '?') {
cin >> L >> R; --R;
cout << get(1, 1, m).ans + R - L + 2 << "\n";
}
}
}
Compilation message
Main.cpp:166:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
166 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
66316 KB |
Output is correct |
2 |
Correct |
40 ms |
66284 KB |
Output is correct |
3 |
Correct |
46 ms |
66316 KB |
Output is correct |
4 |
Correct |
38 ms |
66280 KB |
Output is correct |
5 |
Correct |
40 ms |
66308 KB |
Output is correct |
6 |
Correct |
29 ms |
66036 KB |
Output is correct |
7 |
Correct |
33 ms |
66244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
784 ms |
80724 KB |
Output is correct |
2 |
Correct |
84 ms |
66200 KB |
Output is correct |
3 |
Correct |
670 ms |
80744 KB |
Output is correct |
4 |
Correct |
694 ms |
80752 KB |
Output is correct |
5 |
Correct |
639 ms |
80736 KB |
Output is correct |
6 |
Correct |
756 ms |
80688 KB |
Output is correct |
7 |
Correct |
773 ms |
80872 KB |
Output is correct |
8 |
Correct |
759 ms |
88912 KB |
Output is correct |
9 |
Correct |
667 ms |
88960 KB |
Output is correct |
10 |
Correct |
545 ms |
88872 KB |
Output is correct |
11 |
Correct |
669 ms |
88932 KB |
Output is correct |
12 |
Correct |
769 ms |
88860 KB |
Output is correct |
13 |
Correct |
424 ms |
88884 KB |
Output is correct |
14 |
Correct |
450 ms |
88800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
66316 KB |
Output is correct |
2 |
Correct |
40 ms |
66284 KB |
Output is correct |
3 |
Correct |
46 ms |
66316 KB |
Output is correct |
4 |
Correct |
38 ms |
66280 KB |
Output is correct |
5 |
Correct |
40 ms |
66308 KB |
Output is correct |
6 |
Correct |
29 ms |
66036 KB |
Output is correct |
7 |
Correct |
33 ms |
66244 KB |
Output is correct |
8 |
Correct |
260 ms |
70160 KB |
Output is correct |
9 |
Correct |
331 ms |
70160 KB |
Output is correct |
10 |
Correct |
257 ms |
70116 KB |
Output is correct |
11 |
Correct |
196 ms |
69564 KB |
Output is correct |
12 |
Correct |
288 ms |
70108 KB |
Output is correct |
13 |
Correct |
282 ms |
70164 KB |
Output is correct |
14 |
Correct |
259 ms |
70176 KB |
Output is correct |
15 |
Correct |
247 ms |
70092 KB |
Output is correct |
16 |
Correct |
272 ms |
70348 KB |
Output is correct |
17 |
Correct |
310 ms |
70156 KB |
Output is correct |
18 |
Correct |
141 ms |
70392 KB |
Output is correct |
19 |
Correct |
146 ms |
72756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
66316 KB |
Output is correct |
2 |
Correct |
40 ms |
66284 KB |
Output is correct |
3 |
Correct |
46 ms |
66316 KB |
Output is correct |
4 |
Correct |
38 ms |
66280 KB |
Output is correct |
5 |
Correct |
40 ms |
66308 KB |
Output is correct |
6 |
Correct |
29 ms |
66036 KB |
Output is correct |
7 |
Correct |
33 ms |
66244 KB |
Output is correct |
8 |
Correct |
784 ms |
80724 KB |
Output is correct |
9 |
Correct |
84 ms |
66200 KB |
Output is correct |
10 |
Correct |
670 ms |
80744 KB |
Output is correct |
11 |
Correct |
694 ms |
80752 KB |
Output is correct |
12 |
Correct |
639 ms |
80736 KB |
Output is correct |
13 |
Correct |
756 ms |
80688 KB |
Output is correct |
14 |
Correct |
773 ms |
80872 KB |
Output is correct |
15 |
Correct |
759 ms |
88912 KB |
Output is correct |
16 |
Correct |
667 ms |
88960 KB |
Output is correct |
17 |
Correct |
545 ms |
88872 KB |
Output is correct |
18 |
Correct |
669 ms |
88932 KB |
Output is correct |
19 |
Correct |
769 ms |
88860 KB |
Output is correct |
20 |
Correct |
424 ms |
88884 KB |
Output is correct |
21 |
Correct |
450 ms |
88800 KB |
Output is correct |
22 |
Correct |
260 ms |
70160 KB |
Output is correct |
23 |
Correct |
331 ms |
70160 KB |
Output is correct |
24 |
Correct |
257 ms |
70116 KB |
Output is correct |
25 |
Correct |
196 ms |
69564 KB |
Output is correct |
26 |
Correct |
288 ms |
70108 KB |
Output is correct |
27 |
Correct |
282 ms |
70164 KB |
Output is correct |
28 |
Correct |
259 ms |
70176 KB |
Output is correct |
29 |
Correct |
247 ms |
70092 KB |
Output is correct |
30 |
Correct |
272 ms |
70348 KB |
Output is correct |
31 |
Correct |
310 ms |
70156 KB |
Output is correct |
32 |
Correct |
141 ms |
70392 KB |
Output is correct |
33 |
Correct |
146 ms |
72756 KB |
Output is correct |
34 |
Correct |
32 ms |
65996 KB |
Output is correct |
35 |
Correct |
38 ms |
66072 KB |
Output is correct |
36 |
Correct |
890 ms |
86128 KB |
Output is correct |
37 |
Correct |
963 ms |
89228 KB |
Output is correct |
38 |
Correct |
453 ms |
85240 KB |
Output is correct |
39 |
Correct |
957 ms |
89108 KB |
Output is correct |
40 |
Correct |
902 ms |
86164 KB |
Output is correct |
41 |
Correct |
956 ms |
89124 KB |
Output is correct |
42 |
Correct |
883 ms |
86100 KB |
Output is correct |
43 |
Correct |
934 ms |
89072 KB |
Output is correct |
44 |
Correct |
968 ms |
89116 KB |
Output is correct |
45 |
Correct |
948 ms |
89096 KB |
Output is correct |
46 |
Correct |
790 ms |
89144 KB |
Output is correct |
47 |
Correct |
570 ms |
89292 KB |
Output is correct |