답안 #1093627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1093627 2024-09-27T07:03:13 Z vjudge1 Inside information (BOI21_servers) C++17
100 / 100
300 ms 32104 KB
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
 
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
 
void Hollwo_Pelw();
 
signed main(){
#ifndef hollwo_pelw_local
	if (fopen("servers.inp", "r")){
		freopen("servers.inp", "r", stdin);
		freopen("servers.out", "w", stdout);
	}
#else
	auto start = chrono::steady_clock::now();
#endif
	cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
	int testcases = 1;
	// cin >> testcases;
	for (int test = 1; test <= testcases; test++){
		// cout << "Case #" << test << ": ";
		Hollwo_Pelw();
	}
#ifdef hollwo_pelw_local
	auto end = chrono::steady_clock::now();
	cout << "\nExcution time : " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
 
#define for_cen(v, w, u) for (auto [v, w] : adj[u]) if (!mark[v])
 
const int N = 1.2e5 + 5, Q = N << 1;
 
vector<pair<int, int>> query_q[N];
vector<int> query_c[N];
vector<pair<int, int>> adj[N];
 
int n, q, siz[N], mark[N];
 
int find_sz(int u, int p) {
	siz[u] = 1;
	for_cen(v, w, u) if (v != p)
		siz[u] += find_sz(v, u);
	return siz[u];
}
 
int find_cen(int u, int p, const int &tot) {
	for_cen(v, w, u) if (v != p)
		if (siz[v] > tot / 2) return find_cen(v, u, tot);
	return u;
}
 
struct fenwick_tree {
	int bit[Q], ver[Q], vercnt;
 
	inline void clear() { vercnt ++; }
 
	inline void add(int p, int v) {
		for (; p < Q; p += p & -p) {
			if (ver[p] ^ vercnt)
				ver[p] = vercnt, bit[p] = 0;
			bit[p] += v;
		}
	}
 
	inline int query(int p) {
		int r = 0;
		for (; p > 0; p -= p & -p) {
			if (ver[p] ^ vercnt)
				ver[p] = vercnt, bit[p] = 0;
			r += bit[p];
		}
		return r;
	}
 
} bit;
 
vector<int> inQ, outQ, allinQ;
 
int res[Q], curin[N], curout[N];
 
void dfs_in(int u, int p, int cur) {
	inQ.push_back(u);
 
	for_cen(v, w, u) if (v != p) {
		if (w < cur)
			dfs_in(v, u, w);
	}
}
 
void dfs_out(int u, int p, int cur) {
	outQ.push_back(u);
	
	bit.add(cur, 1);
	curout[u] = cur;
 
	for_cen(v, w, u) if (v != p) {
		if (w > cur)
			dfs_out(v, u, w);
	}
}
 
void centroid_decomp(int r) {
	int tot = find_sz(r, -1);
	r = find_cen(r, -1, tot);
 
	sort(adj[r].begin(), adj[r].end(), [&](const pair<int, int> &a, const pair<int, int> &b){
		return a.second > b.second;
	});
 
	bit.clear();
 
	curin[r] = 0;
 
	for_cen(v, w, r) {
		inQ.clear();
		outQ.clear();
		dfs_in(v, r, w);
 
		for (auto u : inQ) {
			for (auto i : query_c[u]) if (i >= w)
				res[i] += 1 + bit.query(i);
			curin[u] = w;
		}
 
		dfs_out(v, r, w);
	}
 
	for (auto i : query_c[r])
		res[i] += 1 + bit.query(i);
 
	reverse(adj[r].begin(), adj[r].end());
 
	bit.clear();
	
	allinQ.push_back(r);
 
	for_cen(v, w, r) {
		inQ.clear();
		outQ.clear();
		dfs_in(v, r, w);
 
		for (auto u : inQ) {
			allinQ.push_back(u);
		}
 
		dfs_out(v, r, w);
 
		for (auto u : outQ) {
			for (auto [i, v] : query_q[u])
				if (curin[v] != -1 && curin[v] < w && i > curout[u]) {
					res[i] = 1;
				}
		}
	}
 
	for (auto [i, v] : query_q[r])
		if (curin[v] != -1 && i > curin[v]) {
			res[i] = 1;
		}
 
	for (auto u : allinQ)
		curin[u] = -1;
 
	allinQ.clear();
 
	mark[r] = 1;
	for_cen(v, w, r)
		centroid_decomp(v);
}
 
char t[Q];
 
void Hollwo_Pelw(){
	cin >> n >> q, q += n - 1;
	for (int i = 1, u, v; i <= q; i++) {
		cin >> t[i];
		if (t[i] == 'S') {
			cin >> u >> v;
			adj[u].emplace_back(v, i);
			adj[v].emplace_back(u, i);
		}
		if (t[i] == 'Q') {
			cin >> u >> v;
			query_q[u].emplace_back(i, v);
		}
		if (t[i] == 'C') {
			cin >> u;
			query_c[u].push_back(i);
		}
	}
 
	memset(curin, -1, sizeof curin);
 
	centroid_decomp(1);
 
	for (int i = 1; i <= q; i++) {
		if (t[i] == 'Q')
			cout << (res[i] ? "yes\n" : "no\n");
		if (t[i] == 'C')
			cout << res[i] << '\n';
	}
 
}

Compilation message

servers.cpp: In function 'int main()':
servers.cpp:15:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   15 |   freopen("servers.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
servers.cpp:16:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   16 |   freopen("servers.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13400 KB Output is correct
2 Correct 24 ms 14168 KB Output is correct
3 Correct 23 ms 14420 KB Output is correct
4 Correct 24 ms 14428 KB Output is correct
5 Correct 24 ms 13660 KB Output is correct
6 Correct 23 ms 13992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13400 KB Output is correct
2 Correct 24 ms 14168 KB Output is correct
3 Correct 23 ms 14420 KB Output is correct
4 Correct 24 ms 14428 KB Output is correct
5 Correct 24 ms 13660 KB Output is correct
6 Correct 23 ms 13992 KB Output is correct
7 Correct 17 ms 13536 KB Output is correct
8 Correct 32 ms 14168 KB Output is correct
9 Correct 25 ms 13652 KB Output is correct
10 Correct 28 ms 13916 KB Output is correct
11 Correct 29 ms 14128 KB Output is correct
12 Correct 27 ms 13912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13404 KB Output is correct
2 Correct 81 ms 24516 KB Output is correct
3 Correct 81 ms 25028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13404 KB Output is correct
2 Correct 81 ms 24516 KB Output is correct
3 Correct 81 ms 25028 KB Output is correct
4 Correct 17 ms 13396 KB Output is correct
5 Correct 89 ms 24672 KB Output is correct
6 Correct 65 ms 22212 KB Output is correct
7 Correct 94 ms 22500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 13400 KB Output is correct
2 Correct 149 ms 30532 KB Output is correct
3 Correct 155 ms 30552 KB Output is correct
4 Correct 155 ms 31180 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 13400 KB Output is correct
2 Correct 149 ms 30532 KB Output is correct
3 Correct 155 ms 30552 KB Output is correct
4 Correct 155 ms 31180 KB Output is correct
5 Correct 17 ms 13488 KB Output is correct
6 Correct 156 ms 30804 KB Output is correct
7 Correct 209 ms 31696 KB Output is correct
8 Correct 151 ms 29264 KB Output is correct
9 Correct 150 ms 29160 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 13136 KB Output is correct
2 Correct 121 ms 23368 KB Output is correct
3 Correct 144 ms 22924 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 13136 KB Output is correct
2 Correct 121 ms 23368 KB Output is correct
3 Correct 144 ms 22924 KB Output is correct
4 Correct 21 ms 13392 KB Output is correct
5 Correct 153 ms 24220 KB Output is correct
6 Correct 130 ms 23164 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13148 KB Output is correct
2 Correct 141 ms 29604 KB Output is correct
3 Correct 180 ms 29524 KB Output is correct
4 Correct 150 ms 30336 KB Output is correct
5 Correct 17 ms 13180 KB Output is correct
6 Correct 129 ms 23404 KB Output is correct
7 Correct 116 ms 22864 KB Output is correct
8 Correct 133 ms 22864 KB Output is correct
9 Correct 124 ms 23636 KB Output is correct
10 Correct 181 ms 27556 KB Output is correct
11 Correct 170 ms 25692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 13148 KB Output is correct
2 Correct 141 ms 29604 KB Output is correct
3 Correct 180 ms 29524 KB Output is correct
4 Correct 150 ms 30336 KB Output is correct
5 Correct 17 ms 13180 KB Output is correct
6 Correct 129 ms 23404 KB Output is correct
7 Correct 116 ms 22864 KB Output is correct
8 Correct 133 ms 22864 KB Output is correct
9 Correct 124 ms 23636 KB Output is correct
10 Correct 181 ms 27556 KB Output is correct
11 Correct 170 ms 25692 KB Output is correct
12 Correct 17 ms 13216 KB Output is correct
13 Correct 158 ms 29920 KB Output is correct
14 Correct 164 ms 31444 KB Output is correct
15 Correct 166 ms 30004 KB Output is correct
16 Correct 155 ms 30032 KB Output is correct
17 Correct 19 ms 13556 KB Output is correct
18 Correct 156 ms 25040 KB Output is correct
19 Correct 136 ms 24052 KB Output is correct
20 Correct 173 ms 24912 KB Output is correct
21 Correct 143 ms 24988 KB Output is correct
22 Correct 224 ms 27028 KB Output is correct
23 Correct 300 ms 27728 KB Output is correct
24 Correct 284 ms 27352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13404 KB Output is correct
2 Correct 24 ms 14172 KB Output is correct
3 Correct 25 ms 13904 KB Output is correct
4 Correct 26 ms 13908 KB Output is correct
5 Correct 30 ms 13312 KB Output is correct
6 Correct 26 ms 13572 KB Output is correct
7 Correct 17 ms 13396 KB Output is correct
8 Correct 81 ms 23760 KB Output is correct
9 Correct 84 ms 25008 KB Output is correct
10 Correct 18 ms 13400 KB Output is correct
11 Correct 148 ms 31216 KB Output is correct
12 Correct 138 ms 31060 KB Output is correct
13 Correct 136 ms 31692 KB Output is correct
14 Correct 17 ms 13404 KB Output is correct
15 Correct 130 ms 24892 KB Output is correct
16 Correct 116 ms 24404 KB Output is correct
17 Correct 121 ms 24144 KB Output is correct
18 Correct 133 ms 25160 KB Output is correct
19 Correct 205 ms 28976 KB Output is correct
20 Correct 206 ms 27228 KB Output is correct
21 Correct 94 ms 25408 KB Output is correct
22 Correct 158 ms 25208 KB Output is correct
23 Correct 109 ms 24656 KB Output is correct
24 Correct 111 ms 24912 KB Output is correct
25 Correct 199 ms 28484 KB Output is correct
26 Correct 153 ms 23888 KB Output is correct
27 Correct 156 ms 23880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 13404 KB Output is correct
2 Correct 24 ms 14172 KB Output is correct
3 Correct 25 ms 13904 KB Output is correct
4 Correct 26 ms 13908 KB Output is correct
5 Correct 30 ms 13312 KB Output is correct
6 Correct 26 ms 13572 KB Output is correct
7 Correct 17 ms 13396 KB Output is correct
8 Correct 81 ms 23760 KB Output is correct
9 Correct 84 ms 25008 KB Output is correct
10 Correct 18 ms 13400 KB Output is correct
11 Correct 148 ms 31216 KB Output is correct
12 Correct 138 ms 31060 KB Output is correct
13 Correct 136 ms 31692 KB Output is correct
14 Correct 17 ms 13404 KB Output is correct
15 Correct 130 ms 24892 KB Output is correct
16 Correct 116 ms 24404 KB Output is correct
17 Correct 121 ms 24144 KB Output is correct
18 Correct 133 ms 25160 KB Output is correct
19 Correct 205 ms 28976 KB Output is correct
20 Correct 206 ms 27228 KB Output is correct
21 Correct 94 ms 25408 KB Output is correct
22 Correct 158 ms 25208 KB Output is correct
23 Correct 109 ms 24656 KB Output is correct
24 Correct 111 ms 24912 KB Output is correct
25 Correct 199 ms 28484 KB Output is correct
26 Correct 153 ms 23888 KB Output is correct
27 Correct 156 ms 23880 KB Output is correct
28 Correct 18 ms 13396 KB Output is correct
29 Correct 30 ms 13904 KB Output is correct
30 Correct 25 ms 13660 KB Output is correct
31 Correct 30 ms 13948 KB Output is correct
32 Correct 31 ms 13916 KB Output is correct
33 Correct 26 ms 13908 KB Output is correct
34 Correct 18 ms 13404 KB Output is correct
35 Correct 95 ms 24512 KB Output is correct
36 Correct 67 ms 22208 KB Output is correct
37 Correct 73 ms 22476 KB Output is correct
38 Correct 19 ms 13392 KB Output is correct
39 Correct 158 ms 31064 KB Output is correct
40 Correct 168 ms 32104 KB Output is correct
41 Correct 156 ms 30292 KB Output is correct
42 Correct 149 ms 30352 KB Output is correct
43 Correct 18 ms 13404 KB Output is correct
44 Correct 167 ms 25504 KB Output is correct
45 Correct 128 ms 24196 KB Output is correct
46 Correct 143 ms 25424 KB Output is correct
47 Correct 151 ms 25428 KB Output is correct
48 Correct 184 ms 27504 KB Output is correct
49 Correct 294 ms 27988 KB Output is correct
50 Correct 286 ms 27608 KB Output is correct
51 Correct 73 ms 23360 KB Output is correct
52 Correct 70 ms 23232 KB Output is correct
53 Correct 65 ms 22808 KB Output is correct
54 Correct 62 ms 23480 KB Output is correct
55 Correct 69 ms 23480 KB Output is correct
56 Correct 110 ms 23892 KB Output is correct
57 Correct 136 ms 26188 KB Output is correct
58 Correct 215 ms 24164 KB Output is correct
59 Correct 161 ms 24016 KB Output is correct