Submission #349984

# Submission time Handle Problem Language Result Execution time Memory
349984 2021-01-18T19:27:54 Z Kerim Street Lamps (APIO19_street_lamps) C++17
100 / 100
2915 ms 53112 KB
#include "bits/stdc++.h"
#define MAXN 300009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
vector<pair<PII,int> >sg[MAXN<<2];
void upd(int l,int r,int l1,int r1,int v,int nd,int x,int y){
	if(l>y or x>r)
		return;
	if(l<=x and y<=r){
		sg[nd].pb(mp(mp(l1,r1),v));
		return;	
	}
	int mid=(x+y)>>1;
	upd(l,r,l1,r1,v,nd<<1,x,mid);
	upd(l,r,l1,r1,v,nd<<1|1,mid+1,y);
}
int get(int p,int v,int nd,int x,int y){
	int res=0;
	tr(it,sg[nd])
		if(v>=it->ff.ff and v<=it->ff.ss)
			res+=it->ss;
	if(x==y)return res;
	int mid=(x+y)>>1;
	if(p<=mid)return get(p,v,nd<<1,x,mid)+res;
	return get(p,v,nd<<1|1,mid+1,y)+res;	
}
char s[MAXN],t[7];
set<int>st;
PII query[MAXN];
int main(){
    //freopen("file.in", "r", stdin);
    int n,q;
    scanf("%d%d",&n,&q);
    scanf("%s",s+1);s[0]=s[n+1]='0';
    for(int i=0;i<=n+1;i++)
    	if(s[i]=='0')
    		st.insert(i);
	for(int i=1;i<=q;i++){
		scanf("%s",t);
		if(t[0]=='q'){
			int l,r;
			scanf("%d%d",&l,&r);r--;
			query[i]=mp(l,r);
			//adj[l].pb(r);
		}
		else{
			int x;
			scanf("%d",&x);
			query[i].ff=x;
		}
	}
	for(int i=1;i<=q;i++){
		if(query[i].ss){
			int l=query[i].ff,r=query[i].ss;
			int ans=get(l,r,1,1,n);
			__typeof((st).begin())it=st.lower_bound(l);
			if(*it>r)ans+=i;
			printf("%d\n",ans);
			continue;
		}
		int x=query[i].ff,a,b;
		__typeof((st).begin())it=st.lower_bound(x);
		if(s[x]=='0'){
			it--;a=*it;it++;it++;b=*it;
			upd(a+1,x,x,b-1,-i,1,1,n);
			s[x]='1';st.erase(x);
		}
		else{
			b=*it;it--;a=*it;
			upd(a+1,x,x,b-1,i,1,1,n);
			s[x]='0';st.insert(x);
		}
	}
	return 0;
}

Compilation message

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:50:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
   50 |     for(int i=0;i<=n+1;i++)
      |     ^~~
