Submission #225234

# Submission time Handle Problem Language Result Execution time Memory
225234 2020-04-19T18:32:22 Z Yaroslaff Street Lamps (APIO19_street_lamps) C++17
100 / 100
734 ms 58332 KB
// Problem : APIO '19 P3 - Street Lamps
// Contest : DMOJ
// URL : https://dmoj.ca/problem/apio19p3
// Memory Limit : 512 MB
// Time Limit : 2500 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)
 
#include <bits/stdc++.h>
 
using namespace std;
 
struct FenwickTree{
	int bit[300005];
	int lim;
	vector<int> vec;
	int query(int n){
		//cout << "q: " << n << endl;
		int ret = 0;
		while(n){
			ret += bit[n];
			n -= n&-n;
		}
		return ret;
	}
	void add(int n, int v){
		//cout << "a: " << n << endl;
		while(n <= lim){
			bit[n] += v;
			vec.push_back(n);
			n += n&-n;
		}
	}
	void clr(){
		//cout << "c: " << n << endl;
		for(int n : vec){
			bit[n] = 0;
		}
		vec.clear();
	}
};
 
int N, Q;
bool state[300005];
set<int> st;
multiset<int> rhthand;
vector<pair<pair<int, int>, int>> upd, qu;
int ans[300005];
FenwickTree bit;
 
void solve(int l, int r, vector<pair<pair<int, int>, int>> upd, vector<pair<pair<int, int>, int>> qu){
	if(l > r){
		return;
	}
	//cout << l << " " << r << endl;
	int mid = (l+r)>>1;
	vector<pair<pair<int, int>, int>> lftu, rhtu, lftq, rhtq;
	int idx = 0;
	int cnt = 0;
	for(auto q : qu){
		while(idx < (int) upd.size() && abs(upd[idx].second) > q.second){
			if(upd[idx].first.first <= mid){
				if(upd[idx].first.second >= mid){
					//cout << upd[idx].second << endl;
					bit.add(1, upd[idx].second);
					bit.add(upd[idx].first.second+1, -upd[idx].second);
					if(upd[idx].second > 0){
						cnt++;
						rhthand.insert(upd[idx].first.second);
					}
					else{
						cnt--;
						rhthand.erase(rhthand.lower_bound(upd[idx].first.second));
					}
				}
				lftu.push_back(upd[idx]);
			}
			else{
				rhtu.push_back(upd[idx]);
			}
			idx++;
		}
		assert(cnt <= 1);
		if(q.first.first >= mid){
			if(cnt && *rhthand.begin() >= q.first.second){
				ans[q.second] -= q.second;
			}
			ans[q.second] += bit.query(q.first.second);
			if(q.first.first > mid){
				rhtq.push_back(q);
			}
		}
		else{
			lftq.push_back(q);
		}
	}
	bit.clr();
	rhthand.clear();
	solve(l, mid-1, lftu, lftq);
	solve(mid+1, r, rhtu, rhtq);
}
 
