Submission #524046

# Submission time Handle Problem Language Result Execution time Memory
524046 2022-02-08T14:45:18 Z Koosha_mv Street Lamps (APIO19_street_lamps) C++14
100 / 100
3257 ms 206992 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<(x.F)<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
#define int ll

const int N=6e5+99;

int n,q,a[N],b[N],ans[N],type[N];
string t;
set<int> s;
vector<int> seg[N],fenwik[N];

void addvec(int x,int y){
	x+=n;
	while(x){
		seg[x].pb(y);
		fenwik[x].pb(0);
		x/=2;
	}
}
void build(){
	f(i,1,2*n){
		sort(seg[i].begin(),seg[i].end());
		seg[i].resize(unique(seg[i].begin(),seg[i].end())-seg[i].begin());
		while(fenwik[i].size()>seg[i].size()) fenwik[i].pop_back();
	}
}
void add(int id,int x,int val){
	x=lower_bound(all(seg[id]),x)-seg[id].begin();
	for(;x<fenwik[id].size();x+=(x&-x)){
		fenwik[id][x]+=val;
	}
}
void Add(int l,int r,int L,int R,int val){
	for(l+=n,r+=n;l<r;l>>=1,r>>=1){
		if(l&1){
			add(l,L,val);
			add(l,R,-val);
			l++;
		}
		if(r&1){
			r--;
			add(r,L,val);
			add(r,R,-val);
		}
	}
}
int get(int x,int y){
	int res=0;
	y=lower_bound(all(seg[x]),y)-seg[x].begin();
	for(;y>0;y-=y&-y){
		res+=fenwik[x][y];
	}
	return res;
}
int Get(int x,int y){
	int res=0;
	x+=n;
	while(x){
		res+=get(x,y);
		x/=2;
	}
	return res;
}
main(){
	ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
	cin>>n>>q>>t;
	s.insert(-1);
	s.insert(n);
	f(i,0,n) if(t[i]=='0') s.insert(i);
	f(i,1,2*n) seg[i].pb(-1),fenwik[i].pb(0);
	f(i,1,q+1){
		string s;
		cin>>s;
		if(s[0]=='t'){
			type[i]=1;
			cin>>a[i]; a[i]--;
		}
		else{
			type[i]=2;
			cin>>a[i]>>b[i]; a[i]--,b[i]-=2;
			addvec(a[i],b[i]);
		}
	}
	build();
	f(i,1,q+1){
		if(type[i]==1){
			int l=(*prev(s.lower_bound(a[i])))+1,r=(*s.upper_bound(a[i]))-1;
			if(s.find(a[i])!=s.end()){
				s.erase(a[i]);
				Add(l,a[i]+1,a[i],r+1,-i);
			}
			else{
				s.insert(a[i]);
				Add(l,a[i]+1,a[i],r+1,i);
			}
		}
		else{
			int res=Get(a[i],b[i]);
			if((*s.lower_bound(a[i]))>b[i]){
				res+=i;
			}
			cout<<res<<'\n';
		}
	}
}

Compilation message

