답안 #224792

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
224792 2020-04-18T20:02:04 Z AQT 가로등 (APIO19_street_lamps) C++14
100 / 100
753 ms 58188 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";
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 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
# 결과 실행 시간 메모리 Grader output
1 Correct 226 ms 31428 KB Output is correct
2 Correct 249 ms 35604 KB Output is correct
3 Correct 342 ms 36488 KB Output is correct
4 Correct 686 ms 49812 KB Output is correct
5 Correct 722 ms 43504 KB Output is correct
6 Correct 722 ms 48796 KB Output is correct
7 Correct 393 ms 41944 KB Output is correct
8 Correct 332 ms 27856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 512 KB Output is correct
2 Correct 7 ms 512 KB Output is correct
3 Correct 7 ms 512 KB Output is correct
4 Correct 6 ms 512 KB Output is correct
5 Correct 528 ms 42608 KB Output is correct
6 Correct 753 ms 44980 KB Output is correct
7 Correct 701 ms 41936 KB Output is correct
8 Correct 327 ms 25820 KB Output is correct
9 Correct 102 ms 18916 KB Output is correct
10 Correct 107 ms 21216 KB Output is correct
11 Correct 114 ms 21212 KB Output is correct
12 Correct 398 ms 39640 KB Output is correct
13 Correct 328 ms 26092 KB Output is correct
# 결과 실행 시간 메모리 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 6 ms 512 KB Output is correct
5 Correct 462 ms 46296 KB Output is correct
6 Correct 575 ms 46816 KB Output is correct
7 Correct 660 ms 51672 KB Output is correct
8 Correct 503 ms 58188 KB Output is correct
9 Correct 261 ms 38364 KB Output is correct
10 Correct 255 ms 43696 KB Output is correct
11 Correct 267 ms 38104 KB Output is correct
12 Correct 262 ms 44776 KB Output is correct
13 Correct 265 ms 38488 KB Output is correct
14 Correct 263 ms 46320 KB Output is correct
15 Correct 386 ms 45696 KB Output is correct
16 Correct 327 ms 31708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 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 226 ms 31428 KB Output is correct
9 Correct 249 ms 35604 KB Output is correct
10 Correct 342 ms 36488 KB Output is correct
11 Correct 686 ms 49812 KB Output is correct
12 Correct 722 ms 43504 KB Output is correct
13 Correct 722 ms 48796 KB Output is correct
14 Correct 393 ms 41944 KB Output is correct
15 Correct 332 ms 27856 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 7 ms 512 KB Output is correct
18 Correct 7 ms 512 KB Output is correct
19 Correct 6 ms 512 KB Output is correct
20 Correct 528 ms 42608 KB Output is correct
21 Correct 753 ms 44980 KB Output is correct
22 Correct 701 ms 41936 KB Output is correct
23 Correct 327 ms 25820 KB Output is correct
24 Correct 102 ms 18916 KB Output is correct
25 Correct 107 ms 21216 KB Output is correct
26 Correct 114 ms 21212 KB Output is correct
27 Correct 398 ms 39640 KB Output is correct
28 Correct 328 ms 26092 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 6 ms 512 KB Output is correct
33 Correct 462 ms 46296 KB Output is correct
34 Correct 575 ms 46816 KB Output is correct
35 Correct 660 ms 51672 KB Output is correct
36 Correct 503 ms 58188 KB Output is correct
37 Correct 261 ms 38364 KB Output is correct
38 Correct 255 ms 43696 KB Output is correct
39 Correct 267 ms 38104 KB Output is correct
40 Correct 262 ms 44776 KB Output is correct
41 Correct 265 ms 38488 KB Output is correct
42 Correct 263 ms 46320 KB Output is correct
43 Correct 386 ms 45696 KB Output is correct
44 Correct 327 ms 31708 KB Output is correct
45 Correct 106 ms 22428 KB Output is correct
46 Correct 149 ms 24092 KB Output is correct
47 Correct 357 ms 29280 KB Output is correct
48 Correct 645 ms 51416 KB Output is correct
49 Correct 127 ms 27100 KB Output is correct
50 Correct 116 ms 26968 KB Output is correct
51 Correct 118 ms 27352 KB Output is correct
52 Correct 137 ms 30556 KB Output is correct
53 Correct 136 ms 30552 KB Output is correct
54 Correct 141 ms 30684 KB Output is correct