답안 #216616

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
216616 2020-03-27T16:17:06 Z theStaticMind 가로등 (APIO19_street_lamps) C++14
100 / 100
3132 ms 200976 KB
#include<bits/stdc++.h>
#define pb push_back
#define ii pair<int,int>
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define INF 100000000000000000
#define modulo 1000000007
#define mod 998244353
//#define int long long int
using namespace std;
#define rand() (rand() * rand() + rand() + 1)

vector<int> ans(300005, 0);

struct Fenwick{
	
	vector<int> bit;
	vector<ii> arr;
	int size;
	
	void modify(int j, int v){
		j++;
		for(; j < size; j += j & -j)bit[j] += v;
	}
	
	int query(int j){
		j++;
		int v = 0;
		for(; j > 0; j -= j & -j)v += bit[j];
		return v;
	}
	
	int rangequery(int a, int b){
		return query(b) - query(a - 1);
	}
	
	void rangeupdate(int a, int b, int v){
		if(a > b) return;
		modify(a, v);
		modify(b + 1, -v);
	}

	int upper(ii x){
		return upper_bound(all(arr), x) - arr.begin() - 1;
	}
	int lower(ii x){
		return lower_bound(all(arr), x) - arr.begin();
	}

	Fenwick(){}
	
	void resize(int s){
		s += 3;
		size = s;
		bit = vector<int>(s, 0);
	}
	
};

const int S = 524288;
const int size = S * 2;
Fenwick seg[size];

void update(int x1, int x2, int y1, int y2, int v, int j = 1, int l = 0, int r = S - 1){
	if(r < x1 || x2 < l) return;
	if(x1 <= l && r <= x2){
		seg[j].rangeupdate(seg[j].lower({y1, -1}), seg[j].upper({y2, 1e9}), v);
	}
	else{
		update(x1,x2,y1,y2,v,j*2,l,(l+r)/2);
		update(x1,x2,y1,y2,v,j*2+1,(l+r)/2+1,r);
	}
}
void add(int x, int y, int id){
	int j = x + S;
	while(j != 0){
		seg[j].arr.pb({y, id});
		j /= 2;
	}
}

