#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;
typedef long long llong;
typedef pair<int, int> pii;
void initv(vector<int> &v) {
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
struct fen1D {
vector<llong> fen;
vector<int> cp;
void add(int x) {
cp.push_back(x);
}
void init() {
initv(cp);
fen.resize(cp.size(), 0);
}
int find(int x) const {
return lower_bound(cp.begin(), cp.end(), x) - cp.begin() + 1;
}
void update(int i, llong x) {
i = find(i);
while (i <= cp.size()) {
fen[i - 1] += x;
i += i & -i;
}
}
llong query(int i) const {
i = find(i);
llong ret = 0;
while (i) {
ret += fen[i - 1];
i -= i & -i;
}
return ret;
}
};
struct fen2D {
vector<fen1D> fen;
vector<int> cp;
void add1(int x) {
cp.push_back(x);
}
void init1() {
initv(cp);
fen.resize(cp.size());
}
int find(int x) const {
return lower_bound(cp.begin(), cp.end(), x) - cp.begin() + 1;
}
void add2(int x, int y) {
x = find(x);
while (x) {
fen[x - 1].add(y);
x -= x & -x;
}
}
void init2() {
for (fen1D &i : fen) i.init();
}
void update(int i, int j, llong x) {
i = find(i);
while (i <= cp.size()) {
fen[i - 1].update(j, x);
i += i & -i;
}
}
llong query(int i, int j) const {
i = find(i);
llong ret = 0;
while (i) {
ret += fen[i - 1].query(j);
i -= i & -i;
}
return ret;
}
} fenM, fenB;
int n, q;
char in[300001];
int S[300001];
int A[300001];
int B[300001];
int L[300002];
int R[300002];
set<int> Rs;
void update(int x, int y, int v, int t) {
fenM.update(x, -y, v);
fenB.update(x, -y, -t * v);
}
void add(int x, int t) {
int l = L[x], r = R[x + 1];
update(l, x, -1, t);
update(x + 1, r, -1, t);
update(l, r, 1, t);
L[l] = L[r] = l;
R[l] = R[r] = r;
Rs.erase(x);
}
void del(int x, int t) {
int r = *Rs.lower_bound(x);
int l = L[r];
update(l, r, -1, t);
update(l, x, 1, t);
update(x + 1, r, 1, t);
L[l] = L[x] = l;
R[l] = R[x] = x;
L[x + 1] = L[r] = x + 1;
R[x + 1] = R[r] = r;
Rs.insert(x);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
cin >> in;
for (int i = 1; i <= q; ++i) {
char str[20];
cin >> str;
if (str[0] == 't') {
cin >> A[i];
}
else {
cin >> A[i] >> B[i];
fenM.add1(A[i]);
fenB.add1(A[i]);
}
}
fenM.init1();
fenB.init1();
for (int i = 1; i <= q; ++i) if (B[i]) {
fenM.add2(A[i], -B[i]);
fenB.add2(A[i], -B[i]);
}
fenM.init2();
fenB.init2();
for (int i = 1; i <= n + 1; ++i) {
L[i] = R[i] = i;
Rs.insert(i);
fenM.update(i, -i, 1);
fenB.update(i, -i, 1);
}
for (int i = 1; i <= n; ++i) {
S[i] = in[i - 1] - '0';
if (S[i]) {
add(i, 0);
}
}
for (int i = 1; i <= q; ++i) {
if (B[i]) {
llong Mv = fenM.query(A[i], -B[i]);
llong Bv = fenB.query(A[i], -B[i]);
printf("%lld\n", Mv * i + Bv);
}
else {
S[A[i]] ^= 1;
if (S[A[i]]) add(A[i], i);
else del(A[i], i);
}
}
return 0;
}
Compilation message
street_lamps.cpp: In member function 'void fen1D::update(int, llong)':
street_lamps.cpp:30:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i <= cp.size()) {
~~^~~~~~~~~~~~
street_lamps.cpp: In member function 'void fen2D::update(int, int, llong)':
street_lamps.cpp:71:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (i <= cp.size()) {
~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
336 ms |
12872 KB |
Output is correct |
2 |
Correct |
437 ms |
13768 KB |
Output is correct |
3 |
Correct |
803 ms |
18476 KB |
Output is correct |
4 |
Correct |
2989 ms |
77388 KB |
Output is correct |
5 |
Correct |
3039 ms |
86360 KB |
Output is correct |
6 |
Correct |
2486 ms |
67432 KB |
Output is correct |
7 |
Correct |
2879 ms |
112792 KB |
Output is correct |
8 |
Correct |
4149 ms |
114084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
504 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
632 KB |
Output is correct |
5 |
Correct |
884 ms |
25968 KB |
Output is correct |
6 |
Correct |
2065 ms |
58188 KB |
Output is correct |
7 |
Correct |
2698 ms |
86820 KB |
Output is correct |
8 |
Correct |
4145 ms |
122732 KB |
Output is correct |
9 |
Correct |
240 ms |
15224 KB |
Output is correct |
10 |
Correct |
257 ms |
16464 KB |
Output is correct |
11 |
Correct |
277 ms |
17232 KB |
Output is correct |
12 |
Correct |
2971 ms |
121280 KB |
Output is correct |
13 |
Correct |
4072 ms |
122732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
636 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
4 |
Correct |
3 ms |
504 KB |
Output is correct |
5 |
Correct |
3244 ms |
120448 KB |
Output is correct |
6 |
Correct |
2764 ms |
94932 KB |
Output is correct |
7 |
Correct |
2201 ms |
67376 KB |
Output is correct |
8 |
Correct |
1004 ms |
24864 KB |
Output is correct |
9 |
Correct |
495 ms |
10608 KB |
Output is correct |
10 |
Correct |
314 ms |
4860 KB |
Output is correct |
11 |
Correct |
498 ms |
10592 KB |
Output is correct |
12 |
Correct |
317 ms |
4924 KB |
Output is correct |
13 |
Correct |
501 ms |
10604 KB |
Output is correct |
14 |
Correct |
313 ms |
4856 KB |
Output is correct |
15 |
Correct |
2914 ms |
121212 KB |
Output is correct |
16 |
Correct |
4153 ms |
122656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
3 ms |
504 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
336 ms |
12872 KB |
Output is correct |
9 |
Correct |
437 ms |
13768 KB |
Output is correct |
10 |
Correct |
803 ms |
18476 KB |
Output is correct |
11 |
Correct |
2989 ms |
77388 KB |
Output is correct |
12 |
Correct |
3039 ms |
86360 KB |
Output is correct |
13 |
Correct |
2486 ms |
67432 KB |
Output is correct |
14 |
Correct |
2879 ms |
112792 KB |
Output is correct |
15 |
Correct |
4149 ms |
114084 KB |
Output is correct |
16 |
Correct |
3 ms |
376 KB |
Output is correct |
17 |
Correct |
4 ms |
504 KB |
Output is correct |
18 |
Correct |
4 ms |
632 KB |
Output is correct |
19 |
Correct |
6 ms |
632 KB |
Output is correct |
20 |
Correct |
884 ms |
25968 KB |
Output is correct |
21 |
Correct |
2065 ms |
58188 KB |
Output is correct |
22 |
Correct |
2698 ms |
86820 KB |
Output is correct |
23 |
Correct |
4145 ms |
122732 KB |
Output is correct |
24 |
Correct |
240 ms |
15224 KB |
Output is correct |
25 |
Correct |
257 ms |
16464 KB |
Output is correct |
26 |
Correct |
277 ms |
17232 KB |
Output is correct |
27 |
Correct |
2971 ms |
121280 KB |
Output is correct |
28 |
Correct |
4072 ms |
122732 KB |
Output is correct |
29 |
Correct |
5 ms |
636 KB |
Output is correct |
30 |
Correct |
5 ms |
632 KB |
Output is correct |
31 |
Correct |
4 ms |
504 KB |
Output is correct |
32 |
Correct |
3 ms |
504 KB |
Output is correct |
33 |
Correct |
3244 ms |
120448 KB |
Output is correct |
34 |
Correct |
2764 ms |
94932 KB |
Output is correct |
35 |
Correct |
2201 ms |
67376 KB |
Output is correct |
36 |
Correct |
1004 ms |
24864 KB |
Output is correct |
37 |
Correct |
495 ms |
10608 KB |
Output is correct |
38 |
Correct |
314 ms |
4860 KB |
Output is correct |
39 |
Correct |
498 ms |
10592 KB |
Output is correct |
40 |
Correct |
317 ms |
4924 KB |
Output is correct |
41 |
Correct |
501 ms |
10604 KB |
Output is correct |
42 |
Correct |
313 ms |
4856 KB |
Output is correct |
43 |
Correct |
2914 ms |
121212 KB |
Output is correct |
44 |
Correct |
4153 ms |
122656 KB |
Output is correct |
45 |
Correct |
122 ms |
6888 KB |
Output is correct |
46 |
Correct |
248 ms |
8272 KB |
Output is correct |
47 |
Correct |
1319 ms |
40988 KB |
Output is correct |
48 |
Correct |
2500 ms |
77656 KB |
Output is correct |
49 |
Correct |
293 ms |
18008 KB |
Output is correct |
50 |
Correct |
287 ms |
17800 KB |
Output is correct |
51 |
Correct |
311 ms |
18660 KB |
Output is correct |
52 |
Correct |
400 ms |
23400 KB |
Output is correct |
53 |
Correct |
399 ms |
23380 KB |
Output is correct |
54 |
Correct |
396 ms |
23516 KB |
Output is correct |