#include <bits/stdc++.h>
// #pragma GCC optimize ("Ofast,unroll-loops")
// #pragma GCC target ("avx2")
using namespace std;
typedef long long ll;
typedef pair<int, int> pp;
#define per(i,r,l) for(int i = (r); i >= (l); i--)
#define rep(i,l,r) for(int i = (l); i < (r); i++)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define pb push_back
#define ss second
#define ff first
void err(istringstream *iss){}template<typename T,typename ...Args> void err(istringstream *iss,const T &_val, const Args&...args){string _name;*iss>>_name;if(_name.back()==',')_name.pop_back();cerr<<_name<<" = "<<_val<<", ",err(iss,args...);}
void IOS(){
cin.tie(0) -> sync_with_stdio(0);
// #ifndef ONLINE_JUDGE
// #define er(args ...) cerr << __LINE__ << ": ", err(new istringstream(string(#args)), args), cerr << endl
// freopen("inp.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
// #else
// #define er(args ...) 0
// #endif
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod = 1e9 + 7, maxn = 3e5 + 5, lg = 22, inf = ll(1e9) + 5;
ll pw(ll a,ll b,ll md=mod){if(!b)return 1;ll k=pw(a,b>>1ll,md);return k*k%md*(b&1ll?a:1)%md;}
vector<ll> fen[maxn], srt[maxn];
void upd(int x, int y, int k){
for(x++; x < maxn; x += x&-x){
for(int p = upper_bound(all(srt[x]), y) - begin(srt[x]); p <= sz(fen[x]); p += p&-p) fen[x][p-1] += k;
}
}
ll get(int x, int y){
ll res = 0;
for(x++; x; x -= x&-x){
for(int p = upper_bound(all(srt[x]), y) - begin(srt[x]); p; p -= p&-p) res += fen[x][p-1];
}
return res;
}
void addf(int x, int y){
for(x++; x < maxn; x += x&-x) srt[x].pb(y);
}
void upd(int x1, int x2, int y1, int y2, int k, bool f = true){
if(f){
upd(x1, y1, k), upd(x2 + 1, y2 + 1, k);
upd(x1, y2 + 1, -k), upd(x2 + 1, y1, -k);
} else{
addf(x1, y1), addf(x2 + 1, y2 + 1);
addf(x1, y2 + 1), addf(x2 + 1, y1);
}
}
set<int> em;
void add(int p, int t, bool f = true){
em.erase(p);
auto nxt = em.upper_bound(p), prv = prev(nxt);
upd(*prv + 1, p, p, *nxt - 1, -t, f);
}
void rem(int p, int t, bool f = true){
auto nxt = em.upper_bound(p), prv = prev(nxt);
upd(*prv + 1, p, p, *nxt - 1, t, f);
em.insert(p);
}
bool chk(int l, int r){
auto it = em.lower_bound(l);
return it == end(em) || *it > r;
}
int main(){ IOS();
int n, q; cin >> n >> q;
em.insert(-1), em.insert(n);
vector<bool> cr(n), bs;
rep(i,0,n){
char c; cin >> c;
if(c == '0') em.insert(i);
else cr[i] = true;
}
bs = cr;
vector<pp> query(q);
rep(i,0,q){
string t; cin >> t;
if(t[0] == 'q'){
int l, r; cin >> l >> r; l--, r -= 2;
addf(l, r);
query[i] = {l, r};
} else{
int l; cin >> l; l--;
query[i] = {l, -1};
if(cr[l]) rem(l, i + 1, false);
else add(l, i + 1, false);
cr[l] = !cr[l];
}
}
rep(i,0,maxn){
sort(all(srt[i])), srt[i].erase(unique(all(srt[i])), end(srt[i]));
fen[i].resize(sz(srt[i]));
}
cr = bs;
em.clear();
em.insert(-1), em.insert(n);
rep(i,0,n) if(!cr[i]) em.insert(i);
rep(i,0,q){
auto[l, r] = query[i];
if(r == -1){
if(cr[l]) rem(l, i + 1);
else add(l, i + 1);
cr[l] = !cr[l];
}else{
cout << get(l, r) + chk(l, r)*(i + 1) << '\n';
}
}
return 0-0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14436 KB |
Output is correct |
3 |
Correct |
9 ms |
14496 KB |
Output is correct |
4 |
Correct |
10 ms |
14548 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
849 ms |
113476 KB |
Output is correct |
2 |
Correct |
1002 ms |
119124 KB |
Output is correct |
3 |
Correct |
1232 ms |
115796 KB |
Output is correct |
4 |
Correct |
2105 ms |
131840 KB |
Output is correct |
5 |
Correct |
1740 ms |
124460 KB |
Output is correct |
6 |
Correct |
2225 ms |
137880 KB |
Output is correct |
7 |
Correct |
1026 ms |
88912 KB |
Output is correct |
8 |
Correct |
647 ms |
76596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
15060 KB |
Output is correct |
2 |
Correct |
13 ms |
14932 KB |
Output is correct |
3 |
Correct |
12 ms |
14832 KB |
Output is correct |
4 |
Correct |
10 ms |
14564 KB |
Output is correct |
5 |
Correct |
3122 ms |
177512 KB |
Output is correct |
6 |
Correct |
2545 ms |
152340 KB |
Output is correct |
7 |
Correct |
1953 ms |
126020 KB |
Output is correct |
8 |
Correct |
1041 ms |
83392 KB |
Output is correct |
9 |
Correct |
237 ms |
52520 KB |
Output is correct |
10 |
Correct |
264 ms |
57176 KB |
Output is correct |
11 |
Correct |
281 ms |
55996 KB |
Output is correct |
12 |
Correct |
1345 ms |
95936 KB |
Output is correct |
13 |
Correct |
1038 ms |
83356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14676 KB |
Output is correct |
2 |
Correct |
11 ms |
14764 KB |
Output is correct |
3 |
Correct |
13 ms |
14996 KB |
Output is correct |
4 |
Correct |
13 ms |
14968 KB |
Output is correct |
5 |
Correct |
1264 ms |
84056 KB |
Output is correct |
6 |
Correct |
1762 ms |
113948 KB |
Output is correct |
7 |
Correct |
2246 ms |
138516 KB |
Output is correct |
8 |
Correct |
3025 ms |
163520 KB |
Output is correct |
9 |
Correct |
1204 ms |
141788 KB |
Output is correct |
10 |
Correct |
1333 ms |
171856 KB |
Output is correct |
11 |
Correct |
1313 ms |
141640 KB |
Output is correct |
12 |
Correct |
1323 ms |
172360 KB |
Output is correct |
13 |
Correct |
1255 ms |
141844 KB |
Output is correct |
14 |
Correct |
1314 ms |
172164 KB |
Output is correct |
15 |
Correct |
1358 ms |
90168 KB |
Output is correct |
16 |
Correct |
984 ms |
77352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14436 KB |
Output is correct |
3 |
Correct |
9 ms |
14496 KB |
Output is correct |
4 |
Correct |
10 ms |
14548 KB |
Output is correct |
5 |
Correct |
8 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14416 KB |
Output is correct |
7 |
Correct |
8 ms |
14412 KB |
Output is correct |
8 |
Correct |
849 ms |
113476 KB |
Output is correct |
9 |
Correct |
1002 ms |
119124 KB |
Output is correct |
10 |
Correct |
1232 ms |
115796 KB |
Output is correct |
11 |
Correct |
2105 ms |
131840 KB |
Output is correct |
12 |
Correct |
1740 ms |
124460 KB |
Output is correct |
13 |
Correct |
2225 ms |
137880 KB |
Output is correct |
14 |
Correct |
1026 ms |
88912 KB |
Output is correct |
15 |
Correct |
647 ms |
76596 KB |
Output is correct |
16 |
Correct |
14 ms |
15060 KB |
Output is correct |
17 |
Correct |
13 ms |
14932 KB |
Output is correct |
18 |
Correct |
12 ms |
14832 KB |
Output is correct |
19 |
Correct |
10 ms |
14564 KB |
Output is correct |
20 |
Correct |
3122 ms |
177512 KB |
Output is correct |
21 |
Correct |
2545 ms |
152340 KB |
Output is correct |
22 |
Correct |
1953 ms |
126020 KB |
Output is correct |
23 |
Correct |
1041 ms |
83392 KB |
Output is correct |
24 |
Correct |
237 ms |
52520 KB |
Output is correct |
25 |
Correct |
264 ms |
57176 KB |
Output is correct |
26 |
Correct |
281 ms |
55996 KB |
Output is correct |
27 |
Correct |
1345 ms |
95936 KB |
Output is correct |
28 |
Correct |
1038 ms |
83356 KB |
Output is correct |
29 |
Correct |
10 ms |
14676 KB |
Output is correct |
30 |
Correct |
11 ms |
14764 KB |
Output is correct |
31 |
Correct |
13 ms |
14996 KB |
Output is correct |
32 |
Correct |
13 ms |
14968 KB |
Output is correct |
33 |
Correct |
1264 ms |
84056 KB |
Output is correct |
34 |
Correct |
1762 ms |
113948 KB |
Output is correct |
35 |
Correct |
2246 ms |
138516 KB |
Output is correct |
36 |
Correct |
3025 ms |
163520 KB |
Output is correct |
37 |
Correct |
1204 ms |
141788 KB |
Output is correct |
38 |
Correct |
1333 ms |
171856 KB |
Output is correct |
39 |
Correct |
1313 ms |
141640 KB |
Output is correct |
40 |
Correct |
1323 ms |
172360 KB |
Output is correct |
41 |
Correct |
1255 ms |
141844 KB |
Output is correct |
42 |
Correct |
1314 ms |
172164 KB |
Output is correct |
43 |
Correct |
1358 ms |
90168 KB |
Output is correct |
44 |
Correct |
984 ms |
77352 KB |
Output is correct |
45 |
Correct |
488 ms |
90140 KB |
Output is correct |
46 |
Correct |
563 ms |
85808 KB |
Output is correct |
47 |
Correct |
1380 ms |
91544 KB |
Output is correct |
48 |
Correct |
2250 ms |
136320 KB |
Output is correct |
49 |
Correct |
311 ms |
60760 KB |
Output is correct |
50 |
Correct |
349 ms |
60948 KB |
Output is correct |
51 |
Correct |
300 ms |
60960 KB |
Output is correct |
52 |
Correct |
316 ms |
60708 KB |
Output is correct |
53 |
Correct |
317 ms |
60776 KB |
Output is correct |
54 |
Correct |
328 ms |
60732 KB |
Output is correct |