void find(int x, int y, int id){
	int j = x + S;

	while(j != 0){
		ans[id] += seg[j].query(seg[j].lower({y, id}));
		j /= 2;
	}
}

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	srand(time(NULL));

	int n, q;
	cin >> n >> q;
	string s; cin >> s;
	
	vector<bool> open(n, false);
	map<int, int> range;
	
	for(int i = 1; i <= n; i++){
		if(s[i - 1] == '1') open[i] = true;
	}
	vector<string>K(q+1);
	vector<int> L(q+1), R(q+1);

	for(int i = 1; i <= q; i++){
		cin >> K[i];
		if(K[i] == "toggle") cin >> L[i];
		else cin >> L[i] >> R[i], add(L[i], --R[i], i);

	}
	for(int i = 1; i < size; i++) sort(all(seg[i].arr)), seg[i].resize(sz(seg[i].arr));
	int last = 0;
	for(int i = 1; i <= n; i++){
		if(open[i]){
			if(!last) last = i;
		}
		else{
			if(last){
				range[last] = i - 1;
				last = 0;
			}
		}
	}
	if(last) range[last] = n;

	for(int i = 1; i <= q; i++){
		if(K[i] == "toggle"){
			int x = L[i];
			if(open[x]){
				auto itr = --range.upper_bound(x);
				int l = itr->first, r = itr->second;

				update(l, x, x, r, i);

				range.erase(itr);

				if(x != l){
					range[l] = x - 1;
				}
				if(x != r){
					range[x + 1] = r;
				}

			}
			else{
				auto itr = range.lower_bound(x);
				int l = x;
				int r = x;
				if(itr != range.end() && itr->first == x + 1){
					r = itr->second;
					range.erase(itr);
				}
				
				auto itl = range.upper_bound(x);

				if(itl != range.begin()){
					itl--;
					if(itl->second + 1 == x){
						l = itl->first;
						range.erase(itl);
					}
				}
				range[l] = r;
				update(l, x, x, r, -i);

			}
			open[x] = open[x] ^ 1;

		}
		else{
			auto itr = range.upper_bound(L[i]);
			int add = 0;
			if(itr != range.begin() && (--itr)->second >= R[i]) add = i;

			find(L[i], R[i], i);

			cout << ans[i] + add << '\n';
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 91640 KB Output is correct
2 Correct 85 ms 91872 KB Output is correct
3 Correct 86 ms 91768 KB Output is correct
4 Correct 85 ms 91896 KB Output is correct
5 Correct 86 ms 91896 KB Output is correct
6 Correct 96 ms 91768 KB Output is correct
7 Correct 85 ms 91896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1336 ms 141992 KB Output is correct
2 Correct 1471 ms 144272 KB Output is correct
3 Correct 1686 ms 146180 KB Output is correct
4 Correct 1912 ms 159096 KB Output is correct
5 Correct 2177 ms 170228 KB Output is correct
6 Correct 1505 ms 149440 KB Output is correct
7 Correct 3071 ms 198496 KB Output is correct
8 Correct 3062 ms 200976 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 86 ms 91896 KB Output is correct
2 Correct 106 ms 92152 KB Output is correct
3 Correct 91 ms 92140 KB Output is correct
4 Correct 94 ms 92184 KB Output is correct
5 Correct 544 ms 110092 KB Output is correct
6 Correct 1346 ms 136964 KB Output is correct
7 Correct 2068 ms 162680 KB Output is correct
8 Correct 2887 ms 197280 KB Output is correct
9 Correct 1534 ms 164656 KB Output is correct
10 Correct 1748 ms 171924 KB Output is correct
11 Correct 1699 ms 170848 KB Output is correct
12 Correct 2925 ms 195668 KB Output is correct
13 Correct 2899 ms 197236 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 93 ms 92152 KB Output is correct
2 Correct 90 ms 92152 KB Output is correct
3 Correct 94 ms 92024 KB Output is correct
4 Correct 87 ms 91896 KB Output is correct
5 Correct 3132 ms 194680 KB Output is correct
6 Correct 2300 ms 175592 KB Output is correct
7 Correct 1448 ms 149208 KB Output is correct
8 Correct 538 ms 113036 KB Output is correct
9 Correct 848 ms 127248 KB Output is correct
10 Correct 336 ms 107896 KB Output is correct
11 Correct 837 ms 127352 KB Output is correct
12 Correct 340 ms 107896 KB Output is correct
13 Correct 853 ms 127356 KB Output is correct
14 Correct 338 ms 107896 KB Output is correct
15 Correct 2871 ms 195676 KB Output is correct
16 Correct 2899 ms 197372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 91640 KB Output is correct
2 Correct 85 ms 91872 KB Output is correct
3 Correct 86 ms 91768 KB Output is correct
4 Correct 85 ms 91896 KB Output is correct
5 Correct 86 ms 91896 KB Output is correct
6 Correct 96 ms 91768 KB Output is correct
7 Correct 85 ms 91896 KB Output is correct
8 Correct 1336 ms 141992 KB Output is correct
9 Correct 1471 ms 144272 KB Output is correct
10 Correct 1686 ms 146180 KB Output is correct
11 Correct 1912 ms 159096 KB Output is correct
12 Correct 2177 ms 170228 KB Output is correct
13 Correct 1505 ms 149440 KB Output is correct
14 Correct 3071 ms 198496 KB Output is correct
15 Correct 3062 ms 200976 KB Output is correct
16 Correct 86 ms 91896 KB Output is correct
17 Correct 106 ms 92152 KB Output is correct
18 Correct 91 ms 92140 KB Output is correct
19 Correct 94 ms 92184 KB Output is correct
20 Correct 544 ms 110092 KB Output is correct
21 Correct 1346 ms 136964 KB Output is correct
22 Correct 2068 ms 162680 KB Output is correct
23 Correct 2887 ms 197280 KB Output is correct
24 Correct 1534 ms 164656 KB Output is correct
25 Correct 1748 ms 171924 KB Output is correct
26 Correct 1699 ms 170848 KB Output is correct
27 Correct 2925 ms 195668 KB Output is correct
28 Correct 2899 ms 197236 KB Output is correct
29 Correct 93 ms 92152 KB Output is correct
30 Correct 90 ms 92152 KB Output is correct
31 Correct 94 ms 92024 KB Output is correct
32 Correct 87 ms 91896 KB Output is correct
33 Correct 3132 ms 194680 KB Output is correct
34 Correct 2300 ms 175592 KB Output is correct
35 Correct 1448 ms 149208 KB Output is correct
36 Correct 538 ms 113036 KB Output is correct
37 Correct 848 ms 127248 KB Output is correct
38 Correct 336 ms 107896 KB Output is correct
39 Correct 837 ms 127352 KB Output is correct
40 Correct 340 ms 107896 KB Output is correct
41 Correct 853 ms 127356 KB Output is correct
42 Correct 338 ms 107896 KB Output is correct
43 Correct 2871 ms 195676 KB Output is correct
44 Correct 2899 ms 197372 KB Output is correct
45 Correct 743 ms 126380 KB Output is correct
46 Correct 919 ms 126568 KB Output is correct
47 Correct 1127 ms 132724 KB Output is correct
48 Correct 1837 ms 157464 KB Output is correct
49 Correct 1946 ms 181944 KB Output is correct
50 Correct 1983 ms 181192 KB Output is correct
51 Correct 1925 ms 181184 KB Output is correct
52 Correct 1992 ms 183800 KB Output is correct
53 Correct 2037 ms 183672 KB Output is correct
54 Correct 2020 ms 183828 KB Output is correct