int main(){
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> N >> Q;
	st.insert(0);
	st.insert(N+1);
	for(int i = 1; i<=N; i++){
		char c;
		cin >> c;
		state[i] = (c == '1');
		if(!state[i]){
			st.insert(i);
		}
	}
	for(int i = 1; i<=N; i++){
		if(state[i]){
			int idx = *st.lower_bound(i);
			idx--;
			upd.push_back({{i, idx}, Q+1});
			i = idx;
		}
	}
	for(int q = Q; q; q--){
		string s;
		cin >> s;
		if(s == "query"){
			int l, r;
			cin >> l >> r;
			r--;
			qu.push_back({{l, r}, q});
		}
		else{
			int i;
			cin >> i;
			state[i] ^= 1;
			const int coe = state[i] ? 1 : -1;
			int r = i, l = i;
			if(state[i]){
				st.erase(i);
			}
			if(state[i+1]){
				r = *st.lower_bound(i)-1;
				upd.push_back({{i+1, r}, -coe*q});
			}
			if(state[i-1]){
				l = *(--st.lower_bound(i))+1;
				upd.push_back({{l, i-1}, -coe*q});
			}
			upd.push_back({{l, r}, coe*q});			
			if(!state[i]){
				st.insert(i);
			}
			ans[q] = -1;
		}
	}
	bit.lim = N;
	solve(1, N, upd, qu);
	/*
	for(auto q : upd){
		cout << q.first.first << " " << q.first.second << " " << q.second << "\n";
	}
	*/
	for(int q = Q; q; q--){
		if(ans[q] != -1){
			cout << ans[q] << "\n";
		}
	}
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 225 ms 34584 KB Output is correct
2 Correct 257 ms 35596 KB Output is correct
3 Correct 352 ms 36336 KB Output is correct
4 Correct 668 ms 49624 KB Output is correct
5 Correct 734 ms 43616 KB Output is correct
6 Correct 670 ms 48728 KB Output is correct
7 Correct 386 ms 41944 KB Output is correct
8 Correct 338 ms 27556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 525 ms 46996 KB Output is correct
6 Correct 711 ms 49612 KB Output is correct
7 Correct 711 ms 47152 KB Output is correct
8 Correct 330 ms 31756 KB Output is correct
9 Correct 101 ms 21732 KB Output is correct
10 Correct 115 ms 24156 KB Output is correct
11 Correct 113 ms 24416 KB Output is correct
12 Correct 403 ms 45600 KB Output is correct
13 Correct 330 ms 32088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 6 ms 512 KB Output is correct
3 Correct 6 ms 512 KB Output is correct
4 Correct 5 ms 512 KB Output is correct
5 Correct 448 ms 46548 KB Output is correct
6 Correct 574 ms 46860 KB Output is correct
7 Correct 645 ms 51672 KB Output is correct
8 Correct 529 ms 58332 KB Output is correct
9 Correct 267 ms 38236 KB Output is correct
10 Correct 275 ms 43820 KB Output is correct
11 Correct 273 ms 38104 KB Output is correct
12 Correct 265 ms 44700 KB Output is correct
13 Correct 263 ms 38360 KB Output is correct
14 Correct 260 ms 46300 KB Output is correct
15 Correct 392 ms 45856 KB Output is correct
16 Correct 328 ms 31576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 4 ms 384 KB Output is correct
3 Correct 5 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 5 ms 384 KB Output is correct
8 Correct 225 ms 34584 KB Output is correct
9 Correct 257 ms 35596 KB Output is correct
10 Correct 352 ms 36336 KB Output is correct
11 Correct 668 ms 49624 KB Output is correct
12 Correct 734 ms 43616 KB Output is correct
13 Correct 670 ms 48728 KB Output is correct
14 Correct 386 ms 41944 KB Output is correct
15 Correct 338 ms 27556 KB Output is correct
16 Correct 5 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 5 ms 512 KB Output is correct
20 Correct 525 ms 46996 KB Output is correct
21 Correct 711 ms 49612 KB Output is correct
22 Correct 711 ms 47152 KB Output is correct
23 Correct 330 ms 31756 KB Output is correct
24 Correct 101 ms 21732 KB Output is correct
25 Correct 115 ms 24156 KB Output is correct
26 Correct 113 ms 24416 KB Output is correct
27 Correct 403 ms 45600 KB Output is correct
28 Correct 330 ms 32088 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 5 ms 512 KB Output is correct
33 Correct 448 ms 46548 KB Output is correct
34 Correct 574 ms 46860 KB Output is correct
35 Correct 645 ms 51672 KB Output is correct
36 Correct 529 ms 58332 KB Output is correct
37 Correct 267 ms 38236 KB Output is correct
38 Correct 275 ms 43820 KB Output is correct
39 Correct 273 ms 38104 KB Output is correct
40 Correct 265 ms 44700 KB Output is correct
41 Correct 263 ms 38360 KB Output is correct
42 Correct 260 ms 46300 KB Output is correct
43 Correct 392 ms 45856 KB Output is correct
44 Correct 328 ms 31576 KB Output is correct
45 Correct 109 ms 22428 KB Output is correct
46 Correct 148 ms 23972 KB Output is correct
47 Correct 344 ms 29276 KB Output is correct
48 Correct 643 ms 51292 KB Output is correct
49 Correct 128 ms 27056 KB Output is correct
50 Correct 126 ms 26968 KB Output is correct
51 Correct 135 ms 27352 KB Output is correct
52 Correct 137 ms 30556 KB Output is correct
53 Correct 138 ms 30556 KB Output is correct
54 Correct 147 ms 30684 KB Output is correct