#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using db = long double;
using ll = long long;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define vt vector
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define size(x) ((int) (x).size())
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define F0R(i, b) FOR (i, 0, b)
#define endl '\n'
const ll INF = 1e18;
const int inf = 1e9;
namespace treap {
using ptr = struct Node*;
struct Node {
ptr l, r;
int i;
ll val, lazy;
int lo, hi;
Node(int i, ptr l, ptr r) : i(i), l(l), r(r), lo(i), hi(i) {
val = lazy = 0;
if (l) lo = l->lo;
if (r) hi = r->hi;
}
};
void inc(ptr n, int lo, int hi, ll v) {
if (!n) return;
if (lo > n->hi || hi < n->lo) return;
if (lo <= n->lo && n->hi <= hi) {
n->lazy += v;
return;
}
if (lo <= n->i && n->i <= hi) n->val += v;
inc(n->l, lo, hi, v);
inc(n->r, lo, hi, v);
}
ll query(ptr n, int i) {
if (n->i == i) return n->val + n->lazy;
if (i <= n->i) return n->lazy + query(n->l, i);
else return n->lazy + query(n->r, i);
}
ptr build(vt<int>& a, int l, int r) {
if (l > r) return 0;
int m = (l + r) / 2;
return new Node(a[m], build(a, l, m - 1), build(a, m + 1, r));
}
}
struct SegTree {
int n;
vt<vt<int>> todo;
vt<treap::ptr> seg;
void init(int _n) {
for (n = 1; n < _n; n *= 2);
todo.resize(2 * n);
seg.resize(2 * n);
}
void load(int l, int r) {
todo[l += n].pb(r);
while (l /= 2) todo[l].pb(r);
}
void build() {
FOR (i, 1, 2 * n) {
seg[i] = treap::build(todo[i], 0, size(todo[i]) - 1);
}
}
void upd(int l1, int l2, int r1, int r2, int v) {
for (l1 += n, l2 += n + 1; l1 < l2; l1 /= 2, l2 /= 2) {
if (l1 & 1) treap::inc(seg[l1++], r1, r2, v);
if (l2 & 1) treap::inc(seg[--l2], r1, r2, v);
}
}
ll query(int l, int r) {
ll res = treap::query(seg[l += n], r);
while (l /= 2) res += treap::query(seg[l], r);
return res;
}
};
struct BIT {
int n; vt<ll> arr;
void init(int _n) {
n = _n;
arr.resize(n);
}
ll sum(int r) {
ll s = 0;
while (r) {
s += arr[r - 1];
r -= r & -r;
}
return s;
}
void add(int p, ll x) {
for (++p; p <= n; p += p & -p) arr[p - 1] += x;
}
ll sum(int l, int r) {
return sum(r + 1) - sum(l);
}
};
main() {
cin.tie(0)->sync_with_stdio(0);
int n, q; cin >> n >> q;
vt<int> state(n);
BIT bit;
bit.init(n);
F0R (i, n) {
char c; cin >> c;
state[i] = (c == '1');
}
set<int> dead {-1, n};
vt<pi> queries(q);
vt<pi> pts;
F0R (i, q) {
string t; cin >> t;
if (t == "query") {
int l, r; cin >> l >> r; l--, r--;
queries[i] = {l, r - 1};
pts.pb({r - 1, l});
} else if (t == "toggle") {
int j; cin >> j;
queries[i] = {j - 1, -1};
} else assert(0);
}
sort(all(pts));
pts.erase(unique(all(pts)), pts.end());
SegTree seg;
seg.init(n);
for (auto [r, l] : pts) seg.load(l, r);
seg.build();
auto enable = [&] (int i, int t) {
state[i] = true;
dead.erase(i);
bit.add(i, 1);
if (t) {
auto it = dead.lower_bound(i);
int nxt = *it, prv = *prev(it);
seg.upd(prv + 1, i, i, nxt - 1, -t);
}
};
auto disable = [&] (int i, int t) {
if (t) bit.add(i, -1);
auto it = dead.lower_bound(i);
int nxt = *it;
int prv = *prev(it);
seg.upd(prv + 1, i, i, nxt - 1, t);
state[i] = false;
dead.insert(i);
};
F0R (i, n) {
if (state[i]) enable(i, 0);
else disable(i, 0);
}
int t = 0;
for (auto [x, y] : queries) {
t++;
if (y == -1) {
if (state[x] == 0) {
enable(x, t);
} else {
disable(x, t);
}
} else {
if (bit.sum(x, y) == y - x + 1) {
cout << seg.query(x, y) + t << endl;
} else {
cout << seg.query(x, y) << endl;
}
}
}
}
Compilation message
street_lamps.cpp: In constructor 'treap::Node::Node(int, treap::ptr, treap::ptr)':
street_lamps.cpp:29:13: warning: 'treap::Node::i' will be initialized after [-Wreorder]
29 | int i;
| ^
street_lamps.cpp:28:13: warning: 'treap::Node* treap::Node::l' [-Wreorder]
28 | ptr l, r;
| ^
street_lamps.cpp:33:9: warning: when initialized here [-Wreorder]
33 | Node(int i, ptr l, ptr r) : i(i), l(l), r(r), lo(i), hi(i) {
| ^~~~
street_lamps.cpp: At global scope:
street_lamps.cpp:118:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
118 | main() {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
8136 KB |
Output is correct |
2 |
Correct |
96 ms |
8644 KB |
Output is correct |
3 |
Correct |
194 ms |
14532 KB |
Output is correct |
4 |
Correct |
1184 ms |
220616 KB |
Output is correct |
5 |
Correct |
1486 ms |
243584 KB |
Output is correct |
6 |
Correct |
896 ms |
193996 KB |
Output is correct |
7 |
Correct |
2260 ms |
330012 KB |
Output is correct |
8 |
Correct |
2083 ms |
316992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
1112 KB |
Output is correct |
5 |
Correct |
269 ms |
62280 KB |
Output is correct |
6 |
Correct |
856 ms |
182252 KB |
Output is correct |
7 |
Correct |
1789 ms |
302736 KB |
Output is correct |
8 |
Correct |
2655 ms |
462792 KB |
Output is correct |
9 |
Correct |
84 ms |
10636 KB |
Output is correct |
10 |
Correct |
92 ms |
10692 KB |
Output is correct |
11 |
Correct |
102 ms |
14784 KB |
Output is correct |
12 |
Correct |
2774 ms |
475328 KB |
Output is correct |
13 |
Correct |
2744 ms |
463272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
860 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
2694 ms |
464452 KB |
Output is correct |
6 |
Correct |
1749 ms |
345296 KB |
Output is correct |
7 |
Correct |
1014 ms |
221240 KB |
Output is correct |
8 |
Correct |
251 ms |
55088 KB |
Output is correct |
9 |
Correct |
181 ms |
61068 KB |
Output is correct |
10 |
Correct |
82 ms |
8528 KB |
Output is correct |
11 |
Correct |
184 ms |
61132 KB |
Output is correct |
12 |
Correct |
78 ms |
8536 KB |
Output is correct |
13 |
Correct |
181 ms |
61132 KB |
Output is correct |
14 |
Correct |
85 ms |
8528 KB |
Output is correct |
15 |
Correct |
2798 ms |
475300 KB |
Output is correct |
16 |
Correct |
2690 ms |
462844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
82 ms |
8136 KB |
Output is correct |
9 |
Correct |
96 ms |
8644 KB |
Output is correct |
10 |
Correct |
194 ms |
14532 KB |
Output is correct |
11 |
Correct |
1184 ms |
220616 KB |
Output is correct |
12 |
Correct |
1486 ms |
243584 KB |
Output is correct |
13 |
Correct |
896 ms |
193996 KB |
Output is correct |
14 |
Correct |
2260 ms |
330012 KB |
Output is correct |
15 |
Correct |
2083 ms |
316992 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
2 ms |
1112 KB |
Output is correct |
20 |
Correct |
269 ms |
62280 KB |
Output is correct |
21 |
Correct |
856 ms |
182252 KB |
Output is correct |
22 |
Correct |
1789 ms |
302736 KB |
Output is correct |
23 |
Correct |
2655 ms |
462792 KB |
Output is correct |
24 |
Correct |
84 ms |
10636 KB |
Output is correct |
25 |
Correct |
92 ms |
10692 KB |
Output is correct |
26 |
Correct |
102 ms |
14784 KB |
Output is correct |
27 |
Correct |
2774 ms |
475328 KB |
Output is correct |
28 |
Correct |
2744 ms |
463272 KB |
Output is correct |
29 |
Correct |
2 ms |
1116 KB |
Output is correct |
30 |
Correct |
1 ms |
1116 KB |
Output is correct |
31 |
Correct |
1 ms |
860 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
2694 ms |
464452 KB |
Output is correct |
34 |
Correct |
1749 ms |
345296 KB |
Output is correct |
35 |
Correct |
1014 ms |
221240 KB |
Output is correct |
36 |
Correct |
251 ms |
55088 KB |
Output is correct |
37 |
Correct |
181 ms |
61068 KB |
Output is correct |
38 |
Correct |
82 ms |
8528 KB |
Output is correct |
39 |
Correct |
184 ms |
61132 KB |
Output is correct |
40 |
Correct |
78 ms |
8536 KB |
Output is correct |
41 |
Correct |
181 ms |
61132 KB |
Output is correct |
42 |
Correct |
85 ms |
8528 KB |
Output is correct |
43 |
Correct |
2798 ms |
475300 KB |
Output is correct |
44 |
Correct |
2690 ms |
462844 KB |
Output is correct |
45 |
Correct |
45 ms |
5328 KB |
Output is correct |
46 |
Correct |
79 ms |
7712 KB |
Output is correct |
47 |
Correct |
713 ms |
142652 KB |
Output is correct |
48 |
Correct |
1427 ms |
262092 KB |
Output is correct |
49 |
Correct |
98 ms |
13248 KB |
Output is correct |
50 |
Correct |
102 ms |
12500 KB |
Output is correct |
51 |
Correct |
118 ms |
14784 KB |
Output is correct |
52 |
Correct |
236 ms |
94392 KB |
Output is correct |
53 |
Correct |
207 ms |
95536 KB |
Output is correct |
54 |
Correct |
208 ms |
94828 KB |
Output is correct |