#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Node;
const int s1 = 2'000'000;
vector<Node> node(s1); int next1 = 1;
int n;
struct Node {
int left, right;
pair<ll,int> val;
Node() : left(0), right(0), val({0,0}) {}
void update(int v, int xl, int xr, int add, int ct) {
if (v < xl || v > xr) return;
val.first += add;
val.second += ct;
if (xl != xr) {
int xm = (xl + xr) / 2;
if (v <= xm) {
if (!left) left = next1++;
node[left].update(v, xl, xm, add, ct);
} else {
if (!right) right = next1++;
node[right].update(v, xm+1, xr, add, ct);
}
}
}
pair<ll,int> query(int l, int r, int xl, int xr) {
if (l > r) return {0, 0};
if (l == xl && r == xr) {
return val;
} else {
pair<ll,int> ans = {0, 0};
int xm = (xl + xr) / 2;
if (left) {
pair<ll,int> leftans = node[left].query(l, min(r, xm), xl, xm);
ans.first += leftans.first;
ans.second += leftans.second;
}
if (right) {
pair<ll,int> rightans = node[right].query(max(l, xm+1), r, xm+1, xr);
ans.first += rightans.first;
ans.second += rightans.second;
}
return ans;
}
}
};
// count {t1<t2, a1<a2+1, b1>b2-1} for each query
// because of equality, updates must be processed before queries
Node onTree, offTree;
vector<array<int,5>> updates;
vector<ll> offSum, onSum;
vector<int> offCount, onCount;
void cdq(int l, int r) {
if (l == r) return;
int m = (l + r) / 2;
cdq(l, m); cdq(m+1, r);
sort(updates.begin() + l, updates.begin() + m + 1,
[&](array<int,5> A, array<int,5> B) -> bool {
return A[1] < B[1];
});
sort(updates.begin() + m + 1, updates.begin() + r + 1,
[&](array<int,5> A, array<int,5> B) -> bool {
return A[1] < B[1];
});
stack<pair<int,int>> offActions, onActions;
int a = l, b = m+1;
while (a <= m || b <= r) {
bool isleft;
int cur;
if (a > m || (b <= r && updates[b][1] <= updates[a][1])) cur = b++, isleft = 0;
else cur = a++, isleft = 1;
if (updates[cur][3] == -1 && isleft) {
offTree.update(updates[cur][2], 0, n-1, updates[cur][0], +1);
offActions.push({updates[cur][2], updates[cur][0]});
} else if (updates[cur][3] == 1 && isleft) {
onTree.update(updates[cur][2], 0, n-1, updates[cur][0], +1);
onActions.push({updates[cur][2], updates[cur][0]});
} else if (updates[cur][3] == 0 && !isleft) {
int qi = updates[cur][4];
pair<ll,int> onDat = onTree.query(updates[cur][2]+1, n-1, 0, n-1);
onSum[qi] += onDat.first; onCount[qi] += onDat.second;
pair<ll,int> offDat = offTree.query(updates[cur][2]+1, n-1, 0, n-1);
offSum[qi] += offDat.first; offCount[qi] += offDat.second;
}
}
while (offActions.size()) {
offTree.update(offActions.top().first, 0, n-1, -offActions.top().second, -1);
offActions.pop();
}
while (onActions.size()) {
onTree.update(onActions.top().first, 0, n-1, -onActions.top().second, -1);
onActions.pop();
}
}
int main() {
int q;
cin >> n >> q;
string s;
cin >> s;
set<array<int,2>> segments;
for (int i=0; i<n; i++) {
if (s[i] == '1') {
int l = i;
while (i < n-1 && s[i+1] == '1') i++;
segments.insert({l, i});
updates.push_back({0, l, i, +1});
}
}
int qc = 0;
vector<int> qtime;
for (int t=1; t<=q; t++) {
string type;
cin >> type;
if (type == "toggle") {
int i;
cin >> i;
i--;
if (s[i] == '0') {
int l = i, r = i;
auto right = segments.lower_bound({i, i});
if (right != segments.end() && right->at(0) == i+1) {
r = right->at(1);
updates.push_back({t, i+1, r, -1});
segments.erase(right);
}
auto left = segments.lower_bound({i, i});
if (left != segments.begin()) --left;
if (left != segments.end() && left->at(1) == i-1) {
l = left->at(0);
updates.push_back({t, l, i-1, -1});
segments.erase(left);
}
segments.insert({l, r});
updates.push_back({t, l, r, +1});
s[i] = '1';
} else {
auto it = prev(segments.lower_bound({i+1, i+1}));
int l = it->at(0), r = it->at(1);
segments.erase(it);
updates.push_back({t, l, r, -1});
if (l < i) {
segments.insert({l, i-1});
updates.push_back({t, l, i-1, +1});
}
if (r > i) {
segments.insert({i+1, r});
updates.push_back({t, i+1, r, +1});
}
s[i] = '0';
}
} else {
int a, b;
cin >> a >> b;
updates.push_back({t, a, b-3, 0, qc++});
qtime.push_back(t);
}
}
sort(updates.begin(), updates.end());
offSum.resize(qc); onSum.resize(qc);
offCount.resize(qc); onCount.resize(qc);
cdq(0, (int) updates.size() - 1);
for (int i=0; i<qc; i++) {
ll ans = offSum[i] - onSum[i];
if (onCount[i] > offCount[i]) ans += qtime[i];
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
47196 KB |
Output is correct |
2 |
Correct |
9 ms |
47192 KB |
Output is correct |
3 |
Correct |
8 ms |
47196 KB |
Output is correct |
4 |
Correct |
8 ms |
47196 KB |
Output is correct |
5 |
Correct |
8 ms |
47196 KB |
Output is correct |
6 |
Correct |
9 ms |
47196 KB |
Output is correct |
7 |
Correct |
8 ms |
47196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
799 ms |
65448 KB |
Output is correct |
2 |
Correct |
872 ms |
66244 KB |
Output is correct |
3 |
Correct |
1197 ms |
67792 KB |
Output is correct |
4 |
Correct |
1918 ms |
79476 KB |
Output is correct |
5 |
Correct |
1994 ms |
78684 KB |
Output is correct |
6 |
Correct |
2049 ms |
79380 KB |
Output is correct |
7 |
Correct |
466 ms |
69944 KB |
Output is correct |
8 |
Correct |
496 ms |
72396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
47452 KB |
Output is correct |
2 |
Correct |
11 ms |
47452 KB |
Output is correct |
3 |
Correct |
11 ms |
47304 KB |
Output is correct |
4 |
Correct |
9 ms |
47452 KB |
Output is correct |
5 |
Correct |
2152 ms |
77076 KB |
Output is correct |
6 |
Correct |
2422 ms |
77956 KB |
Output is correct |
7 |
Correct |
2119 ms |
79608 KB |
Output is correct |
8 |
Correct |
525 ms |
71172 KB |
Output is correct |
9 |
Correct |
344 ms |
62528 KB |
Output is correct |
10 |
Correct |
401 ms |
65372 KB |
Output is correct |
11 |
Correct |
383 ms |
67024 KB |
Output is correct |
12 |
Correct |
470 ms |
70908 KB |
Output is correct |
13 |
Correct |
523 ms |
70596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
47448 KB |
Output is correct |
2 |
Correct |
10 ms |
47452 KB |
Output is correct |
3 |
Correct |
11 ms |
47452 KB |
Output is correct |
4 |
Correct |
11 ms |
47452 KB |
Output is correct |
5 |
Correct |
1397 ms |
75336 KB |
Output is correct |
6 |
Correct |
1873 ms |
73764 KB |
Output is correct |
7 |
Correct |
2201 ms |
79124 KB |
Output is correct |
8 |
Correct |
2350 ms |
75632 KB |
Output is correct |
9 |
Correct |
1004 ms |
65276 KB |
Output is correct |
10 |
Correct |
942 ms |
71092 KB |
Output is correct |
11 |
Correct |
1022 ms |
65364 KB |
Output is correct |
12 |
Correct |
956 ms |
73116 KB |
Output is correct |
13 |
Correct |
1012 ms |
65460 KB |
Output is correct |
14 |
Correct |
938 ms |
70868 KB |
Output is correct |
15 |
Correct |
469 ms |
69916 KB |
Output is correct |
16 |
Correct |
507 ms |
71692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
47196 KB |
Output is correct |
2 |
Correct |
9 ms |
47192 KB |
Output is correct |
3 |
Correct |
8 ms |
47196 KB |
Output is correct |
4 |
Correct |
8 ms |
47196 KB |
Output is correct |
5 |
Correct |
8 ms |
47196 KB |
Output is correct |
6 |
Correct |
9 ms |
47196 KB |
Output is correct |
7 |
Correct |
8 ms |
47196 KB |
Output is correct |
8 |
Correct |
799 ms |
65448 KB |
Output is correct |
9 |
Correct |
872 ms |
66244 KB |
Output is correct |
10 |
Correct |
1197 ms |
67792 KB |
Output is correct |
11 |
Correct |
1918 ms |
79476 KB |
Output is correct |
12 |
Correct |
1994 ms |
78684 KB |
Output is correct |
13 |
Correct |
2049 ms |
79380 KB |
Output is correct |
14 |
Correct |
466 ms |
69944 KB |
Output is correct |
15 |
Correct |
496 ms |
72396 KB |
Output is correct |
16 |
Correct |
11 ms |
47452 KB |
Output is correct |
17 |
Correct |
11 ms |
47452 KB |
Output is correct |
18 |
Correct |
11 ms |
47304 KB |
Output is correct |
19 |
Correct |
9 ms |
47452 KB |
Output is correct |
20 |
Correct |
2152 ms |
77076 KB |
Output is correct |
21 |
Correct |
2422 ms |
77956 KB |
Output is correct |
22 |
Correct |
2119 ms |
79608 KB |
Output is correct |
23 |
Correct |
525 ms |
71172 KB |
Output is correct |
24 |
Correct |
344 ms |
62528 KB |
Output is correct |
25 |
Correct |
401 ms |
65372 KB |
Output is correct |
26 |
Correct |
383 ms |
67024 KB |
Output is correct |
27 |
Correct |
470 ms |
70908 KB |
Output is correct |
28 |
Correct |
523 ms |
70596 KB |
Output is correct |
29 |
Correct |
10 ms |
47448 KB |
Output is correct |
30 |
Correct |
10 ms |
47452 KB |
Output is correct |
31 |
Correct |
11 ms |
47452 KB |
Output is correct |
32 |
Correct |
11 ms |
47452 KB |
Output is correct |
33 |
Correct |
1397 ms |
75336 KB |
Output is correct |
34 |
Correct |
1873 ms |
73764 KB |
Output is correct |
35 |
Correct |
2201 ms |
79124 KB |
Output is correct |
36 |
Correct |
2350 ms |
75632 KB |
Output is correct |
37 |
Correct |
1004 ms |
65276 KB |
Output is correct |
38 |
Correct |
942 ms |
71092 KB |
Output is correct |
39 |
Correct |
1022 ms |
65364 KB |
Output is correct |
40 |
Correct |
956 ms |
73116 KB |
Output is correct |
41 |
Correct |
1012 ms |
65460 KB |
Output is correct |
42 |
Correct |
938 ms |
70868 KB |
Output is correct |
43 |
Correct |
469 ms |
69916 KB |
Output is correct |
44 |
Correct |
507 ms |
71692 KB |
Output is correct |
45 |
Correct |
372 ms |
60632 KB |
Output is correct |
46 |
Correct |
507 ms |
62164 KB |
Output is correct |
47 |
Correct |
1078 ms |
63012 KB |
Output is correct |
48 |
Correct |
1951 ms |
79564 KB |
Output is correct |
49 |
Correct |
455 ms |
66628 KB |
Output is correct |
50 |
Correct |
453 ms |
67208 KB |
Output is correct |
51 |
Correct |
459 ms |
67540 KB |
Output is correct |
52 |
Correct |
446 ms |
68188 KB |
Output is correct |
53 |
Correct |
462 ms |
68636 KB |
Output is correct |
54 |
Correct |
455 ms |
69140 KB |
Output is correct |