답안 #447216

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
447216 2021-07-25T06:46:28 Z hhhhaura Inside information (BOI21_servers) C++14
100 / 100
1004 ms 51496 KB
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define all(x) x.begin(), x.end()

#define INF 1000000000
#define MOD 1000000007
#define eps (1e-9)

using namespace std;
#define pii pair<int, int>
struct BIT {
	int n;
	vector<int> v;
	stack<pii> ops;
	void init_(int _n) {
		n = _n;
		v.assign(n + 1, 0);
	}
	int lowbit(int x) { return x & (-x); }
	void modify(int x, int val) {
		if(x == 0) return;
		if(val > 0) ops.push({x, val});
		for(int i = x; i <= n; i += lowbit(i)) {
			v[i] += val;
		}
	}
	int query(int x) {
		int ans = 0;
		for(int i = x; i > 0; i -= lowbit(i)) {
			ans += v[i];
		}
		return ans;
	}
	int ask(int x) {
		return query(n) - query(x - 1);
	}
	void undo() {
		while(ops.size()) {
			pii cur = ops.top(); ops.pop();
			modify(cur.first, -cur.second);
		}
	}
};

namespace solver {
	int n, k;
	BIT bit;
	vector<int> vis, mx, sz, R, ch;
	vector<int> L, J, es, cost, t, ans, tp;
	vector<pii> a;
	vector<vector<pii>> nxt;
	vector<vector<int>> mp;
	void init_(int _n, int _k) {
		n = _n, k = _k;
		bit.init_(n + k);
		vis.assign(n + 1, 0);
		mx.assign(n + 1, 0);
		sz.assign(n + 1, 0);
		R.assign(n + 1, 0);
		L.assign(n + 1, 0);
		J.assign(n + 1, 0);
		
		es.assign(1, 0);
		cost.assign(1, INF);
		ch.clear();
		
		tp.assign(k + 1, 0);
		a.assign(k + 1, {0, 0});
		ans.assign(k + 1, 0);
		t.assign(k + 1, 0);
		nxt.assign(n + 1, vector<pii>());
		mp.assign(n + 1, vector<int>());
	}
	void add_edge(int a, int b, int c) {
		mp[a].push_back(es.size());
		mp[b].push_back(es.size());
		es.push_back(a ^ b);
		cost.push_back(c);
	}
	void get_sz(int x, int par) {
		mx[x] = 0, sz[x] = 1;
		for(auto i : mp[x]) if(i != par) {
			int to = es[i] ^ x;
			if(vis[to]) continue;
			get_sz(to, i);
			sz[x] += sz[to];
			mx[x] = max(mx[x], sz[to]);
		}
	}
	int get_cen(int x, int par, int tot) {
		int best = x;
		for(auto i : mp[x]) if(i != par) {
			int to = es[i] ^ x;
			if(vis[to]) continue;
			int cur = get_cen(to, i, tot);
			if(max(mx[cur], tot - sz[cur]) < 
				max(mx[best], tot - sz[best])) best = cur;
		}
		return best;
	}
	void dfs(int x, int par, int l, int j, int r) {
		J[x] = j, L[x] = l, R[x] = r;
		ch.push_back(x);
		for(auto i : mp[x]) if(i != par) {
			int to = es[i] ^ x;
			if(vis[to]) continue;
			dfs(to, i, l,
				(cost[par] > cost[i] ? j : INF), 
				(cost[par] < cost[i] ? max(cost[i], r) : INF)
			);
		}
	}
	void deco(int x, vector<pii> q) {
		ch.clear();
		get_sz(x, -1);
		int c = get_cen(x, -1, sz[x]);
		vis[c] = 1;
		R[c] = J[c] = L[c] = 0;
		vector<vector<pii>> temp(mp[c].size(), vector<pii>());
		for(auto i : mp[c]) {
			int to = es[i] ^ c;
			if(vis[to]) continue;
			nxt[i].clear();
			dfs(to, i, i, cost[i], cost[i]);
		}
		vector<pii> s;
		for(auto i : q) {
			if(i.first == 1) {
				int x, y; tie(x, y) = a[i.second];
				if(L[x] == L[y]) nxt[L[x]].push_back(i);
				else if(x == c) ans[i.second] = (t[i.second] > J[y]);	
				else if(y == c) ans[i.second] = (t[i.second] > R[x]);	
				else ans[i.second] = (J[y] < cost[L[x]] && t[i.second] > R[x]);	
			}
			else s.push_back({0, i.second});
		}
		for(auto i : ch) s.push_back({1, i});
		sort(all(s), [](pii a, pii b) {
			int t1 = (a.first? R[a.second] : t[a.second]);
			int t2 = (b.first? R[b.second] : t[b.second]);
			return t1 < t2;
		});			 
		for(auto i : s) {
			if(i.first) bit.modify(min(n + k, cost[L[i.second]]), 1);
			else {
				int x = a[i.second].first;
				ans[i.second] += bit.ask(min(J[x] + 1, n + k + 1)) + (J[x] < t[i.second]);
				if(x != c) nxt[L[x]].push_back({2, i.second});
			}
 		}
		bit.undo();
		rep(i, 0, signed(mp[c].size()) - 1) {
			temp[i].swap(nxt[mp[c][i]]);
		}
		rep(i, 0, signed(mp[c].size()) - 1) {
			int e = mp[c][i], to = es[e] ^ c;
			if(vis[to]) continue;
			deco(to, temp[i]);
		}
		return;
	}
};
using namespace solver;
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	int n, k, cur = 0; 
	cin >> n >> k;
	init_(n, k);
	vector<pii> q;
	rep(i, 1, n + k - 1) {
		char c; cin >> c;
		if(c == 'S') {
			int x, y; cin >> x >> y;
			add_edge(x, y, i);
		}
		else if(c == 'Q') {
			int x, y; cin >> x >> y;
			a[++cur] = {x, y};
			tp[cur] = 1, t[cur] = i;
			if(x == y) ans[cur] = 1;
			else q.push_back({1, cur});
		}
		else {
			int x; cin >> x;
			a[++cur] = {x, -1};
			tp[cur] = 2, t[cur] = i;
			q.push_back({2, cur});
		} 
	}
	deco(1, q);
	rep(i, 1, k) {
		if(tp[i] == 1) cout << (ans[i] ? "yes" : "no") << "\n";
		else cout << ans[i] << "\n";
	}
	return 0;
}
/*
6 9
S 1 2
S 1 3
S 3 4
Q 5 1
S 4 5
S 1 6
Q 5 1
Q 1 5
C 1
C 2
C 3
C 4
C 5
C 6
*/

