#include<bits/stdc++.h>
#include<fstream>
//#pragma GCC optimize("Ofast,O3,unroll-loops")
//#pragma GCC target("avx2")
using namespace std;
ifstream fin("newbarn.in");
ofstream fout("newbarn.out");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const int mxn = 3e5 + 5, mod = 1e9 + 9, mxq = 3e5 + 5;
const ll inf = 1e17;
int n, q;
struct BIT2D{
vt<int>bit[mxn + 1];
vt<int>node[mxn + 1];
void fakeupd(int x, int y){
for(;x <= n + 1; x += x & (-x)){
node[x].pb(y);
}
}
void fakeget(int x, int y){
for(;x; x -= x & (-x)){
node[x].pb(y);
}
}
int get(int x, int y){
int ans = 0;
for(;x; x -= x & (-x)){
for(int idy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; idy; idy -= idy & (-idy)){
ans += bit[x][idy];
}
}
return(ans);
}
void upd(int x, int y, int v){
for(;x <= n + 1; x += x & (-x)){
for(int idy = lower_bound(node[x].begin(), node[x].end(), y) - node[x].begin() + 1; idy <= sz(node[x]); idy += idy & (-idy)){
bit[x][idy] += v;
}
}
}
void init(){
for(int i = 1; i <= n + 1; i++){
sort(node[i].begin(), node[i].end());
node[i].resize(unique(node[i].begin(), node[i].end()) - node[i].begin());
bit[i].resize(sz(node[i]) + 3);
}
}
void recupd(int i, int j, int x, int y, bool fake, int v = 0){
if(fake){
fakeupd(i, j); fakeupd(i, y + 1); fakeupd(x + 1, j); fakeupd(x + 1, y + 1);
}else{
upd(i, j, v); upd(i, y + 1, -v); upd(x + 1, j, -v); upd(x + 1, y + 1, v);
}
}
};
set<int>pos;
BIT2D cool;
pii queries[mxq + 1];
bool ok[mxn + 1];
int findlast(int x){
auto it = pos.lower_bound(x);
if(it == pos.begin())return(0);
return(*prev(it));
}
int findnext(int x){
auto it = pos.upper_bound(x);
if(it == pos.end())return(n + 1);
return(*it);
}
char c[mxn + 1];
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
for(int i = 1; i <= n; i++){
cin >> c[i];
ok[i] = (c[i] == '0');
if(ok[i]){
pos.insert(i);
}
}
for(int i = 0; i < q; i++){
string s; cin >> s;
if(s[0] == 't'){
int id; cin >> id;
if(!ok[id]){
int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
cool.recupd(topx, topy, botx, boty, 1);
pos.insert(id);
}else{
pos.erase(id);
int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
cool.recupd(topx, topy, botx, boty, 1);
}
ok[id] = !ok[id];
queries[i] = make_pair(id, -1);
}else{
int x, y; cin >> x >> y;
cool.fakeget(x, y);
queries[i] = make_pair(x, y);
}
}
cool.init();
pos.clear();
for(int i = 1; i <= n; i++){
ok[i] = (c[i] == '0');
if(ok[i])pos.insert(i);
}
for(int i = 0; i < q; i++){
if(queries[i].second == -1){
int id = queries[i].first;
if(!ok[id]){
int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
cool.recupd(topx, topy, botx, boty, 0, i + 1);
pos.insert(id);
}else{
pos.erase(id);
int topx = findlast(id) + 1, topy = id + 1, botx = id, boty = findnext(id);
cool.recupd(topx, topy, botx, boty, 0, -(i + 1));
}
ok[id] = !ok[id];
}else{
int x = queries[i].first, y = queries[i].second;
int ans = cool.get(x, y);
if(findnext(x) >= y && findlast(y) < x){
ans += (i + 1);
}
cout << ans << "\n";
}
}
return(0);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14368 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
248 ms |
33116 KB |
Output is correct |
2 |
Correct |
341 ms |
39940 KB |
Output is correct |
3 |
Correct |
656 ms |
48940 KB |
Output is correct |
4 |
Correct |
1856 ms |
91516 KB |
Output is correct |
5 |
Correct |
1558 ms |
87416 KB |
Output is correct |
6 |
Correct |
2003 ms |
94360 KB |
Output is correct |
7 |
Correct |
1052 ms |
71420 KB |
Output is correct |
8 |
Correct |
689 ms |
58644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
14692 KB |
Output is correct |
2 |
Correct |
8 ms |
14568 KB |
Output is correct |
3 |
Correct |
9 ms |
14548 KB |
Output is correct |
4 |
Correct |
7 ms |
14548 KB |
Output is correct |
5 |
Correct |
2763 ms |
116468 KB |
Output is correct |
6 |
Correct |
2146 ms |
101280 KB |
Output is correct |
7 |
Correct |
1562 ms |
85160 KB |
Output is correct |
8 |
Correct |
698 ms |
60300 KB |
Output is correct |
9 |
Correct |
108 ms |
23640 KB |
Output is correct |
10 |
Correct |
108 ms |
24160 KB |
Output is correct |
11 |
Correct |
111 ms |
24460 KB |
Output is correct |
12 |
Correct |
1026 ms |
72860 KB |
Output is correct |
13 |
Correct |
676 ms |
60192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14548 KB |
Output is correct |
2 |
Correct |
9 ms |
14552 KB |
Output is correct |
3 |
Correct |
9 ms |
14592 KB |
Output is correct |
4 |
Correct |
9 ms |
14560 KB |
Output is correct |
5 |
Correct |
936 ms |
66564 KB |
Output is correct |
6 |
Correct |
1398 ms |
81900 KB |
Output is correct |
7 |
Correct |
1924 ms |
94356 KB |
Output is correct |
8 |
Correct |
2604 ms |
107496 KB |
Output is correct |
9 |
Correct |
439 ms |
44060 KB |
Output is correct |
10 |
Correct |
345 ms |
40536 KB |
Output is correct |
11 |
Correct |
437 ms |
44548 KB |
Output is correct |
12 |
Correct |
342 ms |
41212 KB |
Output is correct |
13 |
Correct |
438 ms |
44484 KB |
Output is correct |
14 |
Correct |
349 ms |
40836 KB |
Output is correct |
15 |
Correct |
1020 ms |
72992 KB |
Output is correct |
16 |
Correct |
695 ms |
60276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
7 ms |
14368 KB |
Output is correct |
7 |
Correct |
7 ms |
14420 KB |
Output is correct |
8 |
Correct |
248 ms |
33116 KB |
Output is correct |
9 |
Correct |
341 ms |
39940 KB |
Output is correct |
10 |
Correct |
656 ms |
48940 KB |
Output is correct |
11 |
Correct |
1856 ms |
91516 KB |
Output is correct |
12 |
Correct |
1558 ms |
87416 KB |
Output is correct |
13 |
Correct |
2003 ms |
94360 KB |
Output is correct |
14 |
Correct |
1052 ms |
71420 KB |
Output is correct |
15 |
Correct |
689 ms |
58644 KB |
Output is correct |
16 |
Correct |
9 ms |
14692 KB |
Output is correct |
17 |
Correct |
8 ms |
14568 KB |
Output is correct |
18 |
Correct |
9 ms |
14548 KB |
Output is correct |
19 |
Correct |
7 ms |
14548 KB |
Output is correct |
20 |
Correct |
2763 ms |
116468 KB |
Output is correct |
21 |
Correct |
2146 ms |
101280 KB |
Output is correct |
22 |
Correct |
1562 ms |
85160 KB |
Output is correct |
23 |
Correct |
698 ms |
60300 KB |
Output is correct |
24 |
Correct |
108 ms |
23640 KB |
Output is correct |
25 |
Correct |
108 ms |
24160 KB |
Output is correct |
26 |
Correct |
111 ms |
24460 KB |
Output is correct |
27 |
Correct |
1026 ms |
72860 KB |
Output is correct |
28 |
Correct |
676 ms |
60192 KB |
Output is correct |
29 |
Correct |
8 ms |
14548 KB |
Output is correct |
30 |
Correct |
9 ms |
14552 KB |
Output is correct |
31 |
Correct |
9 ms |
14592 KB |
Output is correct |
32 |
Correct |
9 ms |
14560 KB |
Output is correct |
33 |
Correct |
936 ms |
66564 KB |
Output is correct |
34 |
Correct |
1398 ms |
81900 KB |
Output is correct |
35 |
Correct |
1924 ms |
94356 KB |
Output is correct |
36 |
Correct |
2604 ms |
107496 KB |
Output is correct |
37 |
Correct |
439 ms |
44060 KB |
Output is correct |
38 |
Correct |
345 ms |
40536 KB |
Output is correct |
39 |
Correct |
437 ms |
44548 KB |
Output is correct |
40 |
Correct |
342 ms |
41212 KB |
Output is correct |
41 |
Correct |
438 ms |
44484 KB |
Output is correct |
42 |
Correct |
349 ms |
40836 KB |
Output is correct |
43 |
Correct |
1020 ms |
72992 KB |
Output is correct |
44 |
Correct |
695 ms |
60276 KB |
Output is correct |
45 |
Correct |
96 ms |
23572 KB |
Output is correct |
46 |
Correct |
174 ms |
28036 KB |
Output is correct |
47 |
Correct |
881 ms |
54144 KB |
Output is correct |
48 |
Correct |
1841 ms |
90932 KB |
Output is correct |
49 |
Correct |
118 ms |
25160 KB |
Output is correct |
50 |
Correct |
116 ms |
24928 KB |
Output is correct |
51 |
Correct |
126 ms |
25560 KB |
Output is correct |
52 |
Correct |
155 ms |
27528 KB |
Output is correct |
53 |
Correct |
152 ms |
27340 KB |
Output is correct |
54 |
Correct |
151 ms |
27452 KB |
Output is correct |