street_lamps.cpp: In function 'void add(long long int, long long int, long long int)':
street_lamps.cpp:47:8: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |  for(;x<fenwik[id].size();x+=(x&-x)){
      |       ~^~~~~~~~~~~~~~~~~~
street_lamps.cpp: At global scope:
street_lamps.cpp:82:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   82 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28492 KB Output is correct
2 Correct 15 ms 28492 KB Output is correct
3 Correct 13 ms 28508 KB Output is correct
4 Correct 13 ms 28536 KB Output is correct
5 Correct 15 ms 28472 KB Output is correct
6 Correct 13 ms 28492 KB Output is correct
7 Correct 14 ms 28492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 180 ms 61596 KB Output is correct
2 Correct 235 ms 62380 KB Output is correct
3 Correct 512 ms 74892 KB Output is correct
4 Correct 1872 ms 140252 KB Output is correct
5 Correct 1967 ms 153708 KB Output is correct
6 Correct 1533 ms 123460 KB Output is correct
7 Correct 3257 ms 206992 KB Output is correct
8 Correct 2724 ms 193336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28620 KB Output is correct
2 Correct 20 ms 28748 KB Output is correct
3 Correct 18 ms 28748 KB Output is correct
4 Correct 19 ms 28876 KB Output is correct
5 Correct 613 ms 88240 KB Output is correct
6 Correct 1247 ms 116968 KB Output is correct
7 Correct 1995 ms 146800 KB Output is correct
8 Correct 2531 ms 186528 KB Output is correct
9 Correct 204 ms 69804 KB Output is correct
10 Correct 245 ms 74052 KB Output is correct
11 Correct 230 ms 75864 KB Output is correct
12 Correct 3007 ms 198720 KB Output is correct
13 Correct 2698 ms 186340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 28936 KB Output is correct
2 Correct 19 ms 28792 KB Output is correct
3 Correct 20 ms 28764 KB Output is correct
4 Correct 19 ms 28692 KB Output is correct
5 Correct 3066 ms 191396 KB Output is correct
6 Correct 2226 ms 160068 KB Output is correct
7 Correct 1426 ms 124056 KB Output is correct
8 Correct 521 ms 79440 KB Output is correct
9 Correct 239 ms 50212 KB Output is correct
10 Correct 129 ms 34496 KB Output is correct
11 Correct 256 ms 49980 KB Output is correct
12 Correct 133 ms 34464 KB Output is correct
13 Correct 236 ms 50064 KB Output is correct
14 Correct 129 ms 34484 KB Output is correct
15 Correct 2857 ms 199004 KB Output is correct
16 Correct 2496 ms 186404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28492 KB Output is correct
2 Correct 15 ms 28492 KB Output is correct
3 Correct 13 ms 28508 KB Output is correct
4 Correct 13 ms 28536 KB Output is correct
5 Correct 15 ms 28472 KB Output is correct
6 Correct 13 ms 28492 KB Output is correct
7 Correct 14 ms 28492 KB Output is correct
8 Correct 180 ms 61596 KB Output is correct
9 Correct 235 ms 62380 KB Output is correct
10 Correct 512 ms 74892 KB Output is correct
11 Correct 1872 ms 140252 KB Output is correct
12 Correct 1967 ms 153708 KB Output is correct
13 Correct 1533 ms 123460 KB Output is correct
14 Correct 3257 ms 206992 KB Output is correct
15 Correct 2724 ms 193336 KB Output is correct
16 Correct 16 ms 28620 KB Output is correct
17 Correct 20 ms 28748 KB Output is correct
18 Correct 18 ms 28748 KB Output is correct
19 Correct 19 ms 28876 KB Output is correct
20 Correct 613 ms 88240 KB Output is correct
21 Correct 1247 ms 116968 KB Output is correct
22 Correct 1995 ms 146800 KB Output is correct
23 Correct 2531 ms 186528 KB Output is correct
24 Correct 204 ms 69804 KB Output is correct
25 Correct 245 ms 74052 KB Output is correct
26 Correct 230 ms 75864 KB Output is correct
27 Correct 3007 ms 198720 KB Output is correct
28 Correct 2698 ms 186340 KB Output is correct
29 Correct 17 ms 28936 KB Output is correct
30 Correct 19 ms 28792 KB Output is correct
31 Correct 20 ms 28764 KB Output is correct
32 Correct 19 ms 28692 KB Output is correct
33 Correct 3066 ms 191396 KB Output is correct
34 Correct 2226 ms 160068 KB Output is correct
35 Correct 1426 ms 124056 KB Output is correct
36 Correct 521 ms 79440 KB Output is correct
37 Correct 239 ms 50212 KB Output is correct
38 Correct 129 ms 34496 KB Output is correct
39 Correct 256 ms 49980 KB Output is correct
40 Correct 133 ms 34464 KB Output is correct
41 Correct 236 ms 50064 KB Output is correct
42 Correct 129 ms 34484 KB Output is correct
43 Correct 2857 ms 199004 KB Output is correct
44 Correct 2496 ms 186404 KB Output is correct
45 Correct 95 ms 42508 KB Output is correct
46 Correct 163 ms 48308 KB Output is correct
47 Correct 855 ms 83132 KB Output is correct
48 Correct 1643 ms 135988 KB Output is correct
49 Correct 245 ms 79976 KB Output is correct
50 Correct 240 ms 79756 KB Output is correct
51 Correct 266 ms 81160 KB Output is correct
52 Correct 334 ms 94404 KB Output is correct
53 Correct 332 ms 94028 KB Output is correct
54 Correct 343 ms 93960 KB Output is correct