street_lamps.cpp:53:2: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
   53 |  for(int i=1;i<=q;i++){
      |  ^~~
street_lamps.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   48 |     scanf("%d%d",&n,&q);
      |     ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   49 |     scanf("%s",s+1);s[0]=s[n+1]='0';
      |     ~~~~~^~~~~~~~~~
street_lamps.cpp:54:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   54 |   scanf("%s",t);
      |   ~~~~~^~~~~~~~
street_lamps.cpp:57:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   57 |    scanf("%d%d",&l,&r);r--;
      |    ~~~~~^~~~~~~~~~~~~~
street_lamps.cpp:63:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   63 |    scanf("%d",&x);
      |    ~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28524 KB Output is correct
2 Correct 20 ms 28524 KB Output is correct
3 Correct 20 ms 28524 KB Output is correct
4 Correct 20 ms 28524 KB Output is correct
5 Correct 19 ms 28524 KB Output is correct
6 Correct 19 ms 28524 KB Output is correct
7 Correct 19 ms 28524 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 755 ms 38764 KB Output is correct
2 Correct 412 ms 39404 KB Output is correct
3 Correct 292 ms 40556 KB Output is correct
4 Correct 579 ms 49260 KB Output is correct
5 Correct 511 ms 46956 KB Output is correct
6 Correct 572 ms 50156 KB Output is correct
7 Correct 498 ms 51948 KB Output is correct
8 Correct 224 ms 39148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 28524 KB Output is correct
2 Correct 21 ms 28524 KB Output is correct
3 Correct 20 ms 28524 KB Output is correct
4 Correct 20 ms 28524 KB Output is correct
5 Correct 812 ms 53112 KB Output is correct
6 Correct 668 ms 50028 KB Output is correct
7 Correct 510 ms 46424 KB Output is correct
8 Correct 221 ms 39148 KB Output is correct
9 Correct 119 ms 34028 KB Output is correct
10 Correct 134 ms 34540 KB Output is correct
11 Correct 125 ms 34796 KB Output is correct
12 Correct 505 ms 51820 KB Output is correct
13 Correct 224 ms 39340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 28524 KB Output is correct
2 Correct 20 ms 28524 KB Output is correct
3 Correct 20 ms 28524 KB Output is correct
4 Correct 21 ms 28524 KB Output is correct
5 Correct 390 ms 44944 KB Output is correct
6 Correct 478 ms 47468 KB Output is correct
7 Correct 544 ms 49644 KB Output is correct
8 Correct 634 ms 51968 KB Output is correct
9 Correct 310 ms 40556 KB Output is correct
10 Correct 213 ms 40940 KB Output is correct
11 Correct 302 ms 40556 KB Output is correct
12 Correct 209 ms 41068 KB Output is correct
13 Correct 310 ms 40812 KB Output is correct
14 Correct 215 ms 41196 KB Output is correct
15 Correct 483 ms 51820 KB Output is correct
16 Correct 227 ms 39276 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 28524 KB Output is correct
2 Correct 20 ms 28524 KB Output is correct
3 Correct 20 ms 28524 KB Output is correct
4 Correct 20 ms 28524 KB Output is correct
5 Correct 19 ms 28524 KB Output is correct
6 Correct 19 ms 28524 KB Output is correct
7 Correct 19 ms 28524 KB Output is correct
8 Correct 755 ms 38764 KB Output is correct
9 Correct 412 ms 39404 KB Output is correct
10 Correct 292 ms 40556 KB Output is correct
11 Correct 579 ms 49260 KB Output is correct
12 Correct 511 ms 46956 KB Output is correct
13 Correct 572 ms 50156 KB Output is correct
14 Correct 498 ms 51948 KB Output is correct
15 Correct 224 ms 39148 KB Output is correct
16 Correct 20 ms 28524 KB Output is correct
17 Correct 21 ms 28524 KB Output is correct
18 Correct 20 ms 28524 KB Output is correct
19 Correct 20 ms 28524 KB Output is correct
20 Correct 812 ms 53112 KB Output is correct
21 Correct 668 ms 50028 KB Output is correct
22 Correct 510 ms 46424 KB Output is correct
23 Correct 221 ms 39148 KB Output is correct
24 Correct 119 ms 34028 KB Output is correct
25 Correct 134 ms 34540 KB Output is correct
26 Correct 125 ms 34796 KB Output is correct
27 Correct 505 ms 51820 KB Output is correct
28 Correct 224 ms 39340 KB Output is correct
29 Correct 20 ms 28524 KB Output is correct
30 Correct 20 ms 28524 KB Output is correct
31 Correct 20 ms 28524 KB Output is correct
32 Correct 21 ms 28524 KB Output is correct
33 Correct 390 ms 44944 KB Output is correct
34 Correct 478 ms 47468 KB Output is correct
35 Correct 544 ms 49644 KB Output is correct
36 Correct 634 ms 51968 KB Output is correct
37 Correct 310 ms 40556 KB Output is correct
38 Correct 213 ms 40940 KB Output is correct
39 Correct 302 ms 40556 KB Output is correct
40 Correct 209 ms 41068 KB Output is correct
41 Correct 310 ms 40812 KB Output is correct
42 Correct 215 ms 41196 KB Output is correct
43 Correct 483 ms 51820 KB Output is correct
44 Correct 227 ms 39276 KB Output is correct
45 Correct 2915 ms 35092 KB Output is correct
46 Correct 264 ms 34796 KB Output is correct
47 Correct 266 ms 39148 KB Output is correct
48 Correct 524 ms 48876 KB Output is correct
49 Correct 138 ms 34992 KB Output is correct
50 Correct 134 ms 35052 KB Output is correct
51 Correct 134 ms 35180 KB Output is correct
52 Correct 142 ms 35564 KB Output is correct
53 Correct 138 ms 35436 KB Output is correct
54 Correct 141 ms 35436 KB Output is correct