#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;
template<class T>
struct node
{
T val = {};
node *l = NULL, *r = NULL;
};
template<class T, class F>
void up(int l, int r, F f, node<T> *t, int b, int e)
{
//cerr << "up " << l << ' ' << r << " -> " << b << ' ' << e << '\n';
if (l <= b && e <= r) {
f(&t->val);
return;
}
int mid = (b+e)/2;
if (l < mid) {
if (t->l == NULL)
t->l = new node<T>;
up(l, r, f, t->l, b, mid);
}
if (mid < r) {
if (t->r == NULL)
t->r = new node<T>;
up(l, r, f, t->r, mid, e);
}
}
template<class T, class F>
int get(int p, F f, node<T> *t, int b, int e)
{
//cerr << "get " << p << " -> " << b << ' ' << e << '\n';
int ans = f(&t->val);
if (p < (b+e)/2 && t->l)
ans += get(p, f, t->l, b, (b+e)/2);
if (p >= (b+e)/2 && t->r)
ans += get(p, f, t->r, (b+e)/2, e);
return ans;
}
const int N = 300'010;
string s;
int n, q;
set<pii> inter;
node<node<pii>> rt;
template<class F>
void up2d(int l, int r, F f)
{
if (l >= r)
return;
up(l, r, [=](node<pii> *t) {
up(l, r, f, t, 0, n);
}, &rt, 0, n);
}
void init()
{
s.push_back('0');
int lst = -1;
Loop (i,0,n+1) {
if (lst == -1 && s[i] == '1') {
lst = i;
} else if (lst != -1 && s[i] != '1') {
inter.insert({lst, i});
lst = -1;
}
}
for (auto [l, r] : inter) {
//cerr << l << ' ' << r << " added\n";
up2d(l, r, [](pii *x){*x = {1, 0};});
}
}
void toggle(int i, int t)
{
if (s[i] == '1') {
auto it = --inter.upper_bound({i, N});
auto [l, r] = *it;
inter.erase(it);
up2d(l , r, [=](pii *x){*x = {x->first-1, x->second+t};});
up2d(l , i, [=](pii *x){*x = {x->first+1, x->second-t};});
up2d(i+1, r, [=](pii *x){*x = {x->first+1, x->second-t};});
s[i] = '0';
if (l < i)
inter.insert({l, i});
if (i+1 < r)
inter.insert({i+1, r});
} else {
auto it2 = inter.upper_bound({i, N});
auto it1 = it2;
int l = i, r = i+1;
if (it2 != inter.end() && it2->first == i+1)
r = it2->second;
if (it1 != inter.begin() && (--it1)->second == i)
l = it1->first;
if (r != i+1)
inter.erase(it2);
if (l != i)
inter.erase(it1);
up2d(l , i, [=](pii *x){*x = {x->first-1, x->second+t};});
up2d(i+1, r, [=](pii *x){*x = {x->first-1, x->second+t};});
up2d(l , r, [=](pii *x){*x = {x->first+1, x->second-t};});
s[i] = '1';
inter.insert({l, r});
}
}
int get2d(int l, int r, int t)
{
return get(l, [=](node<pii> *nd) {
return get(r-1, [=](pii *x) {
return x->first * t + x->second;
}, nd, 0, n);
}, &rt, 0, n);
}
int main()
{
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> q;
cin >> s;
init();
Loop (t,1,q+1) {
string s;
cin >> s;
if (s == "toggle") {
int i;
cin >> i;
toggle(i-1, t);
}
if (s == "query") {
int a, b;
cin >> a >> b;
--a; --b;
if (a > b)
swap(a, b);
cout << get2d(a, b, t) << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
1352 KB |
Output is correct |
2 |
Correct |
216 ms |
1688 KB |
Output is correct |
3 |
Correct |
377 ms |
8004 KB |
Output is correct |
4 |
Correct |
853 ms |
159644 KB |
Output is correct |
5 |
Correct |
1102 ms |
228388 KB |
Output is correct |
6 |
Correct |
903 ms |
170888 KB |
Output is correct |
7 |
Correct |
103 ms |
1244 KB |
Output is correct |
8 |
Correct |
110 ms |
2708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
3 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1377 ms |
274568 KB |
Output is correct |
6 |
Correct |
1220 ms |
266924 KB |
Output is correct |
7 |
Correct |
989 ms |
232764 KB |
Output is correct |
8 |
Correct |
108 ms |
8596 KB |
Output is correct |
9 |
Correct |
83 ms |
4056 KB |
Output is correct |
10 |
Correct |
99 ms |
4280 KB |
Output is correct |
11 |
Correct |
97 ms |
4472 KB |
Output is correct |
12 |
Correct |
102 ms |
7256 KB |
Output is correct |
13 |
Correct |
118 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
5 |
Correct |
375 ms |
90124 KB |
Output is correct |
6 |
Correct |
605 ms |
134320 KB |
Output is correct |
7 |
Correct |
862 ms |
170420 KB |
Output is correct |
8 |
Correct |
1165 ms |
209064 KB |
Output is correct |
9 |
Correct |
236 ms |
1204 KB |
Output is correct |
10 |
Correct |
221 ms |
440 KB |
Output is correct |
11 |
Correct |
235 ms |
1020 KB |
Output is correct |
12 |
Correct |
272 ms |
460 KB |
Output is correct |
13 |
Correct |
242 ms |
1020 KB |
Output is correct |
14 |
Correct |
231 ms |
460 KB |
Output is correct |
15 |
Correct |
137 ms |
1352 KB |
Output is correct |
16 |
Correct |
103 ms |
2596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
191 ms |
1352 KB |
Output is correct |
9 |
Correct |
216 ms |
1688 KB |
Output is correct |
10 |
Correct |
377 ms |
8004 KB |
Output is correct |
11 |
Correct |
853 ms |
159644 KB |
Output is correct |
12 |
Correct |
1102 ms |
228388 KB |
Output is correct |
13 |
Correct |
903 ms |
170888 KB |
Output is correct |
14 |
Correct |
103 ms |
1244 KB |
Output is correct |
15 |
Correct |
110 ms |
2708 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
3 ms |
852 KB |
Output is correct |
18 |
Correct |
2 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1377 ms |
274568 KB |
Output is correct |
21 |
Correct |
1220 ms |
266924 KB |
Output is correct |
22 |
Correct |
989 ms |
232764 KB |
Output is correct |
23 |
Correct |
108 ms |
8596 KB |
Output is correct |
24 |
Correct |
83 ms |
4056 KB |
Output is correct |
25 |
Correct |
99 ms |
4280 KB |
Output is correct |
26 |
Correct |
97 ms |
4472 KB |
Output is correct |
27 |
Correct |
102 ms |
7256 KB |
Output is correct |
28 |
Correct |
118 ms |
8628 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
724 KB |
Output is correct |
33 |
Correct |
375 ms |
90124 KB |
Output is correct |
34 |
Correct |
605 ms |
134320 KB |
Output is correct |
35 |
Correct |
862 ms |
170420 KB |
Output is correct |
36 |
Correct |
1165 ms |
209064 KB |
Output is correct |
37 |
Correct |
236 ms |
1204 KB |
Output is correct |
38 |
Correct |
221 ms |
440 KB |
Output is correct |
39 |
Correct |
235 ms |
1020 KB |
Output is correct |
40 |
Correct |
272 ms |
460 KB |
Output is correct |
41 |
Correct |
242 ms |
1020 KB |
Output is correct |
42 |
Correct |
231 ms |
460 KB |
Output is correct |
43 |
Correct |
137 ms |
1352 KB |
Output is correct |
44 |
Correct |
103 ms |
2596 KB |
Output is correct |
45 |
Correct |
78 ms |
2636 KB |
Output is correct |
46 |
Correct |
129 ms |
2756 KB |
Output is correct |
47 |
Correct |
416 ms |
69504 KB |
Output is correct |
48 |
Correct |
784 ms |
164476 KB |
Output is correct |
49 |
Correct |
94 ms |
4444 KB |
Output is correct |
50 |
Correct |
91 ms |
4300 KB |
Output is correct |
51 |
Correct |
95 ms |
4700 KB |
Output is correct |
52 |
Correct |
100 ms |
4932 KB |
Output is correct |
53 |
Correct |
95 ms |
4932 KB |
Output is correct |
54 |
Correct |
102 ms |
4964 KB |
Output is correct |