#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define sz size
const int mxN = 3e5+1;
int n;
bool vals[mxN];
vector<pii> ranges[mxN];
vector<int> indexs[mxN];
int cnt = 0;
int lazsum[4*(20*mxN)+1];
void sumupd(int tl, int tr, int v, int l = 0, int r = cnt-1, int idx = 0){
if (tl > tr) return;
if (l > tr || r < tl) return;
if (l >= tl && r <= tr){
lazsum[idx] += v;
return;
}
int m = (l+r)/2;
sumupd(tl,tr,v,l,m,2*idx+1);
sumupd(tl,tr,v,m+1,r,2*idx+2);
}
int sumquery(int t, int l = 0, int r = cnt-1, int idx = 0){
if (l > t || r < t){
return 0;
}
if (l == r) return lazsum[idx];
int m = (l+r)/2;
return sumquery(t,l,m,2*idx+1) + sumquery(t,m+1,r,2*idx+2) + lazsum[idx];
}
//containing ordered queries in a spec. range
vector<pii> all[4*mxN+1];
int sidx[4*mxN+1];
void build(int l = 0, int r = n-1, int idx = 0){
if (l == r){
sidx[idx] = cnt;
for (pii i : ranges[l]){
all[idx].pb(i);
indexs[i.s].pb(cnt);
cnt++;
}
return;
}
int m = (l+r)/2;
build(l,m,2*idx+1);
build(m+1,r,2*idx+2);
sidx[idx] = cnt;
int cidx = 0;
while (cidx < all[2*idx+2].sz() && all[2*idx+2][cidx].f <= r){
cidx++;
}
for (pii i : all[2*idx+1]){
if (i.f <= r) continue;
while (cidx < all[2*idx+2].sz() && all[2*idx+2][cidx].f < i.f){
all[idx].pb(all[2*idx+2][cidx]);
indexs[all[2*idx+2][cidx].s].pb(cnt);
cnt++;
cidx++;
}
all[idx].pb(i);
indexs[i.s].pb(cnt);
cnt++;
}
while (cidx < all[2*idx+2].sz()){
all[idx].pb(all[2*idx+2][cidx]);
indexs[all[2*idx+2][cidx].s].pb(cnt);
cnt++;
cidx++;
}
/* cout << l << "-" << r << " " << idx << " : ";
for (pii temp : all[idx]) cout << temp.f << ',' << temp.s << " ";
cout << '\n';*/
}
void qupd(int tl, int tr, int t, int v, int l = 0, int r = n-1, int idx = 0){
if (l > tr || r < tl) return;
if (l >= tl && r <= tr){
pii temp = {tr,mxN+1};
int fidx = lower_bound(all[idx].begin(),all[idx].end(),temp) - all[idx].begin();
temp = {t,mxN+1};
int lidx = lower_bound(all[idx].begin(),all[idx].end(),temp) - all[idx].begin();
sumupd(sidx[idx]+fidx,sidx[idx]+lidx-1,v);
return;
}
int m = (l+r)/2;
qupd(tl,tr,t,v,l,m,2*idx+1);
qupd(tl,tr,t,v,m+1,r,2*idx+2);
}
// all for fiding the bounds of a connected range
int laz[4*mxN+1][2];
void pdown(int idx, int w){
if (laz[idx][w] != -1){
laz[2*idx+1][w] = laz[idx][w];
laz[2*idx+2][w] = laz[idx][w];
}
laz[idx][w] = -1;
}
int query(int t, int w, int l = 0, int r = n-1, int idx = 0){
if (l > t || r < t) return -1;
if (l == r) return laz[idx][w];
pdown(idx,w);
int m = (l+r)/2;
int r1 = query(t,w,l,m,2*idx+1);
int r2 = query(t,w,m+1,r,2*idx+2);
if (r1 == -1) return r2;
return r1;
}
void bupd(int tl, int tr, int v, int w, int l = 0, int r = n-1, int idx = 0){
if (l > tr || r < tl) return;
if (l >= tl && r <= tr){
laz[idx][w] = v;
return;
}
pdown(idx,w);
int m = (l+r)/2;
bupd(tl,tr,v,w,l,m,2*idx+1);
bupd(tl,tr,v,w,m+1,r,2*idx+2);
}
void upd(int t, int v, int l = 0, int r = n-1, int idx = 0){
if (l > t || r < t) return;
if (l == r){
// cout << t << " - " << v << ' ';
if (vals[l]){
int lidx = query(l,0);
int ridx = query(l,1);
// cout << lidx << " " << ridx;
qupd(lidx,l,ridx,v);
vals[l] = false;
bupd(l+1,ridx,l+1,0);
bupd(lidx,l,l,1);
} else {
int lidx = query(l,0);
int ridx = query(l+1,1);
// cout << lidx << " " << ridx;
qupd(lidx,l,ridx,-1*v);
vals[l] = true;
bupd(l+1,ridx,lidx,0);
bupd(lidx,l,ridx,1);
}
// cout << '\n';
return;
}
int m = (l+r)/2;
upd(t,v,l,m,2*idx+1);
upd(t,v,m+1,r,2*idx+2);
}
int main()
{
memset(lazsum,0,sizeof(lazsum));
memset(sidx,0,sizeof(sidx));
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int q;
cin >> n >> q;
n++;
string s1;
cin >> s1;
for (int i =0; i < 4*mxN+1; i++) for (int j =0; j < 2; j++) laz[i][j] = -1;
for (int i =0; i < n; i++){
bupd(i,i,i,0);
bupd(i,i,i,1);
}
vector<pii> qu(q);
int curr = 0;
string s2;
for (int i =0; i <q; i++){
cin >> s2;
if (s2 == "toggle"){
cin >> qu[i].f;
qu[i].f -= 1;
qu[i].s = -1;
} else {
cin >> qu[i].f >> qu[i].s;
qu[i].f -= 1;
qu[i].s -= 1;
ranges[qu[i].f].pb({qu[i].s,curr});
curr++;
}
}
for (int i =0; i < n; i++){
sort(ranges[i].begin(),ranges[i].end());
}
build();
/* cout << '\n';
for (int i =0; i < curr; i++){
cout << i << ": ";
for (int j : indexs[i]) cout << j << " ";
cout << '\n';
}
cout << '\n';*/
for (int i =0; i < n-1; i++){
vals[i] = false;
if (s1[i] == '1'){
upd(i,0);
}
}
/* for (int i =0; i < n; i++){
cout << query(i,0) << "-" << query(i,1) << " ";
}
cout << '\n';
*/
curr = 0;
int res;
for (int i = 0; i < q; i++){
if (qu[i].s == -1){
upd(qu[i].f,i+1);
/* for (int j =0; j< n; j++){
cout << query(j,0) << "-" << query(j,1) << " ";
}
cout << '\n';
*/
} else {
res = 0;
for (int j : indexs[curr]){
res += sumquery(j);
}
// cout << qu[i].s << " " << qu[i].f << ' ' << query(qu[i].s,1) << "\n";
if (query(qu[i].s,0) <= qu[i].f) res += i+1;
cout << res << '\n';
curr++;
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'void build(int, int, int)':
street_lamps.cpp:59:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while (cidx < all[2*idx+2].sz() && all[2*idx+2][cidx].f <= r){
| ~~~~~^~~~~~~~~~~~~~~~~~~
street_lamps.cpp:64:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
64 | while (cidx < all[2*idx+2].sz() && all[2*idx+2][cidx].f < i.f){
| ~~~~~^~~~~~~~~~~~~~~~~~~
street_lamps.cpp:74:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | while (cidx < all[2*idx+2].sz()){
| ~~~~~^~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
150612 KB |
Output is correct |
2 |
Correct |
37 ms |
150608 KB |
Output is correct |
3 |
Correct |
39 ms |
150608 KB |
Output is correct |
4 |
Correct |
37 ms |
150616 KB |
Output is correct |
5 |
Correct |
38 ms |
150616 KB |
Output is correct |
6 |
Correct |
40 ms |
150668 KB |
Output is correct |
7 |
Correct |
37 ms |
150684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
163412 KB |
Output is correct |
2 |
Correct |
307 ms |
163584 KB |
Output is correct |
3 |
Correct |
395 ms |
163924 KB |
Output is correct |
4 |
Correct |
727 ms |
170432 KB |
Output is correct |
5 |
Correct |
742 ms |
173548 KB |
Output is correct |
6 |
Correct |
672 ms |
167396 KB |
Output is correct |
7 |
Correct |
589 ms |
183220 KB |
Output is correct |
8 |
Correct |
906 ms |
184296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
150616 KB |
Output is correct |
2 |
Correct |
39 ms |
150612 KB |
Output is correct |
3 |
Correct |
44 ms |
150788 KB |
Output is correct |
4 |
Correct |
46 ms |
150868 KB |
Output is correct |
5 |
Correct |
625 ms |
159484 KB |
Output is correct |
6 |
Correct |
1180 ms |
192956 KB |
Output is correct |
7 |
Correct |
2101 ms |
222948 KB |
Output is correct |
8 |
Correct |
2764 ms |
264352 KB |
Output is correct |
9 |
Correct |
492 ms |
181320 KB |
Output is correct |
10 |
Correct |
477 ms |
183740 KB |
Output is correct |
11 |
Correct |
541 ms |
185336 KB |
Output is correct |
12 |
Correct |
2269 ms |
263236 KB |
Output is correct |
13 |
Correct |
2581 ms |
264400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
151120 KB |
Output is correct |
2 |
Correct |
27 ms |
150832 KB |
Output is correct |
3 |
Correct |
27 ms |
150832 KB |
Output is correct |
4 |
Correct |
25 ms |
150616 KB |
Output is correct |
5 |
Correct |
2368 ms |
262000 KB |
Output is correct |
6 |
Correct |
1785 ms |
233944 KB |
Output is correct |
7 |
Correct |
1148 ms |
203004 KB |
Output is correct |
8 |
Correct |
643 ms |
159360 KB |
Output is correct |
9 |
Correct |
409 ms |
168164 KB |
Output is correct |
10 |
Correct |
248 ms |
156500 KB |
Output is correct |
11 |
Correct |
408 ms |
168016 KB |
Output is correct |
12 |
Correct |
251 ms |
156500 KB |
Output is correct |
13 |
Correct |
401 ms |
167964 KB |
Output is correct |
14 |
Correct |
252 ms |
156640 KB |
Output is correct |
15 |
Correct |
2262 ms |
262912 KB |
Output is correct |
16 |
Correct |
2690 ms |
264456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
150612 KB |
Output is correct |
2 |
Correct |
37 ms |
150608 KB |
Output is correct |
3 |
Correct |
39 ms |
150608 KB |
Output is correct |
4 |
Correct |
37 ms |
150616 KB |
Output is correct |
5 |
Correct |
38 ms |
150616 KB |
Output is correct |
6 |
Correct |
40 ms |
150668 KB |
Output is correct |
7 |
Correct |
37 ms |
150684 KB |
Output is correct |
8 |
Correct |
292 ms |
163412 KB |
Output is correct |
9 |
Correct |
307 ms |
163584 KB |
Output is correct |
10 |
Correct |
395 ms |
163924 KB |
Output is correct |
11 |
Correct |
727 ms |
170432 KB |
Output is correct |
12 |
Correct |
742 ms |
173548 KB |
Output is correct |
13 |
Correct |
672 ms |
167396 KB |
Output is correct |
14 |
Correct |
589 ms |
183220 KB |
Output is correct |
15 |
Correct |
906 ms |
184296 KB |
Output is correct |
16 |
Correct |
41 ms |
150616 KB |
Output is correct |
17 |
Correct |
39 ms |
150612 KB |
Output is correct |
18 |
Correct |
44 ms |
150788 KB |
Output is correct |
19 |
Correct |
46 ms |
150868 KB |
Output is correct |
20 |
Correct |
625 ms |
159484 KB |
Output is correct |
21 |
Correct |
1180 ms |
192956 KB |
Output is correct |
22 |
Correct |
2101 ms |
222948 KB |
Output is correct |
23 |
Correct |
2764 ms |
264352 KB |
Output is correct |
24 |
Correct |
492 ms |
181320 KB |
Output is correct |
25 |
Correct |
477 ms |
183740 KB |
Output is correct |
26 |
Correct |
541 ms |
185336 KB |
Output is correct |
27 |
Correct |
2269 ms |
263236 KB |
Output is correct |
28 |
Correct |
2581 ms |
264400 KB |
Output is correct |
29 |
Correct |
29 ms |
151120 KB |
Output is correct |
30 |
Correct |
27 ms |
150832 KB |
Output is correct |
31 |
Correct |
27 ms |
150832 KB |
Output is correct |
32 |
Correct |
25 ms |
150616 KB |
Output is correct |
33 |
Correct |
2368 ms |
262000 KB |
Output is correct |
34 |
Correct |
1785 ms |
233944 KB |
Output is correct |
35 |
Correct |
1148 ms |
203004 KB |
Output is correct |
36 |
Correct |
643 ms |
159360 KB |
Output is correct |
37 |
Correct |
409 ms |
168164 KB |
Output is correct |
38 |
Correct |
248 ms |
156500 KB |
Output is correct |
39 |
Correct |
408 ms |
168016 KB |
Output is correct |
40 |
Correct |
251 ms |
156500 KB |
Output is correct |
41 |
Correct |
401 ms |
167964 KB |
Output is correct |
42 |
Correct |
252 ms |
156640 KB |
Output is correct |
43 |
Correct |
2262 ms |
262912 KB |
Output is correct |
44 |
Correct |
2690 ms |
264456 KB |
Output is correct |
45 |
Correct |
184 ms |
161096 KB |
Output is correct |
46 |
Correct |
294 ms |
165232 KB |
Output is correct |
47 |
Correct |
772 ms |
186348 KB |
Output is correct |
48 |
Correct |
1484 ms |
213844 KB |
Output is correct |
49 |
Correct |
556 ms |
188212 KB |
Output is correct |
50 |
Correct |
542 ms |
187868 KB |
Output is correct |
51 |
Correct |
619 ms |
189392 KB |
Output is correct |
52 |
Correct |
648 ms |
203616 KB |
Output is correct |
53 |
Correct |
630 ms |
201452 KB |
Output is correct |
54 |
Correct |
627 ms |
201452 KB |
Output is correct |