#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#define all(v) v.begin(),v.end()
using namespace std;
struct interval{
int l,r,t;
bool operator<(const interval &tmp)const{
if (l!=tmp.l)
return l<tmp.l;
return r<tmp.r;
}
};
struct query{
int t,mode,x,y;
};
int N,Q;
string S;
set<interval> Set;
vector<query> Queries[300001];
int Ans[300001];
int Tree[300001];
void fenwickUpdate(int x,int val){
while (x<=N){
Tree[x]+=val;
x+=x&-x;
}
}
int fenwickQuery(int x){
int ret=0;
while (x){
ret+=Tree[x];
x-=x&-x;
}
return ret;
}
void solve(int l,int r){
if (l==r)
return;
int m=(l+r)>>1;
solve(l,m);
solve(m+1,r);
vector<query> qv;
for (int i=l; i<=m; i++){
for (query j:Queries[i]){
if (!j.mode)
qv.push_back(j);
}
}
for (int i=m+1; i<=r; i++){
for (query j:Queries[i]){
if (j.mode)
qv.push_back(j);
}
}
stable_sort(all(qv),[&](query a,query b)->bool{
return a.t<b.t;
});
for (query i:qv){
if (i.mode)
Ans[i.x]+=fenwickQuery(i.y)*i.mode;
else
fenwickUpdate(i.x,i.y);
}
for (query i:qv){
if (!i.mode)
fenwickUpdate(i.x,-i.y);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>N>>Q>>S;
for (int i=1,j=0; i<=N; i++){
if (S[i-1]=='1'){
if (!j)
j=i;
if (i==N||S[i]=='0'){
Set.insert({j,i,0});
j=0;
}
}
}
fill(Ans+1,Ans+Q+1,-1);
for (int t=1; t<=Q; t++){
string s;
cin>>s;
if (s[0]=='t'){
int x;
cin>>x;
auto it=Set.lower_bound({x+1,x+1,0});
if (it!=Set.begin()&&prev(it)->r>=x){//x가 켜져 있음
interval cur=*(--it);
Queries[t].push_back({cur.r,0,cur.l,t-cur.t});
Set.erase(it);
if (cur.l<x)
Set.insert({cur.l,x-1,t});
if (x<cur.r)
Set.insert({x+1,cur.r,t});
}
else{//x가 꺼져 있음
interval cur={x,x,t};
if (it!=Set.begin()&&prev(it)->r==x-1){
cur.l=(--it)->l;
Queries[t].push_back({it->r,0,it->l,t-it->t});
it=Set.erase(it);
}
if (it!=Set.end()&&it->l==x+1){
cur.r=it->r;
Queries[t].push_back({it->r,0,it->l,t-it->t});
Set.erase(it);
}
Set.insert(cur);
}
}
else{
Ans[t]=0;
int l,r;
cin>>l>>r;
r--;
auto it=Set.lower_bound({l+1,l+1,0});
if (it!=Set.begin()&&prev(it)->r>=r)
Ans[t]+=t-prev(it)->t;
Queries[t].push_back({r-1,-1,t,l});
Queries[t].push_back({N,1,t,l});
}
}
solve(1,Q);
for (int i=1; i<=Q; i++){
if (Ans[i]!=-1)
cout<<Ans[i]<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9496 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
342 ms |
28360 KB |
Output is correct |
2 |
Correct |
366 ms |
27860 KB |
Output is correct |
3 |
Correct |
407 ms |
28464 KB |
Output is correct |
4 |
Correct |
524 ms |
34192 KB |
Output is correct |
5 |
Correct |
558 ms |
42924 KB |
Output is correct |
6 |
Correct |
492 ms |
44260 KB |
Output is correct |
7 |
Correct |
376 ms |
43216 KB |
Output is correct |
8 |
Correct |
379 ms |
42740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9564 KB |
Output is correct |
2 |
Correct |
3 ms |
9564 KB |
Output is correct |
3 |
Correct |
3 ms |
9788 KB |
Output is correct |
4 |
Correct |
3 ms |
9564 KB |
Output is correct |
5 |
Correct |
418 ms |
22284 KB |
Output is correct |
6 |
Correct |
533 ms |
33632 KB |
Output is correct |
7 |
Correct |
559 ms |
43280 KB |
Output is correct |
8 |
Correct |
371 ms |
42808 KB |
Output is correct |
9 |
Correct |
179 ms |
31568 KB |
Output is correct |
10 |
Correct |
200 ms |
39424 KB |
Output is correct |
11 |
Correct |
199 ms |
38168 KB |
Output is correct |
12 |
Correct |
368 ms |
42328 KB |
Output is correct |
13 |
Correct |
374 ms |
42376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
9560 KB |
Output is correct |
2 |
Correct |
3 ms |
9820 KB |
Output is correct |
3 |
Correct |
3 ms |
9564 KB |
Output is correct |
4 |
Correct |
3 ms |
9564 KB |
Output is correct |
5 |
Correct |
471 ms |
41872 KB |
Output is correct |
6 |
Correct |
500 ms |
39960 KB |
Output is correct |
7 |
Correct |
480 ms |
40020 KB |
Output is correct |
8 |
Correct |
462 ms |
30524 KB |
Output is correct |
9 |
Correct |
324 ms |
33008 KB |
Output is correct |
10 |
Correct |
277 ms |
26192 KB |
Output is correct |
11 |
Correct |
324 ms |
33944 KB |
Output is correct |
12 |
Correct |
295 ms |
25584 KB |
Output is correct |
13 |
Correct |
322 ms |
32452 KB |
Output is correct |
14 |
Correct |
278 ms |
24812 KB |
Output is correct |
15 |
Correct |
371 ms |
38280 KB |
Output is correct |
16 |
Correct |
367 ms |
37272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9496 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
342 ms |
28360 KB |
Output is correct |
9 |
Correct |
366 ms |
27860 KB |
Output is correct |
10 |
Correct |
407 ms |
28464 KB |
Output is correct |
11 |
Correct |
524 ms |
34192 KB |
Output is correct |
12 |
Correct |
558 ms |
42924 KB |
Output is correct |
13 |
Correct |
492 ms |
44260 KB |
Output is correct |
14 |
Correct |
376 ms |
43216 KB |
Output is correct |
15 |
Correct |
379 ms |
42740 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
3 ms |
9788 KB |
Output is correct |
19 |
Correct |
3 ms |
9564 KB |
Output is correct |
20 |
Correct |
418 ms |
22284 KB |
Output is correct |
21 |
Correct |
533 ms |
33632 KB |
Output is correct |
22 |
Correct |
559 ms |
43280 KB |
Output is correct |
23 |
Correct |
371 ms |
42808 KB |
Output is correct |
24 |
Correct |
179 ms |
31568 KB |
Output is correct |
25 |
Correct |
200 ms |
39424 KB |
Output is correct |
26 |
Correct |
199 ms |
38168 KB |
Output is correct |
27 |
Correct |
368 ms |
42328 KB |
Output is correct |
28 |
Correct |
374 ms |
42376 KB |
Output is correct |
29 |
Correct |
3 ms |
9560 KB |
Output is correct |
30 |
Correct |
3 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
471 ms |
41872 KB |
Output is correct |
34 |
Correct |
500 ms |
39960 KB |
Output is correct |
35 |
Correct |
480 ms |
40020 KB |
Output is correct |
36 |
Correct |
462 ms |
30524 KB |
Output is correct |
37 |
Correct |
324 ms |
33008 KB |
Output is correct |
38 |
Correct |
277 ms |
26192 KB |
Output is correct |
39 |
Correct |
324 ms |
33944 KB |
Output is correct |
40 |
Correct |
295 ms |
25584 KB |
Output is correct |
41 |
Correct |
322 ms |
32452 KB |
Output is correct |
42 |
Correct |
278 ms |
24812 KB |
Output is correct |
43 |
Correct |
371 ms |
38280 KB |
Output is correct |
44 |
Correct |
367 ms |
37272 KB |
Output is correct |
45 |
Correct |
189 ms |
26480 KB |
Output is correct |
46 |
Correct |
223 ms |
26344 KB |
Output is correct |
47 |
Correct |
306 ms |
30104 KB |
Output is correct |
48 |
Correct |
511 ms |
39628 KB |
Output is correct |
49 |
Correct |
237 ms |
41088 KB |
Output is correct |
50 |
Correct |
228 ms |
39780 KB |
Output is correct |
51 |
Correct |
221 ms |
40660 KB |
Output is correct |
52 |
Correct |
226 ms |
40440 KB |
Output is correct |
53 |
Correct |
222 ms |
41928 KB |
Output is correct |
54 |
Correct |
222 ms |
40300 KB |
Output is correct |