답안 #158047

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
158047 2019-10-14T13:38:14 Z nvmdava 가로등 (APIO19_street_lamps) C++17
100 / 100
1720 ms 313576 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
 
#define N 300005
#define INF 0x3f3f3f3f3f3f3f3f

struct Node{
	Node *l = NULL, *r = NULL;
	int val = 0;

	int query(int le, int ri, int x){

		if(le == ri) return val;
		int m = (le + ri) >> 1;
		if(m < x)
			return val + (r == NULL ? 0 : r -> query(m + 1, ri, x));
		return val + (l == NULL ? 0 : l -> query(le, m, x));
	}

	void update(int le, int ri, int R, int v){
		if(ri <= R){
			val += v;
			return;
		}

		int m = (le + ri) >> 1;
		
		if(l == NULL)
			l = new Node();
		l -> update(le, m, R, v);

		if(m < R){
			if(r == NULL)
				r = new Node();
			r -> update(m + 1, ri, R, v);
		}
	}
} *ans[N << 2];

int n, q;

void update(int id, int l, int r, int L, int R, int val){
	if(L <= l && r <= R){
		ans[id] -> update(1, n, R, val);
		return;
	}
	if(r < L || l > R){
		return;
	}
	int m = (l + r) >> 1;
	update(id << 1, l, m, L, R, val);
	update(id << 1 | 1, m + 1, r, L, R, val);
}

int query(int id, int l, int r, int x, int a){
	if(l == r){
		return ans[id] -> query(1, n, a);
	}
	int m = (l + r) >> 1;

	if(m < x)
		return ans[id] -> query(1, n, a) + query(id << 1 | 1, m + 1, r, x, a);
	return ans[id] -> query(1, n, a) + query(id << 1, l, m, x, a);
}

set<pair<pair<int, int>, int> > s;
int t;

void remove(set<pair<pair<int, int>, int> >::iterator& it){
	update(1, 1, n, it -> ff.ss, it -> ff.ff, t - it -> ss);
	it = s.erase(it);
}

int st[N];

