#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 3e5 + 20, MOD = (int)1e9+7;
struct bit{
vector<int> t,add;
int n;
void init(int s){
n = s;
t.assign((s + 1) * 4,0);
add.assign((s + 1) * 4,0);
}
void inc(int v,int val){
add[v] += val;
t[v] += val;
}
void push(int v)
{
if(add[v]){
inc(v+v,add[v]);
inc(v+v+1,add[v]);
add[v] = 0;
}
}
void upd(int l,int r,int val,int v,int tl,int tr){
if(l > r ||tl >r || l > tr) return;
if(tl >= l && tr <= r){
inc(v,val);
}else{
push(v);
int tm = (tl + tr) >> 1;
upd(l,r,val,v+v,tl,tm);
upd(l,r,val,v+v+1,tm+1,tr);
t[v] = t[v + v] + t[v + v + 1];
}
}
int get(int l,int r,int v,int tl,int tr){
if(l > r || tl >r || l > tr) return 0;
if(tl >= l && tr <= r) return t[v];
push(v);
int tm = (tl + tr) >> 1;
return get(l,r,v+v,tl,tm) + get(l,r,v+v+1,tm+1,tr);
}
};
vector<pair<int,int>> qr;
int n,q,a[N];
vector<int> t[N * 4],f[N];
bit b[N * 4];
void build(int v = 1,int tl = 1,int tr = n){
if(tl == tr){
t[v] = f[tl];
t[v].resize(unique(t[v].begin(),t[v].end()) - t[v].begin());
b[v].init((int)t[v].size());
}else{
int tm = (tl + tr) >> 1;
build(v+v,tl,tm);
build(v + v + 1,tm + 1,tr);
t[v].resize((int)t[v+v].size() + (int)t[v +v + 1].size());
merge(t[v + v].begin(),t[v + v].end(),t[v + v + 1].begin(),t[v + v + 1].end(),t[v].begin());
t[v].resize(unique(t[v].begin(),t[v].end()) - t[v].begin());
b[v].init((int)t[v].size());
}
}
void del(set<pair<int,int>> &st,int pos){
auto it = st.upper_bound({pos,(int)1e9});
it--;
auto [l,r] = *it;
st.erase(it);
if(l != pos){
st.insert({l,pos-1});
}
if(r != pos){
st.insert({pos+1,r});
}
}
pair<int,int> ins(set<pair<int,int>> &st,int pos){
if(st.empty()){
st.insert({pos,pos});
return {pos,pos};
}
auto it = st.upper_bound({pos,(int)1e9});
if(it == st.end() || (*it).first != pos + 1){
if(it == st.begin()){
st.insert({pos,pos});
return {pos,pos};
}
it--;
if((*it).second + 1 != pos){
st.insert({pos,pos});
return {pos,pos};
}
int L = (*it).first;
st.insert({L,pos});
st.erase(it);
return {L,pos};
}
int R = (*it).second;
if(it == st.begin()){
st.erase(it);
st.insert({pos,R});
return {pos,R};
}
it--;
if((*it).second + 1 != pos){
it++;
st.erase(it);
st.insert({pos,R});
return {pos,R};
}
int L = (*it).first;
auto it1 = it;
it1++;
st.erase(it1);
st.erase(it);
st.insert({L,R});
return {L,R};
}
void upd(int l,int r,int l1,int r1,int val,int v = 1,int tl = 1,int tr = n){
if(l > r || tl > r || l > tr) return;
if(tl >= l && tr <= r){
int it = lower_bound(t[v].begin(),t[v].end(),l1) - t[v].begin() + 1;
int it1 = upper_bound(t[v].begin(),t[v].end(),r1) - t[v].begin();
b[v].upd(it,it1,val,1,1,b[v].n);
}else{
int tm = (tl + tr) >> 1;
upd(l,r,l1,r1,val,v+v,tl,tm);
upd(l,r,l1,r1,val,v+v+1,tm+1,tr);
}
}
int res = 0;
void get(int l,int r,int v = 1,int tl = 1,int tr = n){
int it = lower_bound(t[v].begin(),t[v].end(),r) - t[v].begin();
res += b[v].get(it + 1,it + 1,1,1,b[v].n);
if(tl == tr) return;
int tm = (tl + tr) >> 1;
if(l <= tm) get(l,r,v+v,tl,tm);
else get(l,r,v+v+1,tm+1,tr);
}
int re[N];
void test(){
cin >> n >> q;
for(int i = 1;i <= n;i++){
char x;
cin >> x;
a[i] = (x - '0');
}
qr.push_back({-1,-1});
for(int i = 1;i <= q;i++)
{
string tp;cin >> tp;
if(tp == "toggle"){
int x;
cin >> x;
qr.push_back({x,-1});
}else{
int l,r;
cin >> l >> r;
r--;
f[l].push_back(r);
qr.push_back({l,r});
}
}
for(int i = 1;i <= n;i++){
sort(f[i].begin(),f[i].end());
}
build();
set<pair<int,int>> o,z;
int lst = 1;
for(int i = 2;i <= n + 1;i++){
if(i == n + 1 || a[i] != a[lst]){
if(a[lst]){
o.insert({lst,i - 1});
}else{
z.insert({lst,i - 1});
}
lst = i;
}
}
for(int i = 1;i <= q;i++){
auto [L,R] = qr[i];
if(R == -1){
int x = L;
if(a[x]){
auto it = o.upper_bound({x,1e9});
it--;
int l = (*it).first,r = (*it).second;
del(o,x);
ins(z,x);
upd(l,x,x,r,i);
// cout << l << ' ' << r << ' ' << x << '\n';
// for(int j = i + 1;j <= q;j++){
// if(qr[j].second != -1 && qr[j].first >= l && qr[j].first <= x && qr[j].second >= x && qr[j].second <= r){
// re[j] += i;
// }
// }
}else{
del(z,x);
auto [l,r] = ins(o,x);
upd(l,x,x,r,-i);
// for(int j = i + 1;j <= q;j++){
// if(qr[j].second != -1 && qr[j].first >= l && qr[j].first <= x && qr[j].second >= x && qr[j].second <= r){
// // cout << i << "-\n";
// re[j] -= i;
// }
// }
}
a[x] ^= 1;
}else{
res = 0;
get(L,R);
auto it = o.upper_bound({L,1e9});
if(it != o.begin()){
it--;
if((*it).second >= R){
res += i;
re[i] += i;
}
}
cout << res << '\n';
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
int t = 1;
// cin >> t;
while(t--){
test();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
101216 KB |
Output is correct |
2 |
Correct |
58 ms |
101204 KB |
Output is correct |
3 |
Correct |
50 ms |
101344 KB |
Output is correct |
4 |
Correct |
49 ms |
101360 KB |
Output is correct |
5 |
Correct |
45 ms |
101456 KB |
Output is correct |
6 |
Correct |
46 ms |
101456 KB |
Output is correct |
7 |
Correct |
45 ms |
101456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
191 ms |
107280 KB |
Output is correct |
2 |
Correct |
241 ms |
110932 KB |
Output is correct |
3 |
Correct |
475 ms |
114536 KB |
Output is correct |
4 |
Correct |
1667 ms |
248664 KB |
Output is correct |
5 |
Correct |
1660 ms |
262068 KB |
Output is correct |
6 |
Correct |
1293 ms |
233620 KB |
Output is correct |
7 |
Correct |
2095 ms |
294644 KB |
Output is correct |
8 |
Correct |
2302 ms |
297228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
102996 KB |
Output is correct |
2 |
Correct |
36 ms |
103256 KB |
Output is correct |
3 |
Correct |
36 ms |
103392 KB |
Output is correct |
4 |
Correct |
37 ms |
103504 KB |
Output is correct |
5 |
Correct |
559 ms |
153080 KB |
Output is correct |
6 |
Correct |
1211 ms |
223460 KB |
Output is correct |
7 |
Correct |
1767 ms |
285020 KB |
Output is correct |
8 |
Correct |
2339 ms |
357404 KB |
Output is correct |
9 |
Correct |
161 ms |
111192 KB |
Output is correct |
10 |
Correct |
165 ms |
111840 KB |
Output is correct |
11 |
Correct |
190 ms |
112288 KB |
Output is correct |
12 |
Correct |
2358 ms |
355704 KB |
Output is correct |
13 |
Correct |
2261 ms |
357252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
103508 KB |
Output is correct |
2 |
Correct |
37 ms |
103248 KB |
Output is correct |
3 |
Correct |
35 ms |
103248 KB |
Output is correct |
4 |
Correct |
35 ms |
103164 KB |
Output is correct |
5 |
Correct |
2574 ms |
360928 KB |
Output is correct |
6 |
Correct |
1970 ms |
304828 KB |
Output is correct |
7 |
Correct |
1229 ms |
244712 KB |
Output is correct |
8 |
Correct |
509 ms |
157640 KB |
Output is correct |
9 |
Correct |
275 ms |
115496 KB |
Output is correct |
10 |
Correct |
179 ms |
109816 KB |
Output is correct |
11 |
Correct |
275 ms |
115380 KB |
Output is correct |
12 |
Correct |
175 ms |
109804 KB |
Output is correct |
13 |
Correct |
276 ms |
115392 KB |
Output is correct |
14 |
Correct |
179 ms |
109772 KB |
Output is correct |
15 |
Correct |
2344 ms |
355640 KB |
Output is correct |
16 |
Correct |
2286 ms |
357300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
101216 KB |
Output is correct |
2 |
Correct |
58 ms |
101204 KB |
Output is correct |
3 |
Correct |
50 ms |
101344 KB |
Output is correct |
4 |
Correct |
49 ms |
101360 KB |
Output is correct |
5 |
Correct |
45 ms |
101456 KB |
Output is correct |
6 |
Correct |
46 ms |
101456 KB |
Output is correct |
7 |
Correct |
45 ms |
101456 KB |
Output is correct |
8 |
Correct |
191 ms |
107280 KB |
Output is correct |
9 |
Correct |
241 ms |
110932 KB |
Output is correct |
10 |
Correct |
475 ms |
114536 KB |
Output is correct |
11 |
Correct |
1667 ms |
248664 KB |
Output is correct |
12 |
Correct |
1660 ms |
262068 KB |
Output is correct |
13 |
Correct |
1293 ms |
233620 KB |
Output is correct |
14 |
Correct |
2095 ms |
294644 KB |
Output is correct |
15 |
Correct |
2302 ms |
297228 KB |
Output is correct |
16 |
Correct |
34 ms |
102996 KB |
Output is correct |
17 |
Correct |
36 ms |
103256 KB |
Output is correct |
18 |
Correct |
36 ms |
103392 KB |
Output is correct |
19 |
Correct |
37 ms |
103504 KB |
Output is correct |
20 |
Correct |
559 ms |
153080 KB |
Output is correct |
21 |
Correct |
1211 ms |
223460 KB |
Output is correct |
22 |
Correct |
1767 ms |
285020 KB |
Output is correct |
23 |
Correct |
2339 ms |
357404 KB |
Output is correct |
24 |
Correct |
161 ms |
111192 KB |
Output is correct |
25 |
Correct |
165 ms |
111840 KB |
Output is correct |
26 |
Correct |
190 ms |
112288 KB |
Output is correct |
27 |
Correct |
2358 ms |
355704 KB |
Output is correct |
28 |
Correct |
2261 ms |
357252 KB |
Output is correct |
29 |
Correct |
35 ms |
103508 KB |
Output is correct |
30 |
Correct |
37 ms |
103248 KB |
Output is correct |
31 |
Correct |
35 ms |
103248 KB |
Output is correct |
32 |
Correct |
35 ms |
103164 KB |
Output is correct |
33 |
Correct |
2574 ms |
360928 KB |
Output is correct |
34 |
Correct |
1970 ms |
304828 KB |
Output is correct |
35 |
Correct |
1229 ms |
244712 KB |
Output is correct |
36 |
Correct |
509 ms |
157640 KB |
Output is correct |
37 |
Correct |
275 ms |
115496 KB |
Output is correct |
38 |
Correct |
179 ms |
109816 KB |
Output is correct |
39 |
Correct |
275 ms |
115380 KB |
Output is correct |
40 |
Correct |
175 ms |
109804 KB |
Output is correct |
41 |
Correct |
276 ms |
115392 KB |
Output is correct |
42 |
Correct |
179 ms |
109772 KB |
Output is correct |
43 |
Correct |
2344 ms |
355640 KB |
Output is correct |
44 |
Correct |
2286 ms |
357300 KB |
Output is correct |
45 |
Correct |
121 ms |
108036 KB |
Output is correct |
46 |
Correct |
159 ms |
108288 KB |
Output is correct |
47 |
Correct |
759 ms |
185924 KB |
Output is correct |
48 |
Correct |
1650 ms |
265040 KB |
Output is correct |
49 |
Correct |
187 ms |
112796 KB |
Output is correct |
50 |
Correct |
187 ms |
112672 KB |
Output is correct |
51 |
Correct |
193 ms |
113176 KB |
Output is correct |
52 |
Correct |
279 ms |
121764 KB |
Output is correct |
53 |
Correct |
284 ms |
121856 KB |
Output is correct |
54 |
Correct |
286 ms |
121812 KB |
Output is correct |