#include<bits/stdc++.h>
using namespace std;
#define fori(i, l, r) for(int i = (int) (l); i <= (int) (r); i++)
#define ford(i, r, l) for(int i = (int) (r); i >= (int) (l); i--)
#define ii pair<int, int>
#define fi first
#define se second
#define vi vector<int>
#define eb emplace_back
#define em emplace
#define sp ' '
#define endl '\n'
//#define int long long
const int maxn = 5e5 + 5;
int n, q;
char c[maxn];
string tp[maxn];
int id[maxn];
bool state[maxn];
int ans[maxn];
struct it {
vi mn, mx; // min, max of off ones
it (int len= 1) {
mn = vi(4 * len + 5, n);
mx = vi(4 * len + 5, 0);
build(1, 1, len);
}
void build(int u, int l, int r) {
if(l == r) mn[u] = mx[u] = l;
else {
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
mn[u] = l;
mx[u] = r;
}
}
void sett(int u, int l, int r, int p, bool on) {
if(l > p or p > r) return;
if(l == r) {
mn[u] = !on? p: n;
mx[u] = !on? p: 0;
}
else {
int mid = l + r >> 1;
sett(u << 1, l, mid, p, on);
sett(u << 1 | 1, mid + 1, r , p, on);
mn[u] = min(mn[u << 1], mn[u << 1 | 1]);
mx[u] = max(mx[u << 1], mx[u << 1 | 1]);
}
}
int get_min(int u, int l, int r, int ll, int rr) {
if(l > rr or ll > r) return n;
if(ll <= l and r <= rr) return mn[u];
int mid = l + r >> 1;
return min(get_min(u << 1, l, mid, ll, rr), get_min(u << 1 | 1, mid + 1, r, ll, rr));
}
int get_max(int u, int l, int r, int ll, int rr) {
if(l > rr or ll > r) return 0;
if(ll <= l and r <= rr) return mx[u];
int mid = l + r >> 1;
return max(get_max(u << 1, l, mid, ll, rr), get_max(u << 1 | 1, mid + 1, r, ll, rr));
}
} IT;
struct bit {
vector<pair<ii, ii> > qrs;
vector<vi> vals;
vector<vi> sum;
vector<int> res;
bit(int len = 0) {
vals.resize(len + 10);
sum.resize(len + 10);
}
void add(int x, int y, int v) {
qrs.eb(ii(0, v), ii(x, y));
for(int i = x; i < sum.size(); i += i & -i) {
vals[i].eb(y);
}
}
void get(int x, int y) {
qrs.eb(ii(1, 0), ii(x, y));
for(int i = x; i > 0; i -= i & -i) {
vals[i].eb(y);
}
}
void real_add(int x, int y, int v) {
for(int i = x; i < sum.size(); i += i &-i) {
for(int j = lower_bound(begin(vals[i]), end(vals[i]), y) - begin(vals[i]) + 1; j > 0; j -= j & -j) {
sum[i][j] += v;
}
}
}
int real_get(int x, int y) {
int r = 0;
for(int i = x; i > 0; i -= i &-i) {
for(int j = lower_bound(begin(vals[i]), end(vals[i]), y) - begin(vals[i]) + 1; j < sum[i].size(); j += j & -j) {
r += sum[i][j];
}
}
return r;
}
void process() {
fori(i, 1, sum.size() - 1) {
sort(begin(vals[i]),end(vals[i]));
sum[i].resize(vals[i].size() + 5);
}
for(auto t: qrs) {
if(t.fi.fi == 0) {
int v= t.fi.se;
int x = t.se.fi, y = t.se.se;
real_add(x, y, v);
}
else {
int x = t.se.fi, y = t.se.se;
res.eb(real_get(x, y));
}
}
}
} BIT;
void turn_on(int i, int t) {
int L = IT.get_max(1, 1, n - 1, 1, i - 1) + 1;
int R = IT.get_min(1, 1, n - 1, i + 1, n - 1) - 1;
IT.sett(1, 1, n - 1, i, 1);
BIT.add(L, R + 1, -t);
BIT.add(L, i, t);
BIT.add(i + 1, R + 1, t);
state[i] = 1;
}
void turn_off(int i, int t) {
int L = IT.get_max(1, 1, n - 1, 1, i - 1) + 1;
int R = IT.get_min(1, 1, n - 1, i + 1, n - 1) - 1;
IT.sett(1, 1, n - 1, i, 0);
BIT.add(L, R + 1, t);
BIT.add(L, i, -t);
BIT.add(i + 1, R + 1, -t);
state[i] = 0;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
n++;
IT = it(n - 1);
BIT = bit(n);
fori(i, 1, n - 1) {
cin >> c[i];
if(c[i] == '1') turn_on(i, 0);
}
fori(i, 1, q) {
cin >> tp[i];
if(tp[i] == "toggle") {
cin >> id[i];
if(state[id[i]] == 0) turn_on(id[i], i);
else if(state[id[i]] == 1) turn_off(id[i], i);
}
else {
int l, r;
cin >> l >> r;
BIT.get(l, r);
if(IT.get_min(1, 1, n - 1, l, r - 1) >= r) ans[i] += i;
}
}
BIT.process();
auto temp = BIT.res;
// cout << temp.size() << endl;
int pt = 0;
fori(i, 1, q) {
if(tp[i] == "query") {
ans[i] += temp[pt++];
cout << ans[i] << endl;
}
}
}
Compilation message
street_lamps.cpp: In member function 'void it::build(int, int, int)':
street_lamps.cpp:32:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
32 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In member function 'void it::sett(int, int, int, int, bool)':
street_lamps.cpp:46:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In member function 'int it::get_min(int, int, int, int, int)':
street_lamps.cpp:57:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In member function 'int it::get_max(int, int, int, int, int)':
street_lamps.cpp:63:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int mid = l + r >> 1;
| ~~^~~
street_lamps.cpp: In member function 'void bit::add(int, int, int)':
street_lamps.cpp:82:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = x; i < sum.size(); i += i & -i) {
| ~~^~~~~~~~~~~~
street_lamps.cpp: In member function 'void bit::real_add(int, int, int)':
street_lamps.cpp:94:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i = x; i < sum.size(); i += i &-i) {
| ~~^~~~~~~~~~~~
street_lamps.cpp: In member function 'int bit::real_get(int, int)':
street_lamps.cpp:104:85: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int j = lower_bound(begin(vals[i]), end(vals[i]), y) - begin(vals[i]) + 1; j < sum[i].size(); j += j & -j) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
11 ms |
15908 KB |
Output is correct |
3 |
Correct |
10 ms |
15928 KB |
Output is correct |
4 |
Correct |
9 ms |
16084 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
530 ms |
51236 KB |
Output is correct |
2 |
Correct |
768 ms |
53768 KB |
Output is correct |
3 |
Correct |
1197 ms |
64848 KB |
Output is correct |
4 |
Correct |
2623 ms |
163436 KB |
Output is correct |
5 |
Correct |
2584 ms |
166204 KB |
Output is correct |
6 |
Correct |
2642 ms |
168064 KB |
Output is correct |
7 |
Correct |
1132 ms |
89316 KB |
Output is correct |
8 |
Correct |
3896 ms |
231344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16248 KB |
Output is correct |
2 |
Correct |
12 ms |
16340 KB |
Output is correct |
3 |
Correct |
12 ms |
16356 KB |
Output is correct |
4 |
Correct |
13 ms |
16480 KB |
Output is correct |
5 |
Correct |
2599 ms |
150284 KB |
Output is correct |
6 |
Correct |
2652 ms |
156624 KB |
Output is correct |
7 |
Correct |
2551 ms |
167168 KB |
Output is correct |
8 |
Correct |
3859 ms |
231784 KB |
Output is correct |
9 |
Correct |
194 ms |
30384 KB |
Output is correct |
10 |
Correct |
237 ms |
32748 KB |
Output is correct |
11 |
Correct |
207 ms |
32864 KB |
Output is correct |
12 |
Correct |
1087 ms |
87188 KB |
Output is correct |
13 |
Correct |
3791 ms |
231588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
16200 KB |
Output is correct |
2 |
Correct |
13 ms |
16272 KB |
Output is correct |
3 |
Correct |
11 ms |
16384 KB |
Output is correct |
4 |
Correct |
12 ms |
16376 KB |
Output is correct |
5 |
Correct |
1839 ms |
134424 KB |
Output is correct |
6 |
Correct |
2157 ms |
151464 KB |
Output is correct |
7 |
Correct |
2716 ms |
167736 KB |
Output is correct |
8 |
Correct |
3372 ms |
203316 KB |
Output is correct |
9 |
Correct |
898 ms |
58492 KB |
Output is correct |
10 |
Correct |
763 ms |
63720 KB |
Output is correct |
11 |
Correct |
879 ms |
58404 KB |
Output is correct |
12 |
Correct |
720 ms |
63596 KB |
Output is correct |
13 |
Correct |
920 ms |
58420 KB |
Output is correct |
14 |
Correct |
732 ms |
63916 KB |
Output is correct |
15 |
Correct |
1114 ms |
86380 KB |
Output is correct |
16 |
Correct |
3955 ms |
230688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
11 ms |
15908 KB |
Output is correct |
3 |
Correct |
10 ms |
15928 KB |
Output is correct |
4 |
Correct |
9 ms |
16084 KB |
Output is correct |
5 |
Correct |
9 ms |
15956 KB |
Output is correct |
6 |
Correct |
8 ms |
15956 KB |
Output is correct |
7 |
Correct |
9 ms |
15956 KB |
Output is correct |
8 |
Correct |
530 ms |
51236 KB |
Output is correct |
9 |
Correct |
768 ms |
53768 KB |
Output is correct |
10 |
Correct |
1197 ms |
64848 KB |
Output is correct |
11 |
Correct |
2623 ms |
163436 KB |
Output is correct |
12 |
Correct |
2584 ms |
166204 KB |
Output is correct |
13 |
Correct |
2642 ms |
168064 KB |
Output is correct |
14 |
Correct |
1132 ms |
89316 KB |
Output is correct |
15 |
Correct |
3896 ms |
231344 KB |
Output is correct |
16 |
Correct |
11 ms |
16248 KB |
Output is correct |
17 |
Correct |
12 ms |
16340 KB |
Output is correct |
18 |
Correct |
12 ms |
16356 KB |
Output is correct |
19 |
Correct |
13 ms |
16480 KB |
Output is correct |
20 |
Correct |
2599 ms |
150284 KB |
Output is correct |
21 |
Correct |
2652 ms |
156624 KB |
Output is correct |
22 |
Correct |
2551 ms |
167168 KB |
Output is correct |
23 |
Correct |
3859 ms |
231784 KB |
Output is correct |
24 |
Correct |
194 ms |
30384 KB |
Output is correct |
25 |
Correct |
237 ms |
32748 KB |
Output is correct |
26 |
Correct |
207 ms |
32864 KB |
Output is correct |
27 |
Correct |
1087 ms |
87188 KB |
Output is correct |
28 |
Correct |
3791 ms |
231588 KB |
Output is correct |
29 |
Correct |
12 ms |
16200 KB |
Output is correct |
30 |
Correct |
13 ms |
16272 KB |
Output is correct |
31 |
Correct |
11 ms |
16384 KB |
Output is correct |
32 |
Correct |
12 ms |
16376 KB |
Output is correct |
33 |
Correct |
1839 ms |
134424 KB |
Output is correct |
34 |
Correct |
2157 ms |
151464 KB |
Output is correct |
35 |
Correct |
2716 ms |
167736 KB |
Output is correct |
36 |
Correct |
3372 ms |
203316 KB |
Output is correct |
37 |
Correct |
898 ms |
58492 KB |
Output is correct |
38 |
Correct |
763 ms |
63720 KB |
Output is correct |
39 |
Correct |
879 ms |
58404 KB |
Output is correct |
40 |
Correct |
720 ms |
63596 KB |
Output is correct |
41 |
Correct |
920 ms |
58420 KB |
Output is correct |
42 |
Correct |
732 ms |
63916 KB |
Output is correct |
43 |
Correct |
1114 ms |
86380 KB |
Output is correct |
44 |
Correct |
3955 ms |
230688 KB |
Output is correct |
45 |
Correct |
179 ms |
36004 KB |
Output is correct |
46 |
Correct |
319 ms |
37556 KB |
Output is correct |
47 |
Correct |
1304 ms |
83084 KB |
Output is correct |
48 |
Correct |
2604 ms |
162832 KB |
Output is correct |
49 |
Correct |
233 ms |
33592 KB |
Output is correct |
50 |
Correct |
240 ms |
33536 KB |
Output is correct |
51 |
Correct |
245 ms |
34088 KB |
Output is correct |
52 |
Correct |
268 ms |
36280 KB |
Output is correct |
53 |
Correct |
273 ms |
36260 KB |
Output is correct |
54 |
Correct |
267 ms |
36184 KB |
Output is correct |