pair<int, int> tmp;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin>>n>>q;
	tmp.ff = n + 1;

	for(int i = 1; i < (n << 2); ++i)
		ans[i] = new Node();

	for(int i = 1; i <= n; i++){
		char c;
		cin>>c;
		st[i] = c - '0';
		if(st[i] == 1){
			if(tmp.ff > n)
				tmp.ss = i;
			tmp.ff = i;
		} else {
			if(tmp.ff <= n){
				s.insert({tmp, 0});
				tmp.ff = n + 1;
			}
		}
	}
	
	if(tmp.ff <= n)
		s.insert({tmp, 0});



	for(t = 1; t <= q; t++){
		string q;
		int a, b;
		cin>>q;
		if(q[0] == 'q'){
			cin>>a>>b;
			b--;
			auto it = s.lower_bound({{a, 0}, 0});
			if(it == s.end())
				cout<<query(1, 1, n, a, b)<<'\n';
			else
				cout<<query(1, 1, n, a, b) + (it -> ff.ss <= a && it -> ff.ff >= b ? t - it -> ss : 0)<<'\n';
		} else {
			cin>>a;
			if(st[a] == 1){
				auto it = s.lower_bound({{a, 0}, 0});
				
				int le = it -> ff.ss;
				int ri = it -> ff.ff;

				remove(it);

				if(le < a)
					s.insert({{a - 1, le}, t});
				if(ri > a)
					s.insert({{ri, a + 1}, t});
			} else {
				int ri = a;
				int le = a;
				auto it = s.lower_bound({{a, a}, 0});
				if(st[a + 1]){
					ri = it -> ff.ff;
					remove(it);
				}
				if(st[a - 1]){
					--it;
					le = it -> ff.ss;
					remove(it);
				}
				s.insert({{ri, le}, t});
			}
			st[a] ^= 1;
		}
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 178 ms 3516 KB Output is correct
2 Correct 225 ms 2844 KB Output is correct
3 Correct 422 ms 9052 KB Output is correct
4 Correct 1068 ms 181000 KB Output is correct
5 Correct 1505 ms 262244 KB Output is correct
6 Correct 1121 ms 197604 KB Output is correct
7 Correct 326 ms 50296 KB Output is correct
8 Correct 330 ms 51620 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1016 KB Output is correct
2 Correct 4 ms 1016 KB Output is correct
3 Correct 4 ms 932 KB Output is correct
4 Correct 3 ms 504 KB Output is correct
5 Correct 1720 ms 313576 KB Output is correct
6 Correct 1640 ms 302840 KB Output is correct
7 Correct 1390 ms 262348 KB Output is correct
8 Correct 340 ms 52540 KB Output is correct
9 Correct 95 ms 3448 KB Output is correct
10 Correct 102 ms 3320 KB Output is correct
11 Correct 104 ms 3448 KB Output is correct
12 Correct 318 ms 49996 KB Output is correct
13 Correct 334 ms 51368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 504 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 4 ms 760 KB Output is correct
4 Correct 4 ms 888 KB Output is correct
5 Correct 537 ms 58636 KB Output is correct
6 Correct 808 ms 141612 KB Output is correct
7 Correct 1083 ms 196984 KB Output is correct
8 Correct 1435 ms 250688 KB Output is correct
9 Correct 208 ms 2168 KB Output is correct
10 Correct 176 ms 1784 KB Output is correct
11 Correct 207 ms 2200 KB Output is correct
12 Correct 176 ms 1664 KB Output is correct
13 Correct 209 ms 2160 KB Output is correct
14 Correct 176 ms 1784 KB Output is correct
15 Correct 325 ms 50152 KB Output is correct
16 Correct 327 ms 51576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 380 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 178 ms 3516 KB Output is correct
9 Correct 225 ms 2844 KB Output is correct
10 Correct 422 ms 9052 KB Output is correct
11 Correct 1068 ms 181000 KB Output is correct
12 Correct 1505 ms 262244 KB Output is correct
13 Correct 1121 ms 197604 KB Output is correct
14 Correct 326 ms 50296 KB Output is correct
15 Correct 330 ms 51620 KB Output is correct
16 Correct 4 ms 1016 KB Output is correct
17 Correct 4 ms 1016 KB Output is correct
18 Correct 4 ms 932 KB Output is correct
19 Correct 3 ms 504 KB Output is correct
20 Correct 1720 ms 313576 KB Output is correct
21 Correct 1640 ms 302840 KB Output is correct
22 Correct 1390 ms 262348 KB Output is correct
23 Correct 340 ms 52540 KB Output is correct
24 Correct 95 ms 3448 KB Output is correct
25 Correct 102 ms 3320 KB Output is correct
26 Correct 104 ms 3448 KB Output is correct
27 Correct 318 ms 49996 KB Output is correct
28 Correct 334 ms 51368 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 3 ms 632 KB Output is correct
31 Correct 4 ms 760 KB Output is correct
32 Correct 4 ms 888 KB Output is correct
33 Correct 537 ms 58636 KB Output is correct
34 Correct 808 ms 141612 KB Output is correct
35 Correct 1083 ms 196984 KB Output is correct
36 Correct 1435 ms 250688 KB Output is correct
37 Correct 208 ms 2168 KB Output is correct
38 Correct 176 ms 1784 KB Output is correct
39 Correct 207 ms 2200 KB Output is correct
40 Correct 176 ms 1664 KB Output is correct
41 Correct 209 ms 2160 KB Output is correct
42 Correct 176 ms 1784 KB Output is correct
43 Correct 325 ms 50152 KB Output is correct
44 Correct 327 ms 51576 KB Output is correct
45 Correct 79 ms 2012 KB Output is correct
46 Correct 116 ms 1784 KB Output is correct
47 Correct 552 ms 79736 KB Output is correct
48 Correct 1025 ms 180216 KB Output is correct
49 Correct 110 ms 1784 KB Output is correct
50 Correct 111 ms 1792 KB Output is correct
51 Correct 116 ms 1936 KB Output is correct
52 Correct 120 ms 1844 KB Output is correct
53 Correct 125 ms 1820 KB Output is correct
54 Correct 119 ms 1832 KB Output is correct