#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN=3e5;
int n, q, ans[mxN], st[mxN];
string s;
vector<int> changes[mxN];
vector<ar<int, 2>> ls[mxN];
vector<ar<int, 4>> queries;
struct ft {
ll ft[mxN+1];
void upd(int i, ll x) {
for (++i; i<=mxN; i+=i&-i)
ft[i]+=x;
}
ll qry(int i) {
ll r=0;
for (++i; i; i-=i&-i)
r+=ft[i];
return r;
}
} ft1, ft2;
void upd(int l, int r, int x) {
ft1.upd(l, -(ll)(l-1)*x);
ft2.upd(l, x);
ft1.upd(r+1, (ll)r*x);
ft2.upd(r+1, -x);
}
ll qry(int i) {
return i*ft2.qry(i)+ft1.qry(i);
}
void solve(int l, int r) {
if (l==r)
return;
int mid=(l+r)/2;
solve(l, mid);
solve(mid+1, r);
vector<ar<int, 4>> tmp;
vector<ar<int, 3>> rb;
tmp.reserve(r-l+1);
for (int i=l, j=mid+1; tmp.size()<r-l+1;) {
if (j>r||i<=mid&&queries[i][0]<=queries[j][0]) {
if (queries[i][2]!=-1) { // update
upd(queries[i][1], queries[i][2], queries[i][3]);
rb.push_back({queries[i][1], queries[i][2], -queries[i][3]});
}
tmp.push_back(queries[i++]);
} else {
if (queries[j][2]==-1) // query
ans[queries[j][1]]+=qry(queries[j][1]);
tmp.push_back(queries[j++]);
}
}
for (int i=l; i<=r; ++i)
queries[i]=tmp[i-l];
for (auto [l, r, x] : rb)
upd(l, r, x);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q >> s;
for (int i=0; i<n; ++i)
if (s[i]=='0')
changes[i].push_back(-1);
for (int i=0; i<q; ++i) {
string type;
cin >> type;
if (type=="toggle") {
int x;
cin >> x, --x;
changes[x].push_back(i);
ans[i]=-1;
} else {
int l, r;
cin >> l >> r, --l, r-=2;
ls[r].push_back({l, i});
}
}
set<ar<int, 3>> s;
s.insert({0, q-1, 0});
queries.push_back({0, 0, q-1, 1});
for (int i=0; i<n; ++i) {
for (int j=0; j<changes[i].size(); j+=2) {
int b=j+1<changes[i].size()?changes[i][j+1]:q-1;
ar<int, 3> seg={changes[i][j]+1, b, i+1};
auto it=s.lower_bound({seg[0]});
while(it!=s.end()&&(*it)[0]<=seg[1]) {
if ((*it)[1]<=seg[1]) { // erase entire segment
queries.push_back({(*it)[2], (*it)[0], (*it)[1], -1});
it=s.erase(it);
} else { // erase part of this segment
int rb=(*it)[1], val=(*it)[2];
queries.push_back({val, (*it)[0], seg[1], -1});
s.erase(it);
it=s.insert({seg[1]+1, rb, val}).first;
}
}
if (it!=s.begin()) {
--it;
if ((*it)[1]>=seg[0]) {
int l=(*it)[0], r=(*it)[1], val=(*it)[2];
s.erase(it);
if (r<=seg[1]) {
queries.push_back({val, seg[0], r, -1});
s.insert({l, seg[0]-1, val});
} else {
assert(l<seg[0]&&seg[1]<r);
queries.push_back({val, seg[0], seg[1], -1});
s.insert({l, seg[0]-1, val});
s.insert({seg[1]+1, r, val});
}
}
}
/*while(s.size()) {
auto it=s.lower_bound({seg[0], -1});
if (it!=s.end()&&(*it)[0]<=seg[1]) {
int l=(*it)[0], r=(*it)[1], val=(*it)[2];
queries.push_back({val, l, min(r, seg[1]), -1});
s.erase(it);
if (r>seg[1])
s.insert({seg[1]+1, r, val});
} else if (it!=s.begin()) {
--it;
int l=(*it)[0], r=(*it)[1], val=(*it)[2];
if (r>=seg[0]) {
assert(l<seg[0]);
queries.push_back({val, seg[0], r, -1});
s.erase(it);
s.insert({l, seg[0]-1, val});
} else
break;
} else
break;
}*/
queries.push_back({seg[2], seg[0], seg[1], 1});
s.insert(seg);
}
for (auto [l, t] : ls[i])
queries.push_back({l, t, -1});
}
//for (auto x : queries)
// cout << x[0] << " " << x[1] << " " << x[2] << " " << x[3] << endl;
solve(0, queries.size()-1);
for (int i=0; i<q; ++i)
if (~ans[i])
cout << ans[i] << "\n";
return 0;
}
Compilation message
street_lamps.cpp: In function 'void solve(int, int)':
street_lamps.cpp:48:35: warning: comparison of integer expressions of different signedness: 'std::vector<std::array<int, 4> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
48 | for (int i=l, j=mid+1; tmp.size()<r-l+1;) {
| ~~~~~~~~~~^~~~~~
street_lamps.cpp:49:18: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
49 | if (j>r||i<=mid&&queries[i][0]<=queries[j][0]) {
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:92:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j=0; j<changes[i].size(); j+=2) {
| ~^~~~~~~~~~~~~~~~~~
street_lamps.cpp:93:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | int b=j+1<changes[i].size()?changes[i][j+1]:q-1;
| ~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14432 KB |
Output is correct |
3 |
Correct |
11 ms |
14476 KB |
Output is correct |
4 |
Correct |
10 ms |
14424 KB |
Output is correct |
5 |
Correct |
8 ms |
14428 KB |
Output is correct |
6 |
Correct |
12 ms |
14512 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
856 ms |
42748 KB |
Output is correct |
2 |
Correct |
873 ms |
46320 KB |
Output is correct |
3 |
Correct |
868 ms |
47568 KB |
Output is correct |
4 |
Correct |
1404 ms |
69092 KB |
Output is correct |
5 |
Correct |
1302 ms |
57932 KB |
Output is correct |
6 |
Correct |
1680 ms |
68004 KB |
Output is correct |
7 |
Correct |
1433 ms |
77700 KB |
Output is correct |
8 |
Correct |
277 ms |
40096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14676 KB |
Output is correct |
2 |
Correct |
12 ms |
14608 KB |
Output is correct |
3 |
Correct |
10 ms |
14520 KB |
Output is correct |
4 |
Correct |
8 ms |
14548 KB |
Output is correct |
5 |
Correct |
2041 ms |
70692 KB |
Output is correct |
6 |
Correct |
1683 ms |
62128 KB |
Output is correct |
7 |
Correct |
1073 ms |
52480 KB |
Output is correct |
8 |
Correct |
331 ms |
33756 KB |
Output is correct |
9 |
Correct |
165 ms |
28688 KB |
Output is correct |
10 |
Correct |
194 ms |
30824 KB |
Output is correct |
11 |
Correct |
206 ms |
29472 KB |
Output is correct |
12 |
Correct |
1444 ms |
71916 KB |
Output is correct |
13 |
Correct |
319 ms |
33576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14548 KB |
Output is correct |
2 |
Correct |
13 ms |
14560 KB |
Output is correct |
3 |
Correct |
12 ms |
14668 KB |
Output is correct |
4 |
Correct |
13 ms |
14676 KB |
Output is correct |
5 |
Correct |
1033 ms |
53064 KB |
Output is correct |
6 |
Correct |
1132 ms |
58836 KB |
Output is correct |
7 |
Correct |
1473 ms |
63284 KB |
Output is correct |
8 |
Correct |
1759 ms |
69116 KB |
Output is correct |
9 |
Correct |
939 ms |
45208 KB |
Output is correct |
10 |
Correct |
1111 ms |
53284 KB |
Output is correct |
11 |
Correct |
919 ms |
48388 KB |
Output is correct |
12 |
Correct |
1165 ms |
53240 KB |
Output is correct |
13 |
Correct |
918 ms |
48424 KB |
Output is correct |
14 |
Correct |
1206 ms |
53220 KB |
Output is correct |
15 |
Correct |
1488 ms |
77876 KB |
Output is correct |
16 |
Correct |
304 ms |
39540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14432 KB |
Output is correct |
3 |
Correct |
11 ms |
14476 KB |
Output is correct |
4 |
Correct |
10 ms |
14424 KB |
Output is correct |
5 |
Correct |
8 ms |
14428 KB |
Output is correct |
6 |
Correct |
12 ms |
14512 KB |
Output is correct |
7 |
Correct |
10 ms |
14420 KB |
Output is correct |
8 |
Correct |
856 ms |
42748 KB |
Output is correct |
9 |
Correct |
873 ms |
46320 KB |
Output is correct |
10 |
Correct |
868 ms |
47568 KB |
Output is correct |
11 |
Correct |
1404 ms |
69092 KB |
Output is correct |
12 |
Correct |
1302 ms |
57932 KB |
Output is correct |
13 |
Correct |
1680 ms |
68004 KB |
Output is correct |
14 |
Correct |
1433 ms |
77700 KB |
Output is correct |
15 |
Correct |
277 ms |
40096 KB |
Output is correct |
16 |
Correct |
13 ms |
14676 KB |
Output is correct |
17 |
Correct |
12 ms |
14608 KB |
Output is correct |
18 |
Correct |
10 ms |
14520 KB |
Output is correct |
19 |
Correct |
8 ms |
14548 KB |
Output is correct |
20 |
Correct |
2041 ms |
70692 KB |
Output is correct |
21 |
Correct |
1683 ms |
62128 KB |
Output is correct |
22 |
Correct |
1073 ms |
52480 KB |
Output is correct |
23 |
Correct |
331 ms |
33756 KB |
Output is correct |
24 |
Correct |
165 ms |
28688 KB |
Output is correct |
25 |
Correct |
194 ms |
30824 KB |
Output is correct |
26 |
Correct |
206 ms |
29472 KB |
Output is correct |
27 |
Correct |
1444 ms |
71916 KB |
Output is correct |
28 |
Correct |
319 ms |
33576 KB |
Output is correct |
29 |
Correct |
10 ms |
14548 KB |
Output is correct |
30 |
Correct |
13 ms |
14560 KB |
Output is correct |
31 |
Correct |
12 ms |
14668 KB |
Output is correct |
32 |
Correct |
13 ms |
14676 KB |
Output is correct |
33 |
Correct |
1033 ms |
53064 KB |
Output is correct |
34 |
Correct |
1132 ms |
58836 KB |
Output is correct |
35 |
Correct |
1473 ms |
63284 KB |
Output is correct |
36 |
Correct |
1759 ms |
69116 KB |
Output is correct |
37 |
Correct |
939 ms |
45208 KB |
Output is correct |
38 |
Correct |
1111 ms |
53284 KB |
Output is correct |
39 |
Correct |
919 ms |
48388 KB |
Output is correct |
40 |
Correct |
1165 ms |
53240 KB |
Output is correct |
41 |
Correct |
918 ms |
48424 KB |
Output is correct |
42 |
Correct |
1206 ms |
53220 KB |
Output is correct |
43 |
Correct |
1488 ms |
77876 KB |
Output is correct |
44 |
Correct |
304 ms |
39540 KB |
Output is correct |
45 |
Correct |
444 ms |
35396 KB |
Output is correct |
46 |
Correct |
563 ms |
35376 KB |
Output is correct |
47 |
Correct |
754 ms |
43664 KB |
Output is correct |
48 |
Correct |
1676 ms |
69252 KB |
Output is correct |
49 |
Correct |
246 ms |
35560 KB |
Output is correct |
50 |
Correct |
207 ms |
36264 KB |
Output is correct |
51 |
Correct |
219 ms |
35200 KB |
Output is correct |
52 |
Correct |
207 ms |
34652 KB |
Output is correct |
53 |
Correct |
234 ms |
34688 KB |
Output is correct |
54 |
Correct |
203 ms |
34744 KB |
Output is correct |