Compilation message

servers.cpp:3: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
    3 | #pragma loop-opt(on)
      |
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5320 KB Output is correct
2 Correct 47 ms 6244 KB Output is correct
3 Correct 46 ms 7600 KB Output is correct
4 Correct 50 ms 6668 KB Output is correct
5 Correct 51 ms 6856 KB Output is correct
6 Correct 40 ms 5644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5320 KB Output is correct
2 Correct 47 ms 6244 KB Output is correct
3 Correct 46 ms 7600 KB Output is correct
4 Correct 50 ms 6668 KB Output is correct
5 Correct 51 ms 6856 KB Output is correct
6 Correct 40 ms 5644 KB Output is correct
7 Correct 38 ms 5880 KB Output is correct
8 Correct 88 ms 8120 KB Output is correct
9 Correct 72 ms 8924 KB Output is correct
10 Correct 123 ms 8808 KB Output is correct
11 Correct 118 ms 9016 KB Output is correct
12 Correct 55 ms 6820 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5088 KB Output is correct
2 Correct 141 ms 24332 KB Output is correct
3 Correct 171 ms 24416 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5088 KB Output is correct
2 Correct 141 ms 24332 KB Output is correct
3 Correct 171 ms 24416 KB Output is correct
4 Correct 43 ms 5216 KB Output is correct
5 Correct 186 ms 25492 KB Output is correct
6 Correct 211 ms 25488 KB Output is correct
7 Correct 200 ms 26536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5892 KB Output is correct
2 Correct 681 ms 34240 KB Output is correct
3 Correct 668 ms 34344 KB Output is correct
4 Correct 457 ms 35500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 5892 KB Output is correct
2 Correct 681 ms 34240 KB Output is correct
3 Correct 668 ms 34344 KB Output is correct
4 Correct 457 ms 35500 KB Output is correct
5 Correct 38 ms 6096 KB Output is correct
6 Correct 812 ms 36060 KB Output is correct
7 Correct 553 ms 37448 KB Output is correct
8 Correct 880 ms 37160 KB Output is correct
9 Correct 863 ms 37152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5820 KB Output is correct
2 Correct 388 ms 22344 KB Output is correct
3 Correct 554 ms 22576 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5820 KB Output is correct
2 Correct 388 ms 22344 KB Output is correct
3 Correct 554 ms 22576 KB Output is correct
4 Correct 38 ms 5824 KB Output is correct
5 Correct 529 ms 24268 KB Output is correct
6 Correct 699 ms 23856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5964 KB Output is correct
2 Correct 664 ms 34144 KB Output is correct
3 Correct 678 ms 34208 KB Output is correct
4 Correct 454 ms 35560 KB Output is correct
5 Correct 34 ms 5884 KB Output is correct
6 Correct 384 ms 22420 KB Output is correct
7 Correct 551 ms 22576 KB Output is correct
8 Correct 520 ms 22696 KB Output is correct
9 Correct 556 ms 23264 KB Output is correct
10 Correct 655 ms 26624 KB Output is correct
11 Correct 644 ms 25860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 35 ms 5964 KB Output is correct
2 Correct 664 ms 34144 KB Output is correct
3 Correct 678 ms 34208 KB Output is correct
4 Correct 454 ms 35560 KB Output is correct
5 Correct 34 ms 5884 KB Output is correct
6 Correct 384 ms 22420 KB Output is correct
7 Correct 551 ms 22576 KB Output is correct
8 Correct 520 ms 22696 KB Output is correct
9 Correct 556 ms 23264 KB Output is correct
10 Correct 655 ms 26624 KB Output is correct
11 Correct 644 ms 25860 KB Output is correct
12 Correct 38 ms 6040 KB Output is correct
13 Correct 776 ms 36072 KB Output is correct
14 Correct 545 ms 37548 KB Output is correct
15 Correct 901 ms 37160 KB Output is correct
16 Correct 873 ms 37292 KB Output is correct
17 Correct 37 ms 5836 KB Output is correct
18 Correct 515 ms 24184 KB Output is correct
19 Correct 672 ms 23848 KB Output is correct
20 Correct 780 ms 25124 KB Output is correct
21 Correct 717 ms 25548 KB Output is correct
22 Correct 890 ms 28960 KB Output is correct
23 Correct 1004 ms 29092 KB Output is correct
24 Correct 804 ms 27792 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5312 KB Output is correct
2 Correct 47 ms 6264 KB Output is correct
3 Correct 47 ms 7672 KB Output is correct
4 Correct 52 ms 6636 KB Output is correct
5 Correct 52 ms 6968 KB Output is correct
6 Correct 41 ms 5664 KB Output is correct
7 Correct 33 ms 5084 KB Output is correct
8 Correct 149 ms 24292 KB Output is correct
9 Correct 159 ms 24312 KB Output is correct
10 Correct 36 ms 5952 KB Output is correct
11 Correct 667 ms 34160 KB Output is correct
12 Correct 651 ms 34212 KB Output is correct
13 Correct 446 ms 35496 KB Output is correct
14 Correct 34 ms 5844 KB Output is correct
15 Correct 387 ms 22328 KB Output is correct
16 Correct 540 ms 22480 KB Output is correct
17 Correct 517 ms 22704 KB Output is correct
18 Correct 567 ms 23080 KB Output is correct
19 Correct 664 ms 26412 KB Output is correct
20 Correct 624 ms 25768 KB Output is correct
21 Correct 179 ms 25184 KB Output is correct
22 Correct 188 ms 24368 KB Output is correct
23 Correct 422 ms 24444 KB Output is correct
24 Correct 391 ms 24292 KB Output is correct
25 Correct 598 ms 29264 KB Output is correct
26 Correct 496 ms 22088 KB Output is correct
27 Correct 481 ms 22116 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 5312 KB Output is correct
2 Correct 47 ms 6264 KB Output is correct
3 Correct 47 ms 7672 KB Output is correct
4 Correct 52 ms 6636 KB Output is correct
5 Correct 52 ms 6968 KB Output is correct
6 Correct 41 ms 5664 KB Output is correct
7 Correct 33 ms 5084 KB Output is correct
8 Correct 149 ms 24292 KB Output is correct
9 Correct 159 ms 24312 KB Output is correct
10 Correct 36 ms 5952 KB Output is correct
11 Correct 667 ms 34160 KB Output is correct
12 Correct 651 ms 34212 KB Output is correct
13 Correct 446 ms 35496 KB Output is correct
14 Correct 34 ms 5844 KB Output is correct
15 Correct 387 ms 22328 KB Output is correct
16 Correct 540 ms 22480 KB Output is correct
17 Correct 517 ms 22704 KB Output is correct
18 Correct 567 ms 23080 KB Output is correct
19 Correct 664 ms 26412 KB Output is correct
20 Correct 624 ms 25768 KB Output is correct
21 Correct 179 ms 25184 KB Output is correct
22 Correct 188 ms 24368 KB Output is correct
23 Correct 422 ms 24444 KB Output is correct
24 Correct 391 ms 24292 KB Output is correct
25 Correct 598 ms 29264 KB Output is correct
26 Correct 496 ms 22088 KB Output is correct
27 Correct 481 ms 22116 KB Output is correct
28 Correct 40 ms 5920 KB Output is correct
29 Correct 92 ms 8112 KB Output is correct
30 Correct 70 ms 8880 KB Output is correct
31 Correct 122 ms 8760 KB Output is correct
32 Correct 119 ms 9048 KB Output is correct
33 Correct 55 ms 6860 KB Output is correct
34 Correct 34 ms 5184 KB Output is correct
35 Correct 187 ms 25496 KB Output is correct
36 Correct 199 ms 25544 KB Output is correct
37 Correct 205 ms 26400 KB Output is correct
38 Correct 39 ms 6088 KB Output is correct
39 Correct 773 ms 36016 KB Output is correct
40 Correct 536 ms 37488 KB Output is correct
41 Correct 887 ms 37268 KB Output is correct
42 Correct 870 ms 37408 KB Output is correct
43 Correct 37 ms 5808 KB Output is correct
44 Correct 523 ms 24192 KB Output is correct
45 Correct 674 ms 23880 KB Output is correct
46 Correct 797 ms 25140 KB Output is correct
47 Correct 711 ms 25624 KB Output is correct
48 Correct 905 ms 29192 KB Output is correct
49 Correct 961 ms 29108 KB Output is correct
50 Correct 791 ms 27820 KB Output is correct
51 Correct 211 ms 27332 KB Output is correct
52 Correct 198 ms 26540 KB Output is correct
53 Correct 217 ms 26456 KB Output is correct
54 Correct 202 ms 25536 KB Output is correct
55 Correct 203 ms 30120 KB Output is correct
56 Correct 460 ms 28312 KB Output is correct
57 Correct 710 ms 51496 KB Output is correct
58 Correct 770 ms 25348 KB Output is correct
59 Correct 533 ms 22356 KB Output is correct