Submission #932065

# Submission time Handle Problem Language Result Execution time Memory
932065 2024-02-22T21:35:30 Z amirhoseinfar1385 Street Lamps (APIO19_street_lamps) C++17
100 / 100
2813 ms 477940 KB
#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
int n,q,all[maxn],res[maxn],now,qu[maxn];
set<int>st;
int fn[maxn],kaf=(1<<19);

struct offt2fen{
	vector<pair<int,int>>ady[maxn];
	void upd(int l,int r,int w){
		l++;
		r++;
		while(l<maxn){
			if(l<0){
				return ;
			}
			ady[l].push_back(make_pair(-r,w));
			l+=((-l)&l);
		}
	}
	void pors(int l,int r,int w){
		l++;
		r++;
		while(l>0){
			ady[l].push_back(make_pair(r,w));
			l-=((-l)&l);
		}
	}
	void cal(int i){
		for(auto x:ady[i]){
			if(x.first<0){
				x.first=-x.first;
				while(x.first<maxn){
					fn[x.first]+=x.second;
					x.first+=((-x.first)&x.first);
				}
			}
			else{
				while(x.first>0){
					res[x.second]+=fn[x.first];
					x.first-=((-x.first)&x.first);
				}
			}
		}
		for(auto x:ady[i]){
			if(x.first<0){
				x.first=-x.first;
				while(x.first<maxn){
					fn[x.first]=0;
					x.first+=((-x.first)&x.first);
				}
			}
		}
	}
	void build(){
		for(int i=0;i<maxn;i++){
			cal(i);
		}
	}
}fen;

void calu(int l,int r,int w){
	if(l>r){
		return ;
	}
	fen.upd(l,l,w);
	fen.upd(l,r+1,-w);
	fen.upd(r+1,l,-w);
	fen.upd(r+1,r+1,w);
}

void upd(int ind){
	st.erase(ind);
	if(ind>=(*st.rbegin())||(ind<=(*st.begin()))){
		return ;
	}
	auto y=st.lower_bound(ind);
	int nx=*y;
	y--;
	int ps=*y;
	if(all[ind]){
		calu(ps+1,ind-1,-(maxn-now));
		calu(ind+1,nx-1,-(maxn-now));
		calu(ps+1,nx-1,maxn-now);
	}
	else{
		calu(ps+1,nx-1,-(maxn-now));
		calu(ps+1,ind-1,(maxn-now));
		calu(ind+1,nx-1,(maxn-now));
		st.insert(ind);
	}
	all[ind]^=1;
}

