#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <ctime>
#include <algorithm>
#include <sstream>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <cstdlib>
#include <cstdio>
#include <iterator>
#include <functional>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <bitset>
#include <random>
#include <chrono>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ld long double
const ll maxn = 1e6 + 1, maxm = 80;
const ll mod = 1e9 + 7, inf = 1e9, blck = 500;
const ld eps = 1e-9;
int n, q;
string s, type;
set<int> setik;
ll rng(){
return (((ll)rand() << 15) + rand());
}
struct tnode{
ll pos, sum, y, prior;
tnode *l, *r;
tnode(int x, int z){
pos = x;
y = z;
sum = z;
prior = rng();
l = r = nullptr;
}
};
struct treap{
ll getsum(tnode* v){
return (v ? v->sum : 0);
}
void pull(tnode* v){
v->sum = getsum(v->l) + v->y + getsum(v->r);
}
pair<tnode*, tnode*> split(tnode* &v, int val){
if (v == nullptr)
return {v, v};
if (v->pos < val){
pair<tnode*, tnode*> splitted = split(v->r, val);
v->r = splitted.f;
pull(v);
return mp(v, splitted.s);
}
else{
pair<tnode*, tnode*> splitted = split(v->l, val);
v->l = splitted.s;
pull(v);
return mp(splitted.f, v);
}
}
tnode *merge(tnode *l, tnode *r){
if (l == nullptr || r == nullptr){
return (l ? l : r);
}
if (l->prior > r->prior){
l->r = merge(l->r, r);
pull(l);
return l;
}
else{
r->l = merge(l, r->l);
pull(r);
return r;
}
}
int is(tnode* &v, int x, ll z){
if (v == nullptr)
return 0;
if (v->pos == x){
v->y += z;
pull(v);
return 1;
}
int ok = 0;
if (v->pos < x){
ok = is(v->r, x, z);
}
else{
ok = is(v->l, x, z);
}
pull(v);
return ok;
}
void insval(tnode* &v, int x, int z){
if (is(v, x, z)){
return;
}
pair<tnode*, tnode*> split1 = split(v, x);
/* if (z == -4){
cout << "kek\n";
cout << x << '\n';
cout << split1.f->pos << '\n';
cout << split1.f->sum << " " << split1.f->y << '\n';
cout << "aloha\n";
cout << split1.s->pos << '\n';
cout << split1.s->sum << " " << split1.s->y << '\n';;
cout << z << '\n';
}*/
tnode* cur = new tnode(x, z);
cur = merge(split1.f, cur);
/* if (z == -4){
cout << cur->sum << '\n';
}*/
v = merge(cur, split1.s);
/*if (z == -4){
cout << v->sum << '\n';
cout << v->y << '\n';
}*/
return;
}
int get(int r, tnode* &v){
if (v == nullptr)
return 0;
if (v->pos <= r){
return getsum(v->l) + v->y + get(r, v->r);
}
else{
return get(r, v->l);
}
}
} tree;
/*
kek
2
1
3 3
aloha
3
4 1
-4
-1
3
1
4 1 2 4
*/
struct seg_tree{
tnode *t[maxn << 2];
void upd(int l, int r, int l2, int r2, int add, int v = 1, int tl = 1, int tr = n){
if (l > tr || r < tl)
return;
if (l <= tl && tr <= r){
tree.insval(t[v], l2, add);
tree.insval(t[v], r2, -add);
// cout << v << " " << l2 << " " << r2 << " " << add << '\n';
return;
}
int tm = (tl + tr) / 2;
upd(l, r, l2, r2, add, v + v, tl, tm);
upd(l, r, l2, r2, add, v + v + 1, tm + 1, tr);
}
int get(int l, int r, int v = 1, int tl = 1, int tr = n){
if (l > tr || r < tl)
return 0;
if (tl == tr){
int kek = tree.get(r, t[v]);
// cout << v << " " << kek << '\n';
return kek;
}
int tm = (tl + tr) / 2;
int add = tree.get(r, t[v]);
// cout << "sum\n";
// cout << v << " " << tl << " " << tr << " " << add << '\n';
if (l <= tm){
add += get(l, r, v + v, tl, tm);
}
else{
add += get(l, r, v + v + 1, tm + 1, tr);
}
return add;
}
} tree2;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
cin >> s;
s = '#'+ s;
setik.insert(0);
setik.insert(n + 1);
for (int i = 1; i <= n; ++i){
if (s[i] == '0'){
setik.insert(i);
}
}
for (int x, l, r, i = 1; i <= q; ++i){
cin >> type;
if (type == "toggle"){
cin >> x;
if (s[x] == '0'){
setik.erase(x);
auto it = setik.upper_bound(x);
r = *it;
l = *(--it) + 1;
tree2.upd(l, x, x, r, -i);
s[x] = '1';
}
else{
auto it = setik.upper_bound(x);
r = *it;
l = *(--it) + 1;
setik.insert(x);
tree2.upd(l, x, x, r, i);
s[x] = '0';
}
// cout << tree.get(1, tree2.t[4]) << " " << tree.get(2, tree2.t[4]) << " " << tree.get(3, tree2.t[4]) << '\n';
}
else{
cin >> l >> r;
int add = 0;
auto it = setik.lower_bound(l);
if (*it > r - 1){
add = 1;
}
// cout << tree2.get(l, r - 1) << '\n';
cout << tree2.get(l, r - 1) + add * i << '\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
133 ms |
4492 KB |
Output is correct |
2 |
Correct |
148 ms |
5060 KB |
Output is correct |
3 |
Correct |
213 ms |
8788 KB |
Output is correct |
4 |
Correct |
408 ms |
42424 KB |
Output is correct |
5 |
Correct |
404 ms |
49340 KB |
Output is correct |
6 |
Correct |
433 ms |
45428 KB |
Output is correct |
7 |
Correct |
365 ms |
21168 KB |
Output is correct |
8 |
Correct |
134 ms |
8564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
554 ms |
71968 KB |
Output is correct |
6 |
Correct |
485 ms |
61928 KB |
Output is correct |
7 |
Correct |
399 ms |
48096 KB |
Output is correct |
8 |
Correct |
129 ms |
7528 KB |
Output is correct |
9 |
Correct |
69 ms |
3980 KB |
Output is correct |
10 |
Correct |
83 ms |
4368 KB |
Output is correct |
11 |
Correct |
79 ms |
4532 KB |
Output is correct |
12 |
Correct |
346 ms |
20128 KB |
Output is correct |
13 |
Correct |
128 ms |
7592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
289 ms |
20132 KB |
Output is correct |
6 |
Correct |
376 ms |
35124 KB |
Output is correct |
7 |
Correct |
415 ms |
45036 KB |
Output is correct |
8 |
Correct |
460 ms |
55536 KB |
Output is correct |
9 |
Correct |
165 ms |
4364 KB |
Output is correct |
10 |
Correct |
147 ms |
3484 KB |
Output is correct |
11 |
Correct |
151 ms |
4352 KB |
Output is correct |
12 |
Correct |
137 ms |
3388 KB |
Output is correct |
13 |
Correct |
160 ms |
4260 KB |
Output is correct |
14 |
Correct |
136 ms |
3420 KB |
Output is correct |
15 |
Correct |
348 ms |
21288 KB |
Output is correct |
16 |
Correct |
126 ms |
8700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
133 ms |
4492 KB |
Output is correct |
9 |
Correct |
148 ms |
5060 KB |
Output is correct |
10 |
Correct |
213 ms |
8788 KB |
Output is correct |
11 |
Correct |
408 ms |
42424 KB |
Output is correct |
12 |
Correct |
404 ms |
49340 KB |
Output is correct |
13 |
Correct |
433 ms |
45428 KB |
Output is correct |
14 |
Correct |
365 ms |
21168 KB |
Output is correct |
15 |
Correct |
134 ms |
8564 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
554 ms |
71968 KB |
Output is correct |
21 |
Correct |
485 ms |
61928 KB |
Output is correct |
22 |
Correct |
399 ms |
48096 KB |
Output is correct |
23 |
Correct |
129 ms |
7528 KB |
Output is correct |
24 |
Correct |
69 ms |
3980 KB |
Output is correct |
25 |
Correct |
83 ms |
4368 KB |
Output is correct |
26 |
Correct |
79 ms |
4532 KB |
Output is correct |
27 |
Correct |
346 ms |
20128 KB |
Output is correct |
28 |
Correct |
128 ms |
7592 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
289 ms |
20132 KB |
Output is correct |
34 |
Correct |
376 ms |
35124 KB |
Output is correct |
35 |
Correct |
415 ms |
45036 KB |
Output is correct |
36 |
Correct |
460 ms |
55536 KB |
Output is correct |
37 |
Correct |
165 ms |
4364 KB |
Output is correct |
38 |
Correct |
147 ms |
3484 KB |
Output is correct |
39 |
Correct |
151 ms |
4352 KB |
Output is correct |
40 |
Correct |
137 ms |
3388 KB |
Output is correct |
41 |
Correct |
160 ms |
4260 KB |
Output is correct |
42 |
Correct |
136 ms |
3420 KB |
Output is correct |
43 |
Correct |
348 ms |
21288 KB |
Output is correct |
44 |
Correct |
126 ms |
8700 KB |
Output is correct |
45 |
Correct |
63 ms |
2632 KB |
Output is correct |
46 |
Correct |
92 ms |
2808 KB |
Output is correct |
47 |
Correct |
223 ms |
20016 KB |
Output is correct |
48 |
Correct |
394 ms |
42040 KB |
Output is correct |
49 |
Correct |
113 ms |
4444 KB |
Output is correct |
50 |
Correct |
94 ms |
4336 KB |
Output is correct |
51 |
Correct |
81 ms |
4556 KB |
Output is correct |
52 |
Correct |
90 ms |
4852 KB |
Output is correct |
53 |
Correct |
90 ms |
4812 KB |
Output is correct |
54 |
Correct |
89 ms |
4808 KB |
Output is correct |