#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
const int N = 300'000;
int n, q, s[N + 10];
int type[N + 10], a[N + 10], b[N + 10];
int ans[N + 10], val[N + 10], typeQuery[N + 10];
int x1[N + 10], x2[N + 10], y1[N + 10], y2[N + 10];
void readInput() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
char c;
cin >> c;
s[i] = (c == '1');
}
for (int i = 1; i <= q; i++) {
string s;
cin >> s;
if (s[0] == 't') {
type[i] = 0;
cin >> a[i];
}
else {
type[i] = 1;
cin >> a[i] >> b[i];
b[i]--;
}
}
}
struct Segment {
set<int> s1[2], s2[2];
void init() {
for (int i = 1; i <= n; i++) {
s1[s[i]].insert(i);
s2[s[i]].insert(-i);
}
}
void toggle(int i) {
s1[s[i]].erase(i);
s2[s[i]].erase(-i);
s[i] ^= 1;
s1[s[i]].insert(i);
s2[s[i]].insert(-i);
}
int getNext(int idx, int t) {
auto au = s1[t].upper_bound(idx);
return au == s1[t].end()? n + 1: *au;
}
int getLast(int idx, int t) {
auto au = s2[t].upper_bound(-idx);
return au == s2[t].end()? 0: -(*au);
}
bool isPath(int a, int b) {
int k = getNext(a - 1, 0);
return b < k;
}
} seg;
struct Fenwick2D {
vector<int> fen[N + 10], vals[N + 10];
void updateRow(int x, int y, int val) {
int idx = lower_bound(vals[x].begin(), vals[x].end(), y) - vals[x].begin();
/*if (vals[x][idx] != y)
cout << "bad " << endl;*/
for (; idx < vals[x].size(); idx += idx & -idx)
fen[x][idx] += val;
}
void updatePoint(int x, int y, int val) {
if (x == 0 || y == 0)
return;
for (; x <= n; x += x & -x)
updateRow(x, y, val);
}
void update(int x1, int x2, int y1, int y2, int val) {
updatePoint(x2, y2, val);
updatePoint(x2, y1 - 1, -val);
updatePoint(x1 - 1, y2, -val);
updatePoint(x1 - 1, y1 - 1, val);
}
void addPoint(int x, int y) {
if (x == 0 || y == 0)
return;
int x2 = x;
for (; x <= n; x += x & -x)
vals[x].push_back(y);
x = x2;
for (; x; x -= x & -x)
vals[x].push_back(y);
}
void addQueryChange(int x1, int x2, int y1, int y2) {
addPoint(x2, y2);
addPoint(x2, y1 - 1);
addPoint(x1 - 1, y2);
addPoint(x1 - 1, y1 - 1);
}
void addQueryGet(int x, int y) {
addQueryChange(x, n, y, n);
}
void init() {
for (int i = 1; i <= n; i++) {
vals[i].push_back(0);
sort(vals[i].begin(), vals[i].end());
vals[i].resize(unique(vals[i].begin(), vals[i].end()) - vals[i].begin());
fen[i].resize(vals[i].size() + 3);
}
}
int getRow(int x, int idx) {
if (idx == 0)
return 0;
int t = idx;
idx = lower_bound(vals[x].begin(), vals[x].end(), idx) - vals[x].begin();
/*if (vals[x][idx] != t)
cout << "no " << x << ' ' << t << endl;*/
int res = 0;
for (; idx; idx -= idx & -idx)
res += fen[x][idx];
return res;
}
int getPoint(int x, int y) {
if (x == 0 || y == 0)
return 0;
int res = 0;
for (; x; x -= x & -x)
res += getRow(x, y);
return res;
}
int get(int x1, int x2, int y1, int y2) {
return getPoint(x2, y2) - getPoint(x2, y1 - 1) - getPoint(x1 - 1, y2) + getPoint(x1 - 1, y1 - 1);
}
int get(int x, int y) {
return get(x, n, y, n);
}
} fen;
void change(int idx, int idQuery, int d) {
int l = seg.getLast(idx, 0);
int r = seg.getNext(idx, 0);
fen.addQueryChange(l + 1, idx, idx, r - 1);
typeQuery[idQuery] = 0;
x1[idQuery] = l + 1;
x2[idQuery] = idx;
y1[idQuery] = idx;
y2[idQuery] = r - 1;
val[idQuery] = -d * idQuery;
//cout << l << ' ' << idx << ' ' << r << '\n';
}
void initQueryChange(int idx, int idQuery) {
if (!s[idx])
change(idx, idQuery, 1);
else
change(idx, idQuery, -1);
seg.toggle(idx);
}
void initQueryGet(int l, int r, int idQuery) {
typeQuery[idQuery] = 1;
x1[idQuery] = l;
y1[idQuery] = r;
val[idQuery] = (seg.isPath(l, r)? idQuery: 0);
fen.addQueryGet(l, r);
//cout << idQuery << ": " << seg.isPath(l, r) << '\n';
}
void init() {
seg.init();
for (int i = 1; i <= q; i++)
if (type[i] == 0)
initQueryChange(a[i], i);
else
initQueryGet(a[i], b[i], i);
fen.init();
}
void solve() {
for (int i = 1; i <= q; i++)
if (type[i] == 0)
fen.update(x1[i], x2[i], y1[i], y2[i], val[i]);
else
ans[i] = fen.get(x1[i], y1[i]) + val[i];
}
void writeOutput() {
for (int i = 1; i <= q; i++)
if (type[i])
cout << ans[i] << '\n';
cout.flush();
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
readInput();
init();
solve();
writeOutput();
return 0;
}
Compilation message
street_lamps.cpp: In member function 'void Fenwick2D::updateRow(int, int, int)':
street_lamps.cpp:71:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for (; idx < vals[x].size(); idx += idx & -idx)
| ~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp: In member function 'int Fenwick2D::getRow(int, int)':
street_lamps.cpp:116:13: warning: unused variable 't' [-Wunused-variable]
116 | int t = idx;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27284 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
5 ms |
27284 KB |
Output is correct |
7 |
Correct |
5 ms |
27224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
425 ms |
70024 KB |
Output is correct |
2 |
Correct |
580 ms |
77004 KB |
Output is correct |
3 |
Correct |
1034 ms |
98116 KB |
Output is correct |
4 |
Correct |
3213 ms |
191316 KB |
Output is correct |
5 |
Correct |
3022 ms |
193764 KB |
Output is correct |
6 |
Correct |
3074 ms |
191760 KB |
Output is correct |
7 |
Correct |
2509 ms |
180276 KB |
Output is correct |
8 |
Correct |
2174 ms |
187436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27484 KB |
Output is correct |
2 |
Correct |
8 ms |
27480 KB |
Output is correct |
3 |
Correct |
8 ms |
27484 KB |
Output is correct |
4 |
Correct |
8 ms |
27736 KB |
Output is correct |
5 |
Correct |
3488 ms |
206700 KB |
Output is correct |
6 |
Correct |
3318 ms |
198684 KB |
Output is correct |
7 |
Correct |
3008 ms |
197196 KB |
Output is correct |
8 |
Correct |
2408 ms |
186844 KB |
Output is correct |
9 |
Correct |
252 ms |
56656 KB |
Output is correct |
10 |
Correct |
278 ms |
66944 KB |
Output is correct |
11 |
Correct |
266 ms |
61856 KB |
Output is correct |
12 |
Correct |
2647 ms |
189380 KB |
Output is correct |
13 |
Correct |
2403 ms |
193168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27484 KB |
Output is correct |
2 |
Correct |
9 ms |
27624 KB |
Output is correct |
3 |
Correct |
8 ms |
27484 KB |
Output is correct |
4 |
Correct |
8 ms |
27672 KB |
Output is correct |
5 |
Correct |
2645 ms |
189520 KB |
Output is correct |
6 |
Correct |
2803 ms |
195292 KB |
Output is correct |
7 |
Correct |
2990 ms |
199260 KB |
Output is correct |
8 |
Correct |
3348 ms |
205680 KB |
Output is correct |
9 |
Correct |
661 ms |
83576 KB |
Output is correct |
10 |
Correct |
492 ms |
71244 KB |
Output is correct |
11 |
Correct |
648 ms |
83796 KB |
Output is correct |
12 |
Correct |
480 ms |
72092 KB |
Output is correct |
13 |
Correct |
642 ms |
84004 KB |
Output is correct |
14 |
Correct |
480 ms |
72144 KB |
Output is correct |
15 |
Correct |
2597 ms |
187540 KB |
Output is correct |
16 |
Correct |
2462 ms |
190080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
27224 KB |
Output is correct |
2 |
Correct |
5 ms |
27224 KB |
Output is correct |
3 |
Correct |
5 ms |
27284 KB |
Output is correct |
4 |
Correct |
5 ms |
27228 KB |
Output is correct |
5 |
Correct |
5 ms |
27228 KB |
Output is correct |
6 |
Correct |
5 ms |
27284 KB |
Output is correct |
7 |
Correct |
5 ms |
27224 KB |
Output is correct |
8 |
Correct |
425 ms |
70024 KB |
Output is correct |
9 |
Correct |
580 ms |
77004 KB |
Output is correct |
10 |
Correct |
1034 ms |
98116 KB |
Output is correct |
11 |
Correct |
3213 ms |
191316 KB |
Output is correct |
12 |
Correct |
3022 ms |
193764 KB |
Output is correct |
13 |
Correct |
3074 ms |
191760 KB |
Output is correct |
14 |
Correct |
2509 ms |
180276 KB |
Output is correct |
15 |
Correct |
2174 ms |
187436 KB |
Output is correct |
16 |
Correct |
8 ms |
27484 KB |
Output is correct |
17 |
Correct |
8 ms |
27480 KB |
Output is correct |
18 |
Correct |
8 ms |
27484 KB |
Output is correct |
19 |
Correct |
8 ms |
27736 KB |
Output is correct |
20 |
Correct |
3488 ms |
206700 KB |
Output is correct |
21 |
Correct |
3318 ms |
198684 KB |
Output is correct |
22 |
Correct |
3008 ms |
197196 KB |
Output is correct |
23 |
Correct |
2408 ms |
186844 KB |
Output is correct |
24 |
Correct |
252 ms |
56656 KB |
Output is correct |
25 |
Correct |
278 ms |
66944 KB |
Output is correct |
26 |
Correct |
266 ms |
61856 KB |
Output is correct |
27 |
Correct |
2647 ms |
189380 KB |
Output is correct |
28 |
Correct |
2403 ms |
193168 KB |
Output is correct |
29 |
Correct |
8 ms |
27484 KB |
Output is correct |
30 |
Correct |
9 ms |
27624 KB |
Output is correct |
31 |
Correct |
8 ms |
27484 KB |
Output is correct |
32 |
Correct |
8 ms |
27672 KB |
Output is correct |
33 |
Correct |
2645 ms |
189520 KB |
Output is correct |
34 |
Correct |
2803 ms |
195292 KB |
Output is correct |
35 |
Correct |
2990 ms |
199260 KB |
Output is correct |
36 |
Correct |
3348 ms |
205680 KB |
Output is correct |
37 |
Correct |
661 ms |
83576 KB |
Output is correct |
38 |
Correct |
492 ms |
71244 KB |
Output is correct |
39 |
Correct |
648 ms |
83796 KB |
Output is correct |
40 |
Correct |
480 ms |
72092 KB |
Output is correct |
41 |
Correct |
642 ms |
84004 KB |
Output is correct |
42 |
Correct |
480 ms |
72144 KB |
Output is correct |
43 |
Correct |
2597 ms |
187540 KB |
Output is correct |
44 |
Correct |
2462 ms |
190080 KB |
Output is correct |
45 |
Correct |
150 ms |
42708 KB |
Output is correct |
46 |
Correct |
291 ms |
52172 KB |
Output is correct |
47 |
Correct |
1555 ms |
113136 KB |
Output is correct |
48 |
Correct |
3293 ms |
200556 KB |
Output is correct |
49 |
Correct |
290 ms |
62868 KB |
Output is correct |
50 |
Correct |
308 ms |
68456 KB |
Output is correct |
51 |
Correct |
312 ms |
64724 KB |
Output is correct |
52 |
Correct |
474 ms |
79420 KB |
Output is correct |
53 |
Correct |
486 ms |
80984 KB |
Output is correct |
54 |
Correct |
477 ms |
81588 KB |
Output is correct |