#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef tuple<int, int, int, int> iiii;
const int MAXN = 300005;
int nums, queries;
ll ans[MAXN], tfw[MAXN], cfw[MAXN];
void update(int x, int v, ll *fw){
while (x <= nums){
fw[x] += v;
x += (x & (-x));
}
}
ll query(int x, ll *fw){
ll res = 0;
while (x){
res += fw[x];
x -= (x & (-x));
}
return res;
}
ll rquery(int x, int y, ll *fw){
return query(y, fw) - query(x - 1, fw);
}
void dnc(int s, int e, vector<iiii> &qv, vector<iiii> &uv){
vector<iiii> lqv, rqv, luv, ruv;
int m = (s + e) >> 1, uid = 0, usz = uv.size();
for (auto &[t, lx, ly, r] : qv){
if (lx == s && ly == e){
for (; uid != usz && get<0>(uv[uid]) <= t; uid++){
int ut, ul, ur, utyp; tie(ut, ul, ur, utyp) = uv[uid];
update(ur, ut * utyp, tfw); update(ur, -utyp, cfw);
}
ans[t] += rquery(r, nums, tfw) + rquery(r, nums, cfw) * t;
}
else if (ly <= m) lqv.push_back({t, lx, ly, r});
else if (lx > m) rqv.push_back({t, lx, ly, r});
else{
lqv.push_back({t, lx, m, r}); rqv.push_back({t, m + 1, ly, r});
}
}
for (uid--; uid >= 0; uid--){
int ut, ul, ur, utyp; tie(ut, ul, ur, utyp) = uv[uid];
update(ur, -ut * utyp, tfw); update(ur, utyp, cfw);
}
for (auto &[t, l, r, typ] : uv){
if (l <= m) luv.push_back({t, l, r, typ});
else ruv.push_back({t, l, r, typ});
}
if (s != e){
dnc(s, m, lqv, luv); dnc(m + 1, e, rqv, ruv);
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> nums >> queries;
vector<int> state(nums + 2, 0);
set<pair<int, int>> rs;
vector<iiii> uv, qv;
for (int i = 1; i <= nums; i++){
char ch; cin >> ch;
state[i] = ch - '0';
}
int sid = -1;
for (int i = 1; i <= nums; i++){
if (!state[i]) continue;
if (sid == -1) sid = i;
if (!state[i + 1]){
rs.insert({sid, i}); uv.push_back({0, sid, i, -1});
sid = -1;
}
}
vector<int> qt;
for (int q = 1; q <= queries; q++){
string op; cin >> op;
if (op == "toggle"){
int x; cin >> x;
if (!state[x]){
int nl = x, nr = x;
if (state[x - 1]){
int l, r; tie(l, r) = *prev(rs.lower_bound(make_pair(x, -1)));
rs.erase({l, r}); uv.push_back({q, l, r, 1});
nl = l;
}
if (state[x + 1]){
int l, r; tie(l, r) = *(rs.lower_bound(make_pair(x, -1)));
rs.erase({l, r}); uv.push_back({q, l, r, 1});
nr = r;
}
rs.insert({nl, nr}); uv.push_back({q, nl, nr, -1});
state[x] = 1;
}
else{
int l, r; tie(l, r) = *prev(rs.lower_bound(make_pair(x + 1, -1)));
rs.erase({l, r}); uv.push_back({q, l, r, 1});
if (state[x - 1]){
rs.insert({l, x - 1}); uv.push_back({q, l, x - 1, -1});
}
if (state[x + 1]){
rs.insert({x + 1, r}); uv.push_back({q, x + 1, r, -1});
}
state[x] = 0;
}
}
else{
int l, r; cin >> l >> r; r--;
qv.push_back({q, 1, l, r});
qt.push_back(q);
}
}
dnc(1, nums, qv, uv);
for (int q : qt) cout << ans[q] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
43048 KB |
Output is correct |
2 |
Correct |
185 ms |
44448 KB |
Output is correct |
3 |
Correct |
293 ms |
43976 KB |
Output is correct |
4 |
Correct |
628 ms |
56220 KB |
Output is correct |
5 |
Correct |
651 ms |
57264 KB |
Output is correct |
6 |
Correct |
590 ms |
48820 KB |
Output is correct |
7 |
Correct |
285 ms |
39788 KB |
Output is correct |
8 |
Correct |
311 ms |
41260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4580 KB |
Output is correct |
5 |
Correct |
543 ms |
58268 KB |
Output is correct |
6 |
Correct |
654 ms |
62120 KB |
Output is correct |
7 |
Correct |
588 ms |
53968 KB |
Output is correct |
8 |
Correct |
311 ms |
41972 KB |
Output is correct |
9 |
Correct |
71 ms |
27528 KB |
Output is correct |
10 |
Correct |
103 ms |
33320 KB |
Output is correct |
11 |
Correct |
87 ms |
33728 KB |
Output is correct |
12 |
Correct |
290 ms |
37696 KB |
Output is correct |
13 |
Correct |
336 ms |
41596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4700 KB |
Output is correct |
3 |
Correct |
2 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4700 KB |
Output is correct |
5 |
Correct |
358 ms |
54332 KB |
Output is correct |
6 |
Correct |
490 ms |
48376 KB |
Output is correct |
7 |
Correct |
571 ms |
50512 KB |
Output is correct |
8 |
Correct |
616 ms |
58088 KB |
Output is correct |
9 |
Correct |
242 ms |
37508 KB |
Output is correct |
10 |
Correct |
200 ms |
40444 KB |
Output is correct |
11 |
Correct |
214 ms |
36116 KB |
Output is correct |
12 |
Correct |
198 ms |
45776 KB |
Output is correct |
13 |
Correct |
218 ms |
36024 KB |
Output is correct |
14 |
Correct |
201 ms |
41388 KB |
Output is correct |
15 |
Correct |
316 ms |
40172 KB |
Output is correct |
16 |
Correct |
272 ms |
40972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
159 ms |
43048 KB |
Output is correct |
9 |
Correct |
185 ms |
44448 KB |
Output is correct |
10 |
Correct |
293 ms |
43976 KB |
Output is correct |
11 |
Correct |
628 ms |
56220 KB |
Output is correct |
12 |
Correct |
651 ms |
57264 KB |
Output is correct |
13 |
Correct |
590 ms |
48820 KB |
Output is correct |
14 |
Correct |
285 ms |
39788 KB |
Output is correct |
15 |
Correct |
311 ms |
41260 KB |
Output is correct |
16 |
Correct |
1 ms |
4700 KB |
Output is correct |
17 |
Correct |
2 ms |
4700 KB |
Output is correct |
18 |
Correct |
2 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4580 KB |
Output is correct |
20 |
Correct |
543 ms |
58268 KB |
Output is correct |
21 |
Correct |
654 ms |
62120 KB |
Output is correct |
22 |
Correct |
588 ms |
53968 KB |
Output is correct |
23 |
Correct |
311 ms |
41972 KB |
Output is correct |
24 |
Correct |
71 ms |
27528 KB |
Output is correct |
25 |
Correct |
103 ms |
33320 KB |
Output is correct |
26 |
Correct |
87 ms |
33728 KB |
Output is correct |
27 |
Correct |
290 ms |
37696 KB |
Output is correct |
28 |
Correct |
336 ms |
41596 KB |
Output is correct |
29 |
Correct |
2 ms |
4952 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
2 ms |
4700 KB |
Output is correct |
32 |
Correct |
2 ms |
4700 KB |
Output is correct |
33 |
Correct |
358 ms |
54332 KB |
Output is correct |
34 |
Correct |
490 ms |
48376 KB |
Output is correct |
35 |
Correct |
571 ms |
50512 KB |
Output is correct |
36 |
Correct |
616 ms |
58088 KB |
Output is correct |
37 |
Correct |
242 ms |
37508 KB |
Output is correct |
38 |
Correct |
200 ms |
40444 KB |
Output is correct |
39 |
Correct |
214 ms |
36116 KB |
Output is correct |
40 |
Correct |
198 ms |
45776 KB |
Output is correct |
41 |
Correct |
218 ms |
36024 KB |
Output is correct |
42 |
Correct |
201 ms |
41388 KB |
Output is correct |
43 |
Correct |
316 ms |
40172 KB |
Output is correct |
44 |
Correct |
272 ms |
40972 KB |
Output is correct |
45 |
Correct |
74 ms |
24876 KB |
Output is correct |
46 |
Correct |
100 ms |
25256 KB |
Output is correct |
47 |
Correct |
309 ms |
32328 KB |
Output is correct |
48 |
Correct |
557 ms |
52916 KB |
Output is correct |
49 |
Correct |
91 ms |
35280 KB |
Output is correct |
50 |
Correct |
115 ms |
37368 KB |
Output is correct |
51 |
Correct |
100 ms |
35152 KB |
Output is correct |
52 |
Correct |
108 ms |
39100 KB |
Output is correct |
53 |
Correct |
127 ms |
39408 KB |
Output is correct |
54 |
Correct |
113 ms |
37372 KB |
Output is correct |