#include <bits/stdc++.h>
#define ll long long
#define db long double
#define N 300002
#define II pair <int,int>
#define fst first
#define snd second
using namespace std;
set <int> off;
map <II,int> Begin;
int n,i,j,l,r,u,v,q,lg,pos;
string s;
struct trie
{
int sum=0;
trie *c[2];
};
trie *bit[N];
void add(trie *u,int x,int k)
{
for(int i=lg;i>=0;i--)
{
if(u->c[(x>>i)&1]==NULL) u->c[(x>>i)&1]=new trie();
u=u->c[(x>>i)&1];
u->sum+=k;
}
}
int cal(trie *u,int x)
{
int res=0;
for(int i=lg;i>=0;i--)
{
if(!((x>>i)&1))
{
if(u->c[1]!=NULL) res+=u->c[1]->sum;
}
if(u->c[(x>>i)&1]==NULL) return res;
u=u->c[(x>>i)&1];
}
return res+u->sum;
}
void update(int i,int x,int k)
{
while(i<=n) add(bit[i],x,k),i+=(i & -i);
}
int get(int i,int x)
{
int res=0;
while(i>=1) res+=cal(bit[i],x),i-=(i & -i);
return res;
}
int main()
{
// freopen("ntu.inp","r",stdin);
// freopen("ntu.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin>>n>>q>>s; s=" "+s;
lg=trunc(log2(n));
off.insert(0); off.insert(n+1);
for(i=1;i<=n;i++)
{
bit[i]=new trie();
if(s[i]=='0') off.insert(i),pos=i;
else if(i==n || s[i+1]=='0') Begin[{ pos+1,i }]=0;
}
for(j=1;j<=q;j++)
{
string type; cin>>type;
if(type=="toggle")
{
cin>>i;
if(s[i]=='0')
{
s[i]='1'; off.erase(i);
set <int>::iterator it=off.lower_bound(i); it--;
l=(*it)+1; r=*(off.upper_bound(i))-1;
if(l<i) update(l,i-1,j-Begin[{ l,i-1 }]);
if(r>i) update(i+1,r,j-Begin[{ i+1,r }]);
Begin[{ l,r }]=j;
}
else if(s[i]=='1')
{
s[i]='0'; off.insert(i);
set <int>::iterator it=off.lower_bound(i); it--;
l=(*it)+1; r=*(off.upper_bound(i))-1;
if(l<i) Begin[{ l,i-1 }]=j;
if(r>i) Begin[{ i+1,r }]=j;
update(l,r,j-Begin[{ l,r }]);
}
}
else
{
cin>>l>>r; r--;
set <int>::iterator it=off.upper_bound(r);
int u,v=*it;
it--; u=*it;
u++; v--;
if(u<=l && r<=v) cout<<get(l,r)+j-Begin[{ u,v }]<<'\n';
else cout<<get(l,r)<<'\n';
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
4564 KB |
Output is correct |
2 |
Correct |
241 ms |
5216 KB |
Output is correct |
3 |
Correct |
479 ms |
13216 KB |
Output is correct |
4 |
Correct |
1476 ms |
217312 KB |
Output is correct |
5 |
Correct |
1652 ms |
240320 KB |
Output is correct |
6 |
Correct |
1572 ms |
234684 KB |
Output is correct |
7 |
Correct |
432 ms |
32964 KB |
Output is correct |
8 |
Correct |
163 ms |
20408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
720 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2158 ms |
313716 KB |
Output is correct |
6 |
Correct |
1710 ms |
296912 KB |
Output is correct |
7 |
Correct |
1365 ms |
239764 KB |
Output is correct |
8 |
Correct |
161 ms |
20256 KB |
Output is correct |
9 |
Correct |
106 ms |
3920 KB |
Output is correct |
10 |
Correct |
82 ms |
4272 KB |
Output is correct |
11 |
Correct |
83 ms |
4484 KB |
Output is correct |
12 |
Correct |
493 ms |
33092 KB |
Output is correct |
13 |
Correct |
162 ms |
20220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
388 ms |
40848 KB |
Output is correct |
6 |
Correct |
880 ms |
171440 KB |
Output is correct |
7 |
Correct |
1383 ms |
234516 KB |
Output is correct |
8 |
Correct |
1834 ms |
286220 KB |
Output is correct |
9 |
Correct |
213 ms |
4512 KB |
Output is correct |
10 |
Correct |
181 ms |
3436 KB |
Output is correct |
11 |
Correct |
207 ms |
4660 KB |
Output is correct |
12 |
Correct |
174 ms |
3392 KB |
Output is correct |
13 |
Correct |
233 ms |
4612 KB |
Output is correct |
14 |
Correct |
181 ms |
3424 KB |
Output is correct |
15 |
Correct |
393 ms |
32956 KB |
Output is correct |
16 |
Correct |
170 ms |
20300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
328 KB |
Output is correct |
2 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
182 ms |
4564 KB |
Output is correct |
9 |
Correct |
241 ms |
5216 KB |
Output is correct |
10 |
Correct |
479 ms |
13216 KB |
Output is correct |
11 |
Correct |
1476 ms |
217312 KB |
Output is correct |
12 |
Correct |
1652 ms |
240320 KB |
Output is correct |
13 |
Correct |
1572 ms |
234684 KB |
Output is correct |
14 |
Correct |
432 ms |
32964 KB |
Output is correct |
15 |
Correct |
163 ms |
20408 KB |
Output is correct |
16 |
Correct |
2 ms |
852 KB |
Output is correct |
17 |
Correct |
2 ms |
852 KB |
Output is correct |
18 |
Correct |
2 ms |
720 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
2158 ms |
313716 KB |
Output is correct |
21 |
Correct |
1710 ms |
296912 KB |
Output is correct |
22 |
Correct |
1365 ms |
239764 KB |
Output is correct |
23 |
Correct |
161 ms |
20256 KB |
Output is correct |
24 |
Correct |
106 ms |
3920 KB |
Output is correct |
25 |
Correct |
82 ms |
4272 KB |
Output is correct |
26 |
Correct |
83 ms |
4484 KB |
Output is correct |
27 |
Correct |
493 ms |
33092 KB |
Output is correct |
28 |
Correct |
162 ms |
20220 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
724 KB |
Output is correct |
32 |
Correct |
2 ms |
852 KB |
Output is correct |
33 |
Correct |
388 ms |
40848 KB |
Output is correct |
34 |
Correct |
880 ms |
171440 KB |
Output is correct |
35 |
Correct |
1383 ms |
234516 KB |
Output is correct |
36 |
Correct |
1834 ms |
286220 KB |
Output is correct |
37 |
Correct |
213 ms |
4512 KB |
Output is correct |
38 |
Correct |
181 ms |
3436 KB |
Output is correct |
39 |
Correct |
207 ms |
4660 KB |
Output is correct |
40 |
Correct |
174 ms |
3392 KB |
Output is correct |
41 |
Correct |
233 ms |
4612 KB |
Output is correct |
42 |
Correct |
181 ms |
3424 KB |
Output is correct |
43 |
Correct |
393 ms |
32956 KB |
Output is correct |
44 |
Correct |
170 ms |
20300 KB |
Output is correct |
45 |
Correct |
75 ms |
2708 KB |
Output is correct |
46 |
Correct |
102 ms |
2868 KB |
Output is correct |
47 |
Correct |
810 ms |
89020 KB |
Output is correct |
48 |
Correct |
1510 ms |
216944 KB |
Output is correct |
49 |
Correct |
127 ms |
4456 KB |
Output is correct |
50 |
Correct |
92 ms |
4336 KB |
Output is correct |
51 |
Correct |
97 ms |
4604 KB |
Output is correct |
52 |
Correct |
111 ms |
4784 KB |
Output is correct |
53 |
Correct |
95 ms |
4896 KB |
Output is correct |
54 |
Correct |
95 ms |
4940 KB |
Output is correct |