답안 #524044

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
524044 2022-02-08T14:44:30 Z Koosha_mv 가로등 (APIO19_street_lamps) C++14
100 / 100
3188 ms 212388 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';
		}
	}
}
/*
2 3
00
t 1
t 2
q 1 3

2 2
00
t 1
q 1 3

5 2
11011
toggle 3
query 1 6
*/

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(){
      | ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 28492 KB Output is correct
2 Correct 15 ms 28480 KB Output is correct
3 Correct 18 ms 28480 KB Output is correct
4 Correct 18 ms 28492 KB Output is correct
5 Correct 15 ms 28548 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 184 ms 58440 KB Output is correct
2 Correct 255 ms 64692 KB Output is correct
3 Correct 470 ms 77640 KB Output is correct
4 Correct 1835 ms 144080 KB Output is correct
5 Correct 1955 ms 157832 KB Output is correct
6 Correct 1437 ms 127376 KB Output is correct
7 Correct 3071 ms 212388 KB Output is correct
8 Correct 3188 ms 199052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 28620 KB Output is correct
2 Correct 21 ms 28748 KB Output is correct
3 Correct 22 ms 28848 KB Output is correct
4 Correct 19 ms 28900 KB Output is correct
5 Correct 700 ms 90948 KB Output is correct
6 Correct 1474 ms 121640 KB Output is correct
7 Correct 2227 ms 152132 KB Output is correct
8 Correct 2573 ms 192132 KB Output is correct
9 Correct 209 ms 72436 KB Output is correct
10 Correct 225 ms 76968 KB Output is correct
11 Correct 270 ms 78868 KB Output is correct
12 Correct 2868 ms 204576 KB Output is correct
13 Correct 2497 ms 192316 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 28876 KB Output is correct
2 Correct 17 ms 28772 KB Output is correct
3 Correct 17 ms 28748 KB Output is correct
4 Correct 17 ms 28612 KB Output is correct
5 Correct 2776 ms 194440 KB Output is correct
6 Correct 2116 ms 165296 KB Output is correct
7 Correct 1375 ms 128840 KB Output is correct
8 Correct 475 ms 83544 KB Output is correct
9 Correct 244 ms 53068 KB Output is correct
10 Correct 146 ms 37084 KB Output is correct
11 Correct 246 ms 52988 KB Output is correct
12 Correct 142 ms 37104 KB Output is correct
13 Correct 242 ms 53036 KB Output is correct
14 Correct 132 ms 37064 KB Output is correct
15 Correct 3057 ms 204544 KB Output is correct
16 Correct 2664 ms 191876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 28492 KB Output is correct
2 Correct 15 ms 28480 KB Output is correct
3 Correct 18 ms 28480 KB Output is correct
4 Correct 18 ms 28492 KB Output is correct
5 Correct 15 ms 28548 KB Output is correct
6 Correct 14 ms 28500 KB Output is correct
7 Correct 14 ms 28492 KB Output is correct
8 Correct 184 ms 58440 KB Output is correct
9 Correct 255 ms 64692 KB Output is correct
10 Correct 470 ms 77640 KB Output is correct
11 Correct 1835 ms 144080 KB Output is correct
12 Correct 1955 ms 157832 KB Output is correct
13 Correct 1437 ms 127376 KB Output is correct
14 Correct 3071 ms 212388 KB Output is correct
15 Correct 3188 ms 199052 KB Output is correct
16 Correct 18 ms 28620 KB Output is correct
17 Correct 21 ms 28748 KB Output is correct
18 Correct 22 ms 28848 KB Output is correct
19 Correct 19 ms 28900 KB Output is correct
20 Correct 700 ms 90948 KB Output is correct
21 Correct 1474 ms 121640 KB Output is correct
22 Correct 2227 ms 152132 KB Output is correct
23 Correct 2573 ms 192132 KB Output is correct
24 Correct 209 ms 72436 KB Output is correct
25 Correct 225 ms 76968 KB Output is correct
26 Correct 270 ms 78868 KB Output is correct
27 Correct 2868 ms 204576 KB Output is correct
28 Correct 2497 ms 192316 KB Output is correct
29 Correct 17 ms 28876 KB Output is correct
30 Correct 17 ms 28772 KB Output is correct
31 Correct 17 ms 28748 KB Output is correct
32 Correct 17 ms 28612 KB Output is correct
33 Correct 2776 ms 194440 KB Output is correct
34 Correct 2116 ms 165296 KB Output is correct
35 Correct 1375 ms 128840 KB Output is correct
36 Correct 475 ms 83544 KB Output is correct
37 Correct 244 ms 53068 KB Output is correct
38 Correct 146 ms 37084 KB Output is correct
39 Correct 246 ms 52988 KB Output is correct
40 Correct 142 ms 37104 KB Output is correct
41 Correct 242 ms 53036 KB Output is correct
42 Correct 132 ms 37064 KB Output is correct
43 Correct 3057 ms 204544 KB Output is correct
44 Correct 2664 ms 191876 KB Output is correct
45 Correct 100 ms 44000 KB Output is correct
46 Correct 164 ms 49968 KB Output is correct
47 Correct 896 ms 85836 KB Output is correct
48 Correct 1821 ms 140516 KB Output is correct
49 Correct 284 ms 83172 KB Output is correct
50 Correct 284 ms 82820 KB Output is correct
51 Correct 355 ms 84364 KB Output is correct
52 Correct 350 ms 97712 KB Output is correct
53 Correct 345 ms 97652 KB Output is correct
54 Correct 400 ms 97692 KB Output is correct