Submission #349984

#TimeUsernameProblemLanguageResultExecution timeMemory
349984KerimStreet Lamps (APIO19_street_lamps)C++17
100 / 100
2915 ms53112 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...