#include <bits/stdc++.h>
#define all(v) (v).begin(), (v).end()
#define sortv(v) sort(all(v))
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define pb push_back
#define FI first
#define SE second
#define lb lower_bound
#define ub upper_bound
#define mp make_pair
#define test 0
#define TEST if(test)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const int MOD = 1000000007; // 998244353
const int INF = 2e9;
const ll INFLL = 1e18;
const int MAX_N = 300000;
int N, Q;
string str;
set<pii> st;
set<pii>::iterator it1, it2;
int t[MAX_N+1];
struct S{
int x, y, s, e;
bool operator <(const S &a)const{
return s<a.s;
}
};
vector<S> query;
struct ST{
int x, y, z;
bool operator <(const ST &a)const{
return z<a.z;
}
};
vector<ST> ask;
struct SEG2{
struct NODE{
int l, r;
int sum;
};
int SZ;
vector<NODE> seg;
void add(){
seg.pb({-1, -1, 0});
}
void Init(int x){
SZ = x;
add();
}
void Update(int x, int y, int z){
update(0, 1, SZ, x, y, z);
}
void update(int idx, int s, int e, int x, int y, int z){
if(x<=s && e<=y){
seg[idx].sum+=z;
return;
}
if(x>e || y<s) return;
if(seg[idx].l==-1){
seg[idx].l = seg.size(); add();
}
if(seg[idx].r==-1){
seg[idx].r = seg.size(); add();
}
update(seg[idx].l, s, (s+e)/2, x, y, z);
update(seg[idx].r, (s+e)/2+1, e, x, y, z);
}
int Calc(int x){
return calc(0, 1, SZ, x);
}
int calc(int idx, int s, int e, int k){
if(idx==-1) return 0;
if(s==e){
return seg[idx].sum;
}
if(k<=(s+e)/2){
return seg[idx].sum + calc(seg[idx].l, s, (s+e)/2, k);
}else{
return seg[idx].sum + calc(seg[idx].r, (s+e)/2+1, e, k);
}
}
} S1;
vector<SEG2> Seg2;
struct SEG1{
struct NODE{
int l, r, d;
};
int SZ;
vector<NODE> seg;
void add(){
seg.pb({-1, -1, -1});
}
void Init(int x){
add();
SZ = x;
}
void Update(int x, int y, int z){
update(0, 1, SZ, x, y, z);
}
void update(int idx, int s, int e, int x, int y, int z){
if(x<=s && e<=y){
if(seg[idx].d==-1){
seg[idx].d = Seg2.size();
Seg2.pb(S1);
Seg2[seg[idx].d].Init(SZ);
}
Seg2[seg[idx].d].Update(x, y, z);
return;
}
if(x>e || y<s) return;
if(seg[idx].l==-1){
seg[idx].l = seg.size(); add();
}
if(seg[idx].r==-1){
seg[idx].r = seg.size(); add();
}
update(seg[idx].l, s, (s+e)/2, x, y, z);
update(seg[idx].r, (s+e)/2+1, e, x, y, z);
}
int Calc(int x, int y){
return calc(0, 1, SZ, x, y);
}
int calc(int idx, int s, int e, int x, int y){
if(idx==-1) return 0;
int ret = 0;
if(seg[idx].d!=-1){
ret+=Seg2[seg[idx].d].Calc(y);
}
if(s==e){
return ret;
}
if(x<=(s+e)/2){
return ret + calc(seg[idx].l, s, (s+e)/2, x, y);
}else{
return ret + calc(seg[idx].r, (s+e)/2+1, e, x, y);
}
}
} Seg;
void update(int x){
TEST cout<<"U "<<query[x].x<<" "<<query[x].y<<" "<<query[x].s<<" "<<query[x].e<<endl;
Seg.Update(query[x].x, query[x].y, query[x].e-query[x].s);
}
int calc(int x, int y){
return Seg.Calc(x, y);
}
int main(){
scanf("%d%d", &N, &Q);
Seg.Init(N+1);
cin>>str;
for(int i=0; i<N; i++){
if(str[i]=='1'){
int s = i, e = i;
while(e<N-1 && str[e+1]=='1') e++;
i = e;
e++;
st.insert({e+1, s+1});
t[s+1] = 1;
}
}
for(int i=2; i<=Q+1; i++){
cin>>str;
int a, b;
if(str[0]=='t'){
scanf("%d", &a);
it1 = st.upper_bound({a, -1});
it2 = st.upper_bound({a+1, -1});
if(it2!=st.end() && (*it2).second<=a){
int l = (*it2).second, r = (*it2).first;
query.pb({l, r, t[l], i});
st.erase({r, l});
if(l!=a){
t[l] = i; st.insert({a, l});
}
if(r!=a+1){
t[a+1] = i;
st.insert({r, a+1});
}
}else{
int l = a, r = a+1;
if(it2!=st.end() && (*it2).second==a+1){
r = (*it2).first;
query.pb({a+1, r, t[a+1], i});
st.erase({r, a+1});
}
if(it1!=st.end() && (*it1).first==a){
l = (*it1).second;
query.pb({l, a, t[l], i});
st.erase({a, l});
}
t[l] = i;
st.insert({r, l});
}
}else{
scanf("%d%d", &a, &b);
ask.pb({a, b, i});
it1 = st.upper_bound({b, -1});
if(it1!=st.end() && (*it1).second<=a){
int l = (*it1).second, r = (*it1).first;
query.pb({l, r, t[l], i});
t[l] = i;
}
}
}
sort(query.begin(), query.end());
int idx = 0, idx2 = 0;
while(idx<query.size() && query[idx].s==1){
update(idx);
idx++;
}
for(int i=2; i<=Q+1; i++){
if(idx2<ask.size() && ask[idx2].z==i){
TEST cout<<"Q "<<ask[idx2].x<<" "<<ask[idx2].y<<endl;
printf("%d\n", calc(ask[idx2].x, ask[idx2].y));
idx2++;
}
while(idx<query.size() && query[idx].s<=i){
update(idx);
idx++;
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:223:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(idx<query.size() && query[idx].s==1){
~~~^~~~~~~~~~~~~
street_lamps.cpp:228:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(idx2<ask.size() && ask[idx2].z==i){
~~~~^~~~~~~~~~~
street_lamps.cpp:233:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(idx<query.size() && query[idx].s<=i){
~~~^~~~~~~~~~~~~
street_lamps.cpp:164:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &Q);
~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:181:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a);
~~~~~^~~~~~~~~~
street_lamps.cpp:211:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
419 ms |
9864 KB |
Output is correct |
2 |
Correct |
482 ms |
10596 KB |
Output is correct |
3 |
Correct |
667 ms |
18340 KB |
Output is correct |
4 |
Correct |
1496 ms |
190324 KB |
Output is correct |
5 |
Correct |
2355 ms |
251536 KB |
Output is correct |
6 |
Correct |
1580 ms |
212976 KB |
Output is correct |
7 |
Correct |
294 ms |
12244 KB |
Output is correct |
8 |
Correct |
316 ms |
17400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
1016 KB |
Output is correct |
2 |
Correct |
5 ms |
888 KB |
Output is correct |
3 |
Correct |
5 ms |
888 KB |
Output is correct |
4 |
Correct |
3 ms |
376 KB |
Output is correct |
5 |
Correct |
2189 ms |
312816 KB |
Output is correct |
6 |
Correct |
1953 ms |
299276 KB |
Output is correct |
7 |
Correct |
1668 ms |
248724 KB |
Output is correct |
8 |
Correct |
319 ms |
17396 KB |
Output is correct |
9 |
Correct |
251 ms |
10344 KB |
Output is correct |
10 |
Correct |
266 ms |
12900 KB |
Output is correct |
11 |
Correct |
286 ms |
13504 KB |
Output is correct |
12 |
Correct |
288 ms |
12276 KB |
Output is correct |
13 |
Correct |
321 ms |
17516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
4 ms |
760 KB |
Output is correct |
4 |
Correct |
5 ms |
760 KB |
Output is correct |
5 |
Correct |
537 ms |
20672 KB |
Output is correct |
6 |
Correct |
958 ms |
127792 KB |
Output is correct |
7 |
Correct |
1299 ms |
189324 KB |
Output is correct |
8 |
Correct |
1676 ms |
246864 KB |
Output is correct |
9 |
Correct |
441 ms |
9820 KB |
Output is correct |
10 |
Correct |
426 ms |
11484 KB |
Output is correct |
11 |
Correct |
446 ms |
9748 KB |
Output is correct |
12 |
Correct |
416 ms |
11636 KB |
Output is correct |
13 |
Correct |
443 ms |
9876 KB |
Output is correct |
14 |
Correct |
420 ms |
11676 KB |
Output is correct |
15 |
Correct |
276 ms |
12240 KB |
Output is correct |
16 |
Correct |
319 ms |
17344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
419 ms |
9864 KB |
Output is correct |
9 |
Correct |
482 ms |
10596 KB |
Output is correct |
10 |
Correct |
667 ms |
18340 KB |
Output is correct |
11 |
Correct |
1496 ms |
190324 KB |
Output is correct |
12 |
Correct |
2355 ms |
251536 KB |
Output is correct |
13 |
Correct |
1580 ms |
212976 KB |
Output is correct |
14 |
Correct |
294 ms |
12244 KB |
Output is correct |
15 |
Correct |
316 ms |
17400 KB |
Output is correct |
16 |
Correct |
5 ms |
1016 KB |
Output is correct |
17 |
Correct |
5 ms |
888 KB |
Output is correct |
18 |
Correct |
5 ms |
888 KB |
Output is correct |
19 |
Correct |
3 ms |
376 KB |
Output is correct |
20 |
Correct |
2189 ms |
312816 KB |
Output is correct |
21 |
Correct |
1953 ms |
299276 KB |
Output is correct |
22 |
Correct |
1668 ms |
248724 KB |
Output is correct |
23 |
Correct |
319 ms |
17396 KB |
Output is correct |
24 |
Correct |
251 ms |
10344 KB |
Output is correct |
25 |
Correct |
266 ms |
12900 KB |
Output is correct |
26 |
Correct |
286 ms |
13504 KB |
Output is correct |
27 |
Correct |
288 ms |
12276 KB |
Output is correct |
28 |
Correct |
321 ms |
17516 KB |
Output is correct |
29 |
Correct |
3 ms |
376 KB |
Output is correct |
30 |
Correct |
4 ms |
632 KB |
Output is correct |
31 |
Correct |
4 ms |
760 KB |
Output is correct |
32 |
Correct |
5 ms |
760 KB |
Output is correct |
33 |
Correct |
537 ms |
20672 KB |
Output is correct |
34 |
Correct |
958 ms |
127792 KB |
Output is correct |
35 |
Correct |
1299 ms |
189324 KB |
Output is correct |
36 |
Correct |
1676 ms |
246864 KB |
Output is correct |
37 |
Correct |
441 ms |
9820 KB |
Output is correct |
38 |
Correct |
426 ms |
11484 KB |
Output is correct |
39 |
Correct |
446 ms |
9748 KB |
Output is correct |
40 |
Correct |
416 ms |
11636 KB |
Output is correct |
41 |
Correct |
443 ms |
9876 KB |
Output is correct |
42 |
Correct |
420 ms |
11676 KB |
Output is correct |
43 |
Correct |
276 ms |
12240 KB |
Output is correct |
44 |
Correct |
319 ms |
17344 KB |
Output is correct |
45 |
Correct |
199 ms |
5784 KB |
Output is correct |
46 |
Correct |
252 ms |
5776 KB |
Output is correct |
47 |
Correct |
681 ms |
86348 KB |
Output is correct |
48 |
Correct |
1243 ms |
171096 KB |
Output is correct |
49 |
Correct |
283 ms |
9832 KB |
Output is correct |
50 |
Correct |
257 ms |
9856 KB |
Output is correct |
51 |
Correct |
262 ms |
9964 KB |
Output is correct |
52 |
Correct |
261 ms |
10336 KB |
Output is correct |
53 |
Correct |
264 ms |
10352 KB |
Output is correct |
54 |
Correct |
263 ms |
10284 KB |
Output is correct |