#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
vector<vector<int> > to;
struct BIT{
int n;
vector<int> bt;
void init(int x){
n=x;
bt.resize(n+1);
}
void update(int x,int y){
x++;
for(;x<=n;x+=(x&-x)){
bt[x]+=y;
}
}
int query(int x){
x++;
int ans=0;
for(;x>0;x-=(x&-x)){
ans+=bt[x];
}
return ans;
}
};
struct BBIT{
int n;
vector<BIT> v;
BBIT(int x,vector<int> t){
n=x;
v.resize(n+1);
for(int i=1;i<=n;i++){
v[i].init(t[i]);
}
}
void update(int x,int y,int z){
x++;
for(;x<=n;x+=(x&-x)){
v[x].update(lower_bound(to[x].begin(),to[x].end(),y)-to[x].begin(),z);
}
}
int query(int a,int y){
a++;
int ans=0;
for(;a>0;a-=(a&-a)){
ans+=v[a].query(lower_bound(to[a].begin(),to[a].end(),y)-to[a].begin());
}
return ans;
}
};
signed main(){
AquA;
int n,q;
cin >> n >> q;
set<pair<int,int> > s;
string x;
cin >> x;
vector<pair<pair<int,int>,int> > vz;
auto add=[&](int a,int b,int c,int d,int x){
vz.push_back({{a,c},x});
vz.push_back({{a,d+1},-x});
vz.push_back({{b+1,c},-x});
vz.push_back({{b+1,d+1},x});
};
int l=-1,r=0;
for(int i=0;i<n;i++){
if(x[i]=='0'){
if(l==-1){
continue;
}
s.insert({l,r});
l=-1;
}
else{
if(l==-1){
l=i;
}
r=max(r,i);
}
}
if(l!=-1){
s.insert({l,r});
}
for(int i=1;i<=q;i++){
string tf;
cin >> tf;
if(tf[0]=='q'){
int a,b;
cin >> a >> b;
a--;
b--;
int flag=0;
if(s.lower_bound(make_pair(a,1e9))!=s.begin()){
auto h=*prev(s.lower_bound(make_pair(a,1e9)));
if(h.fs<=a && h.sc>=b-1){
flag=i;
}
}
vz.push_back({{a,-b},flag});
//cout << bt.query(a,b)+flag << '\n';
}
else{
int z;
cin >> z;
z--;
if(x[z]=='1'){
auto u=*prev(s.lower_bound(make_pair(z,1e9)));
s.erase(u);
add(u.fs,z,z+1,u.sc+1,i);
if(u.fs<=z-1){
s.insert({u.fs,z-1});
}
if(z+1<=u.sc){
s.insert({z+1,u.sc});
}
x[z]='0';
}
else{
int al=z,ar=z;
auto p=s.lower_bound(make_pair(z,-1));
if(p!=s.begin()){
auto u=prev(p);
if((*u).sc==z-1){
al=(*u).fs;
s.erase(u);
}
}
if(p!=s.end() && (*p).fs==z+1){
ar=(*p).sc;
s.erase(p);
}
//cout << al << " " << z << " " << ar << endl;
add(al,z,z+1,ar+1,-i);
s.insert({al,ar});
x[z]='1';
}
}
}
vector<int> xx(n+6);
to.resize(n+6);
for(auto h:vz){
if(h.fs.sc<0){
for(int i=h.fs.fs+1;i>0;i-=(i&-i)){
to[i].push_back(-h.fs.sc);
}
}
else{
for(int i=h.fs.fs+1;i<=n+5;i+=(i&-i)){
to[i].push_back(h.fs.sc);
}
}
}
for(int i=0;i<=n+5;i++){
sort(to[i].begin(),to[i].end());
to[i].erase(unique(to[i].begin(),to[i].end()),to[i].end());
xx[i]=to[i].size();
}
BBIT bt(n+5,xx);
for(auto h:vz){
if(h.fs.sc<0){
cout << bt.query(h.fs.fs,-h.fs.sc)+h.sc << "\n";
}
else{
bt.update(h.fs.fs,h.fs.sc,h.sc);
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 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 |
297 ms |
46852 KB |
Output is correct |
2 |
Correct |
458 ms |
61608 KB |
Output is correct |
3 |
Correct |
743 ms |
74628 KB |
Output is correct |
4 |
Correct |
1958 ms |
156944 KB |
Output is correct |
5 |
Correct |
1701 ms |
142508 KB |
Output is correct |
6 |
Correct |
1874 ms |
166072 KB |
Output is correct |
7 |
Correct |
945 ms |
84476 KB |
Output is correct |
8 |
Correct |
898 ms |
86364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
712 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
2 ms |
584 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2722 ms |
197440 KB |
Output is correct |
6 |
Correct |
2148 ms |
170408 KB |
Output is correct |
7 |
Correct |
1749 ms |
139764 KB |
Output is correct |
8 |
Correct |
992 ms |
92336 KB |
Output is correct |
9 |
Correct |
118 ms |
17200 KB |
Output is correct |
10 |
Correct |
126 ms |
18836 KB |
Output is correct |
11 |
Correct |
138 ms |
19172 KB |
Output is correct |
12 |
Correct |
906 ms |
90892 KB |
Output is correct |
13 |
Correct |
986 ms |
92204 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 |
724 KB |
Output is correct |
4 |
Correct |
3 ms |
724 KB |
Output is correct |
5 |
Correct |
1086 ms |
96724 KB |
Output is correct |
6 |
Correct |
1490 ms |
134500 KB |
Output is correct |
7 |
Correct |
1915 ms |
168240 KB |
Output is correct |
8 |
Correct |
2541 ms |
201256 KB |
Output is correct |
9 |
Correct |
514 ms |
76316 KB |
Output is correct |
10 |
Correct |
433 ms |
76440 KB |
Output is correct |
11 |
Correct |
527 ms |
76292 KB |
Output is correct |
12 |
Correct |
445 ms |
75400 KB |
Output is correct |
13 |
Correct |
498 ms |
76048 KB |
Output is correct |
14 |
Correct |
428 ms |
75616 KB |
Output is correct |
15 |
Correct |
880 ms |
90936 KB |
Output is correct |
16 |
Correct |
910 ms |
92084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 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 |
297 ms |
46852 KB |
Output is correct |
9 |
Correct |
458 ms |
61608 KB |
Output is correct |
10 |
Correct |
743 ms |
74628 KB |
Output is correct |
11 |
Correct |
1958 ms |
156944 KB |
Output is correct |
12 |
Correct |
1701 ms |
142508 KB |
Output is correct |
13 |
Correct |
1874 ms |
166072 KB |
Output is correct |
14 |
Correct |
945 ms |
84476 KB |
Output is correct |
15 |
Correct |
898 ms |
86364 KB |
Output is correct |
16 |
Correct |
3 ms |
712 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
584 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
2722 ms |
197440 KB |
Output is correct |
21 |
Correct |
2148 ms |
170408 KB |
Output is correct |
22 |
Correct |
1749 ms |
139764 KB |
Output is correct |
23 |
Correct |
992 ms |
92336 KB |
Output is correct |
24 |
Correct |
118 ms |
17200 KB |
Output is correct |
25 |
Correct |
126 ms |
18836 KB |
Output is correct |
26 |
Correct |
138 ms |
19172 KB |
Output is correct |
27 |
Correct |
906 ms |
90892 KB |
Output is correct |
28 |
Correct |
986 ms |
92204 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 |
724 KB |
Output is correct |
32 |
Correct |
3 ms |
724 KB |
Output is correct |
33 |
Correct |
1086 ms |
96724 KB |
Output is correct |
34 |
Correct |
1490 ms |
134500 KB |
Output is correct |
35 |
Correct |
1915 ms |
168240 KB |
Output is correct |
36 |
Correct |
2541 ms |
201256 KB |
Output is correct |
37 |
Correct |
514 ms |
76316 KB |
Output is correct |
38 |
Correct |
433 ms |
76440 KB |
Output is correct |
39 |
Correct |
527 ms |
76292 KB |
Output is correct |
40 |
Correct |
445 ms |
75400 KB |
Output is correct |
41 |
Correct |
498 ms |
76048 KB |
Output is correct |
42 |
Correct |
428 ms |
75616 KB |
Output is correct |
43 |
Correct |
880 ms |
90936 KB |
Output is correct |
44 |
Correct |
910 ms |
92084 KB |
Output is correct |
45 |
Correct |
122 ms |
27424 KB |
Output is correct |
46 |
Correct |
224 ms |
35548 KB |
Output is correct |
47 |
Correct |
948 ms |
84260 KB |
Output is correct |
48 |
Correct |
1886 ms |
157508 KB |
Output is correct |
49 |
Correct |
162 ms |
20540 KB |
Output is correct |
50 |
Correct |
144 ms |
20140 KB |
Output is correct |
51 |
Correct |
153 ms |
21096 KB |
Output is correct |
52 |
Correct |
201 ms |
24312 KB |
Output is correct |
53 |
Correct |
178 ms |
24236 KB |
Output is correct |
54 |
Correct |
202 ms |
24288 KB |
Output is correct |