#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mid ((l + r) >> 1)
#define lc (id << 1)
#define rc (lc | 1)
#define all(x) x.begin(), x.end()
#define bug(x) cout << #x << " : " << x << '\n'
#define ios ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 3e5 + 10;
const int mod = 1e9 + 7;
const int inf = 1e9 + 10;
struct node{
ll ans;
int L, R;
ll l1, l2, r1, r2;
int len;
};
node operator + (node l, node r){
if(l.ans == -1) return r;
if(r.ans == -1) return l;
node S;
S.len = l.len + r.len;
S.l1 = l.l1, S.r1 = r.r1;
if(l.len > 1)
S.l2 = l.l2;
else
S.l2 = r.l1;
if(r.len > 1)
S.r2 = r.r2;
else
S.r2 = l.r1;
S.L = l.L;
if(l.L == l.len){
if(l.r1 != r.l1 and (l.len == 1 or (l.r2 < l.r1) != (l.r1 < r.l1))){
S.L ++;
if(r.len == 1 or (l.r1 < r.l1) != (r.l1 < r.l2)){
S.L = l.L + r.L;
}
}
}
S.R = r.R;
if(r.R == r.len){
if(r.l1 != l.r1 and (r.len == 1 or (l.r1 < r.l1) != (r.l1 < r.l2))){
S.R ++;
if(l.len == 1 or (l.r2 < l.r1) != (l.r1 < r.l1)){
S.R = r.R + l.R;
}
}
}
S.ans = l.ans + r.ans;
if(l.r1 != r.l1){
int valL = 1, valR = 1;
if(r.len == 1 or (l.r1 < r.l1) != (r.l1 < r.l2))
valR = r.L;
if(l.len == 1 or (l.r2 < l.r1) != (l.r1 < r.l1))
valL = l.R;
S.ans += 1ll * valL * valR;
}
return S;
}
int a[maxn];
pair<int, ll> lz[maxn << 2];
node seg[maxn << 2];
void build(int id, int l, int r){
seg[id].ans = seg[id].len = r - l;
seg[id].L = seg[id].R = 1;
lz[id] = mp(1, 0);
if(l + 1 == r)
return;
build(lc, l, mid);
build(rc, mid, r);
}
void shift(int id, int l, int r);
void update(int id, int l, int r, int ql, int qr, bool t, ll x){
if(qr <= l or r <= ql)
return;
if(l >= ql and r <= qr){
if(t){
seg[id].l1 *= -1;
seg[id].r1 *= -1;
if(seg[id].len > 1){
seg[id].l2 *= -1;
seg[id].r2 *= -1;
}
lz[id].fi *= -1;
lz[id].se *= -1;
}
else{
seg[id].l1 += x;
seg[id].r1 += x;
if(seg[id].len > 1){
seg[id].l2 += x;
seg[id].r2 += x;
}
lz[id].se += x;
}
return;
}
shift(id, l, r);
update(lc, l, mid, ql, qr, t, x);
update(rc, mid, r, ql, qr, t, x);
seg[id] = seg[lc] + seg[rc];
}
void shift(int id, int l, int r){
if(lz[id].fi == -1){
update(lc, l, mid, l, mid, 1, -1);
update(rc, mid, r, mid, r, 1, -1);
lz[id].fi = 1;
}
if(lz[id].se){
update(lc, l, mid, l, mid, 0, lz[id].se);
update(rc, mid, r, mid, r, 0, lz[id].se);
lz[id].se = 0;
}
}
node get(int id, int l, int r, int ql, int qr){
if(qr <= l or r <= ql)
return node{-1, -1, -1, -1, -1, -1, -1};
if(l >= ql and r <= qr)
return seg[id];
shift(id, l, r);
node L = get(lc, l, mid, ql, qr);
node R = get(rc, mid, r, ql, qr);
/* if(id == -1){
bug(L.ans), bug(L.L), bug(L.R), bug(L.l1), bug(L.l2), bug(L.r1), bug(L.r2), bug(L.len);
bug(R.ans), bug(R.L), bug(R.R), bug(R.l1), bug(R.l2), bug(R.r1), bug(R.r2), bug(R.len);
}*/
return L + R;
}
int main(){
ios;
int n, q;
cin >> n >> q;
build(1, 0, n);
for(int i = 0; i < n; i ++){
cin >> a[i];
update(1, 0, n, i, i + 1, 0, a[i]);
}
while(q--){
char c;
int l, r;
cin >> c >> l >> r;
l --;
if(c == '+'){
int x; cin >> x;
update(1, 0, n, l, r, 0, x);
}
if(c == '*'){
update(1, 0, n, l, r, 1, -1);
}
if(c == '?'){
cout << get(1, 0, n, l, r).ans << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1520 KB |
Output is correct |
2 |
Correct |
11 ms |
1528 KB |
Output is correct |
3 |
Correct |
10 ms |
1604 KB |
Output is correct |
4 |
Correct |
10 ms |
1620 KB |
Output is correct |
5 |
Correct |
9 ms |
1604 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
1640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
834 ms |
75980 KB |
Output is correct |
2 |
Correct |
51 ms |
456 KB |
Output is correct |
3 |
Correct |
667 ms |
76176 KB |
Output is correct |
4 |
Correct |
858 ms |
84208 KB |
Output is correct |
5 |
Correct |
756 ms |
84300 KB |
Output is correct |
6 |
Correct |
783 ms |
84352 KB |
Output is correct |
7 |
Correct |
871 ms |
81144 KB |
Output is correct |
8 |
Correct |
859 ms |
84224 KB |
Output is correct |
9 |
Correct |
748 ms |
84300 KB |
Output is correct |
10 |
Correct |
566 ms |
84312 KB |
Output is correct |
11 |
Correct |
740 ms |
84344 KB |
Output is correct |
12 |
Correct |
837 ms |
84352 KB |
Output is correct |
13 |
Correct |
421 ms |
84268 KB |
Output is correct |
14 |
Correct |
424 ms |
84184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1520 KB |
Output is correct |
2 |
Correct |
11 ms |
1528 KB |
Output is correct |
3 |
Correct |
10 ms |
1604 KB |
Output is correct |
4 |
Correct |
10 ms |
1620 KB |
Output is correct |
5 |
Correct |
9 ms |
1604 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
1640 KB |
Output is correct |
8 |
Correct |
304 ms |
21044 KB |
Output is correct |
9 |
Correct |
292 ms |
21068 KB |
Output is correct |
10 |
Correct |
314 ms |
22156 KB |
Output is correct |
11 |
Correct |
185 ms |
21708 KB |
Output is correct |
12 |
Correct |
299 ms |
22092 KB |
Output is correct |
13 |
Correct |
303 ms |
22120 KB |
Output is correct |
14 |
Correct |
263 ms |
22100 KB |
Output is correct |
15 |
Correct |
286 ms |
21076 KB |
Output is correct |
16 |
Correct |
321 ms |
22008 KB |
Output is correct |
17 |
Correct |
265 ms |
22052 KB |
Output is correct |
18 |
Correct |
130 ms |
22068 KB |
Output is correct |
19 |
Correct |
132 ms |
22004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
1520 KB |
Output is correct |
2 |
Correct |
11 ms |
1528 KB |
Output is correct |
3 |
Correct |
10 ms |
1604 KB |
Output is correct |
4 |
Correct |
10 ms |
1620 KB |
Output is correct |
5 |
Correct |
9 ms |
1604 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
8 ms |
1640 KB |
Output is correct |
8 |
Correct |
834 ms |
75980 KB |
Output is correct |
9 |
Correct |
51 ms |
456 KB |
Output is correct |
10 |
Correct |
667 ms |
76176 KB |
Output is correct |
11 |
Correct |
858 ms |
84208 KB |
Output is correct |
12 |
Correct |
756 ms |
84300 KB |
Output is correct |
13 |
Correct |
783 ms |
84352 KB |
Output is correct |
14 |
Correct |
871 ms |
81144 KB |
Output is correct |
15 |
Correct |
859 ms |
84224 KB |
Output is correct |
16 |
Correct |
748 ms |
84300 KB |
Output is correct |
17 |
Correct |
566 ms |
84312 KB |
Output is correct |
18 |
Correct |
740 ms |
84344 KB |
Output is correct |
19 |
Correct |
837 ms |
84352 KB |
Output is correct |
20 |
Correct |
421 ms |
84268 KB |
Output is correct |
21 |
Correct |
424 ms |
84184 KB |
Output is correct |
22 |
Correct |
304 ms |
21044 KB |
Output is correct |
23 |
Correct |
292 ms |
21068 KB |
Output is correct |
24 |
Correct |
314 ms |
22156 KB |
Output is correct |
25 |
Correct |
185 ms |
21708 KB |
Output is correct |
26 |
Correct |
299 ms |
22092 KB |
Output is correct |
27 |
Correct |
303 ms |
22120 KB |
Output is correct |
28 |
Correct |
263 ms |
22100 KB |
Output is correct |
29 |
Correct |
286 ms |
21076 KB |
Output is correct |
30 |
Correct |
321 ms |
22008 KB |
Output is correct |
31 |
Correct |
265 ms |
22052 KB |
Output is correct |
32 |
Correct |
130 ms |
22068 KB |
Output is correct |
33 |
Correct |
132 ms |
22004 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1137 ms |
81388 KB |
Output is correct |
37 |
Correct |
1096 ms |
84488 KB |
Output is correct |
38 |
Correct |
630 ms |
83472 KB |
Output is correct |
39 |
Correct |
1089 ms |
84596 KB |
Output is correct |
40 |
Correct |
1063 ms |
81660 KB |
Output is correct |
41 |
Correct |
946 ms |
84596 KB |
Output is correct |
42 |
Correct |
981 ms |
81516 KB |
Output is correct |
43 |
Correct |
1165 ms |
84656 KB |
Output is correct |
44 |
Correct |
1091 ms |
84576 KB |
Output is correct |
45 |
Correct |
1085 ms |
84576 KB |
Output is correct |
46 |
Correct |
967 ms |
84452 KB |
Output is correct |
47 |
Correct |
664 ms |
84796 KB |
Output is correct |