void pors(int l,int r){
	auto x=*st.lower_bound(l);
	if(x>r){
		res[now]-=maxn-now;
	}
	fen.pors(l,r,now);
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>q;
	st.insert(0);
	st.insert(n+1);
	calu(1,n,maxn);
	for(int i=1;i<=n;i++){
		char c;
		cin>>c;
		all[i]=c-'0';
		all[i]^=1;
		if(all[i]==1){
			all[i]=0;
			upd(i);
		}
	}
	for(int i=1;i<=q;i++){
		now=i;
		string s;
		int l,r;
		cin>>s>>l;
		if(s[0]=='q'){
			cin>>r;
			r--;
			pors(l,r);
			qu[now]=1;
		}
		else{
			upd(l);
		}
	}
	fen.build();
	for(int i=1;i<=q;i++){
		if(qu[i]==1){
			cout<<res[i]<<"\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16984 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 5 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1155 ms 186848 KB Output is correct
2 Correct 1104 ms 208776 KB Output is correct
3 Correct 895 ms 195728 KB Output is correct
4 Correct 1809 ms 296000 KB Output is correct
5 Correct 1655 ms 275780 KB Output is correct
6 Correct 1878 ms 359168 KB Output is correct
7 Correct 1837 ms 313428 KB Output is correct
8 Correct 239 ms 53044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 18780 KB Output is correct
2 Correct 14 ms 18780 KB Output is correct
3 Correct 12 ms 18320 KB Output is correct
4 Correct 5 ms 19032 KB Output is correct
5 Correct 2813 ms 477940 KB Output is correct
6 Correct 2389 ms 428044 KB Output is correct
7 Correct 1630 ms 273592 KB Output is correct
8 Correct 265 ms 49960 KB Output is correct
9 Correct 60 ms 26344 KB Output is correct
10 Correct 64 ms 26816 KB Output is correct
11 Correct 66 ms 27124 KB Output is correct
12 Correct 1788 ms 304936 KB Output is correct
13 Correct 231 ms 49804 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 17752 KB Output is correct
2 Correct 9 ms 18012 KB Output is correct
3 Correct 12 ms 18524 KB Output is correct
4 Correct 15 ms 18780 KB Output is correct
5 Correct 1187 ms 200332 KB Output is correct
6 Correct 1549 ms 263980 KB Output is correct
7 Correct 1875 ms 343964 KB Output is correct
8 Correct 2273 ms 421404 KB Output is correct
9 Correct 1482 ms 258772 KB Output is correct
10 Correct 2173 ms 337740 KB Output is correct
11 Correct 1501 ms 264680 KB Output is correct
12 Correct 2165 ms 347196 KB Output is correct
13 Correct 1522 ms 271712 KB Output is correct
14 Correct 2273 ms 347396 KB Output is correct
15 Correct 1850 ms 295556 KB Output is correct
16 Correct 276 ms 49968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 16984 KB Output is correct
2 Correct 4 ms 16988 KB Output is correct
3 Correct 5 ms 16988 KB Output is correct
4 Correct 5 ms 16988 KB Output is correct
5 Correct 5 ms 16988 KB Output is correct
6 Correct 4 ms 16988 KB Output is correct
7 Correct 4 ms 19036 KB Output is correct
8 Correct 1155 ms 186848 KB Output is correct
9 Correct 1104 ms 208776 KB Output is correct
10 Correct 895 ms 195728 KB Output is correct
11 Correct 1809 ms 296000 KB Output is correct
12 Correct 1655 ms 275780 KB Output is correct
13 Correct 1878 ms 359168 KB Output is correct
14 Correct 1837 ms 313428 KB Output is correct
15 Correct 239 ms 53044 KB Output is correct
16 Correct 15 ms 18780 KB Output is correct
17 Correct 14 ms 18780 KB Output is correct
18 Correct 12 ms 18320 KB Output is correct
19 Correct 5 ms 19032 KB Output is correct
20 Correct 2813 ms 477940 KB Output is correct
21 Correct 2389 ms 428044 KB Output is correct
22 Correct 1630 ms 273592 KB Output is correct
23 Correct 265 ms 49960 KB Output is correct
24 Correct 60 ms 26344 KB Output is correct
25 Correct 64 ms 26816 KB Output is correct
26 Correct 66 ms 27124 KB Output is correct
27 Correct 1788 ms 304936 KB Output is correct
28 Correct 231 ms 49804 KB Output is correct
29 Correct 8 ms 17752 KB Output is correct
30 Correct 9 ms 18012 KB Output is correct
31 Correct 12 ms 18524 KB Output is correct
32 Correct 15 ms 18780 KB Output is correct
33 Correct 1187 ms 200332 KB Output is correct
34 Correct 1549 ms 263980 KB Output is correct
35 Correct 1875 ms 343964 KB Output is correct
36 Correct 2273 ms 421404 KB Output is correct
37 Correct 1482 ms 258772 KB Output is correct
38 Correct 2173 ms 337740 KB Output is correct
39 Correct 1501 ms 264680 KB Output is correct
40 Correct 2165 ms 347196 KB Output is correct
41 Correct 1522 ms 271712 KB Output is correct
42 Correct 2273 ms 347396 KB Output is correct
43 Correct 1850 ms 295556 KB Output is correct
44 Correct 276 ms 49968 KB Output is correct
45 Correct 1010 ms 136400 KB Output is correct
46 Correct 793 ms 136172 KB Output is correct
47 Correct 894 ms 169928 KB Output is correct
48 Correct 1823 ms 303024 KB Output is correct
49 Correct 76 ms 27452 KB Output is correct
50 Correct 85 ms 27372 KB Output is correct
51 Correct 74 ms 28164 KB Output is correct
52 Correct 86 ms 30520 KB Output is correct
53 Correct 95 ms 30752 KB Output is correct
54 Correct 105 ms 30612 KB Output is correct