Submission #721533

# Submission time Handle Problem Language Result Execution time Memory
721533 2023-04-11T03:51:35 Z alvingogo Street Lamps (APIO19_street_lamps) C++14
20 / 100
343 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--;
			int flag=0;
			if(s.lower_bound(make_pair(a,1e9))!=s.begin()){
				auto h=*prev(s.lower_bound(make_pair(a,1e9)));
				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 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 3412 KB Output is correct
2 Correct 149 ms 4028 KB Output is correct
3 Correct 343 ms 199800 KB Output is correct
4 Runtime error 188 ms 524288 KB Execution killed with signal 9
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 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 8276 KB Output is correct
5 Runtime error 211 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 4 ms 8276 KB Output is correct
5 Runtime error 192 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 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 123 ms 3412 KB Output is correct
9 Correct 149 ms 4028 KB Output is correct
10 Correct 343 ms 199800 KB Output is correct
11 Runtime error 188 ms 524288 KB Execution killed with signal 9
12 Halted 0 ms 0 KB -