Submission #721529

# Submission time Handle Problem Language Result Execution time Memory
721529 2023-04-11T03:49:31 Z alvingogo Street Lamps (APIO19_street_lamps) C++14
0 / 100
327 ms 524288 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;

struct BIT{
	int n;
	vector<int> bt;
	BIT(int x){
		n=x;
		bt.resize(n+1);
	}
	void update(int x,int y){
		x++;
		for(;x<=n;x+=(x&-x)){
			bt[x]+=y;
		}
	}
	int query(int x){
		x++;
		int ans=0;
		for(;x>0;x-=(x&-x)){
			ans+=bt[x];
		}
		return ans;
	}
};

struct BBIT{
	int n,m;
	vector<BIT> v;
	BBIT(int x,int y){
		n=x;
		m=y;
		v.resize(n+1,BIT(m));
	}	
	void update(int x,int y,int z){
		x++;
		for(;x<=n;x+=(x&-x)){
			v[x].update(y,z);
		}
	}
	int query(int a,int y){
		a++;
		int ans=0;
		for(;a>0;a-=(a&-a)){
			ans+=v[a].query(y);
		}
		return ans;
	}
};
signed main(){
    AquA;
	int n,q;
	cin >> n >> q;
	set<pair<int,int> > s;
	string x;
	cin >> x;
	BBIT bt(n+5,n+5);
	auto add=[&](int a,int b,int c,int d,int x){
		bt.update(a,c,x);
		bt.update(a,d+1,-x);
		bt.update(b+1,c,-x);
		bt.update(b+1,d+1,x);
	};
	int l=-1,r=0;
	for(int i=0;i<n;i++){
		if(x[i]=='0'){
			if(l==-1){
				continue;
			}
			s.insert({l,r});
			l=-1;
		}
		else{
			if(l==-1){
				l=i;
			}
			r=max(r,i);
		}
	}
	if(l!=-1){
		s.insert({l,r});
	}
	for(int i=1;i<=q;i++){
		string tf;
		cin >> tf;
		if(tf[0]=='q'){
			int a,b;
			cin >> a >> b;
			a--;
			b--;
			auto h=*prev(s.lower_bound(make_pair(a,1e9)));
			int flag=0;
			if(h.fs<=a && h.sc>=b-1){
				flag=i;
			}
			cout << bt.query(a,b)+flag << '\n';
		}
		else{
			int z;
			cin >> z;
			z--;
			if(x[z]=='1'){
				auto u=*prev(s.lower_bound(make_pair(z,1e9)));
				s.erase(u);
				add(u.fs,z,z+1,u.sc+1,i);
				if(u.fs<=z-1){
					s.insert({u.fs,z-1});
				}
				if(z+1<=u.sc){
					s.insert({z+1,u.sc});
				}
				x[z]='0';
			}
			else{
				int al=z,ar=z;
				auto p=s.lower_bound(make_pair(z,-1));
				if(p!=s.begin()){
					auto u=prev(p);
					if((*u).sc==z-1){
						al=(*u).fs;
						s.erase(u);
					}
				}
				if(p!=s.end() && (*p).fs==z+1){
					ar=(*p).sc;
					s.erase(p);
				}
				//cout << al << " " << z << " " << ar << endl;
				add(al,z,z+1,ar+1,-i);
				s.insert({al,ar});
				x[z]='1';
			}
		}
	}
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Runtime error 1 ms 596 KB Execution killed with signal 11
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 110 ms 1788 KB Output is correct
2 Correct 135 ms 2468 KB Output is correct
3 Correct 327 ms 198092 KB Output is correct
4 Runtime error 325 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8276 KB Output is correct
2 Correct 4 ms 8276 KB Output is correct
3 Correct 4 ms 8276 KB Output is correct
4 Correct 4 ms 8208 KB Output is correct
5 Runtime error 224 ms 524288 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8276 KB Output is correct
2 Correct 5 ms 8276 KB Output is correct
3 Correct 5 ms 8276 KB Output is correct
4 Correct 5 ms 8260 KB Output is correct
5 Runtime error 209 ms 524288 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Runtime error 1 ms 596 KB Execution killed with signal 11
7 Halted 0 ms 0 KB -