Submission #528508

# Submission time Handle Problem Language Result Execution time Memory
528508 2022-02-20T12:16:08 Z koioi.org-koosaga Inside information (BOI21_servers) C++17
100 / 100
523 ms 74576 KB
#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
using namespace std;
typedef long long lint;
using pi = pair<int, int>;
const int MAXN = 120005;

vector<pi> gph[MAXN];

namespace CD{
	int sz[MAXN], msz[MAXN], vis[MAXN];
	vector<int> dfn;
	void dfs(int x, int p = -1){
		sz[x] = 1;
		msz[x] = 0;
		dfn.push_back(x);
		for(auto &[_, y] : gph[x]){
			if(y != p && !vis[y]){
				dfs(y, x);
				sz[x] += sz[y];
				msz[x] = max(msz[x], sz[y]);
			}
		}
	}
	int get_center(int x){
		dfn.clear();
		dfs(x);
		pi ret(1e9, -1);
		for(auto &v : dfn){
			int val = max(msz[v], sz(dfn) - sz[v]);
			ret = min(ret, pi(val, v));
		}
		return ret.second;
	}
	pi good[17][MAXN];
	pi can[17][MAXN];
	void dfs2(int x, int l, int r, int p, vector<pi> &strg, int depth){
		can[depth][x] = pi(l, r);
		strg.emplace_back(l, r);
		for(auto &[c, y] : gph[x]){
			if(!vis[y] && y != p && r < c) dfs2(y, l, c, x, strg, depth);
		}
	}
	void dfs3(int x, int l, int r, int p, int depth){
		good[depth][x].second = 1;
		good[depth][x].first = l;
		for(auto &[c, y] : gph[x]){
			if(!vis[y] && y != p && r > c) dfs3(y, l, c, x, depth);
		}
	}
	int par[MAXN], dep[MAXN];
	vector<pi> ds[MAXN];
	void init(int n){
		memset(can, 0x3f, sizeof(can));
		queue<pi> que;
		que.push(pi(1, -1));
		while(sz(que)){
			int x = que.front().first;
			int p = que.front().second;
			que.pop();
			x = get_center(x);
			vis[x] = 1;
			par[x] = p;
			if(~p) dep[x] = dep[p] + 1;
			good[dep[x]][x] = pi(-1e9, 1);
			can[dep[x]][x] = pi(1e9, -1e9);
			for(auto &[c, y] : gph[x]){
				if(vis[y]) continue;
				que.emplace(y, x);
				dfs2(y, c, c, -1, ds[x], dep[x]);
				dfs3(y, c, c, -1, dep[x]);
			}
		}
	}
	int ans[MAXN];
	struct query{
		int x, y, idx;
	};
	vector<query> qry[MAXN];
	void query(int v, int hi, int stackIdx){
		for(int w = v; ~w; w = par[w]){
			if(!good[dep[w]][v].second) continue;
			if(good[dep[w]][v].first > hi) continue;
			int lo = good[dep[w]][v].first + 1;
			ans[stackIdx]++;
			qry[w].push_back({lo, hi, stackIdx});
		}
	}
	int lca(int x, int y){
		if(dep[x] > dep[y]) swap(x, y);
		while(dep[x] < dep[y]) y = par[y];
		while(x != y){
			x = par[x], y = par[y];
		}
		return x;
	}
	bool reach(int u, int v, int d){
		int l = lca(u, v);
		if(!good[dep[l]][u].second) return 0;
		if(can[dep[l]][v].second > d) return 0;
		if(good[dep[l]][u].first > d) return 0;
		if(good[dep[l]][u].first > can[dep[l]][v].first) return 0;
		return 1;
	}
	struct bit{
		int tree[MAXN];
		void add(int x, int v){
			if(x <= 0) return;
			for(int i = x; i < MAXN; i += i & -i) tree[i] += v;
		}
		int query(int x){
			if(x <= 0) return 0;
			int ret = 0;
			for(int i = x; i; i -= i & -i) ret += tree[i];
			return ret;
		}
	}bit;
	vector<int> sweep(int n, int q){
		for(int i = 1; i <= n; i++){
			sort(all(qry[i]), [&](struct query a, struct query b){
				return a.y < b.y;
			});
			for(auto &[x, y] : ds[i]) swap(x, y);
			sort(all(ds[i]));
			int j = 0;
			for(auto &p : qry[i]){
				while(j < sz(ds[i]) && ds[i][j].first <= p.y){
					bit.add(ds[i][j++].second, 1);
				}
				ans[p.idx] += bit.query(MAXN - 1) - bit.query(p.x - 1);
			}
			while(j > 0){
				bit.add(ds[i][--j].second, -1);
			}
		}
		return vector<int>(ans, ans + q);
	}
};

struct query{
	int t, u, v, lim, idx;
};

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int n, q;
	cin >> n >> q;
	int c = 0;
	vector<query> qry;
	for(int i = 0; i < n+q-1; i++){
		string s; cin >> s;
		if(s == "S"){
			int u, v; cin >> u >> v;
			c++;
			gph[u].emplace_back(c, v);
			gph[v].emplace_back(c, u);
		}
		else if(s == "Q"){
			int u, v; cin >> u >> v;
			int sq = sz(qry);
			qry.push_back({1, v, u, c, sq});
		}
		else{
			int sq = sz(qry);
			int u; cin >> u;
			qry.push_back({2, u, -1, c, sq});
		}
	}
	CD::init(n);
	for(auto &i : qry){
		if(i.t == 2) CD::query(i.u, i.lim, i.idx);
	}
	auto swp = CD::sweep(n, sz(qry));
	for(auto &i : qry){
		if(i.t == 1) cout << (CD::reach(i.u, i.v, i.lim) ? "yes" : "no") << "\n";
		else cout << swp[i.idx] << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 44 ms 28956 KB Output is correct
2 Correct 68 ms 29792 KB Output is correct
3 Correct 55 ms 29644 KB Output is correct
4 Correct 66 ms 29928 KB Output is correct
5 Correct 53 ms 30084 KB Output is correct
6 Correct 46 ms 29532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 44 ms 28956 KB Output is correct
2 Correct 68 ms 29792 KB Output is correct
3 Correct 55 ms 29644 KB Output is correct
4 Correct 66 ms 29928 KB Output is correct
5 Correct 53 ms 30084 KB Output is correct
6 Correct 46 ms 29532 KB Output is correct
7 Correct 47 ms 29408 KB Output is correct
8 Correct 61 ms 31920 KB Output is correct
9 Correct 57 ms 32088 KB Output is correct
10 Correct 63 ms 32024 KB Output is correct
11 Correct 63 ms 32972 KB Output is correct
12 Correct 50 ms 31680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 28832 KB Output is correct
2 Correct 132 ms 40264 KB Output is correct
3 Correct 139 ms 40176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 28832 KB Output is correct
2 Correct 132 ms 40264 KB Output is correct
3 Correct 139 ms 40176 KB Output is correct
4 Correct 42 ms 29260 KB Output is correct
5 Correct 131 ms 41868 KB Output is correct
6 Correct 117 ms 43528 KB Output is correct
7 Correct 123 ms 44204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 28780 KB Output is correct
2 Correct 267 ms 58064 KB Output is correct
3 Correct 263 ms 58240 KB Output is correct
4 Correct 181 ms 68064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 28780 KB Output is correct
2 Correct 267 ms 58064 KB Output is correct
3 Correct 263 ms 58240 KB Output is correct
4 Correct 181 ms 68064 KB Output is correct
5 Correct 44 ms 29368 KB Output is correct
6 Correct 287 ms 61024 KB Output is correct
7 Correct 256 ms 74064 KB Output is correct
8 Correct 287 ms 62472 KB Output is correct
9 Correct 295 ms 62444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 28816 KB Output is correct
2 Correct 193 ms 54240 KB Output is correct
3 Correct 222 ms 46488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 28816 KB Output is correct
2 Correct 193 ms 54240 KB Output is correct
3 Correct 222 ms 46488 KB Output is correct
4 Correct 43 ms 29356 KB Output is correct
5 Correct 265 ms 62260 KB Output is correct
6 Correct 270 ms 50124 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 28872 KB Output is correct
2 Correct 253 ms 58048 KB Output is correct
3 Correct 255 ms 58092 KB Output is correct
4 Correct 185 ms 68112 KB Output is correct
5 Correct 42 ms 28876 KB Output is correct
6 Correct 186 ms 54136 KB Output is correct
7 Correct 229 ms 46500 KB Output is correct
8 Correct 280 ms 52076 KB Output is correct
9 Correct 277 ms 51536 KB Output is correct
10 Correct 321 ms 62984 KB Output is correct
11 Correct 314 ms 62368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 28872 KB Output is correct
2 Correct 253 ms 58048 KB Output is correct
3 Correct 255 ms 58092 KB Output is correct
4 Correct 185 ms 68112 KB Output is correct
5 Correct 42 ms 28876 KB Output is correct
6 Correct 186 ms 54136 KB Output is correct
7 Correct 229 ms 46500 KB Output is correct
8 Correct 280 ms 52076 KB Output is correct
9 Correct 277 ms 51536 KB Output is correct
10 Correct 321 ms 62984 KB Output is correct
11 Correct 314 ms 62368 KB Output is correct
12 Correct 43 ms 29364 KB Output is correct
13 Correct 279 ms 60992 KB Output is correct
14 Correct 256 ms 73960 KB Output is correct
15 Correct 288 ms 62572 KB Output is correct
16 Correct 324 ms 62400 KB Output is correct
17 Correct 42 ms 29372 KB Output is correct
18 Correct 262 ms 62272 KB Output is correct
19 Correct 272 ms 50080 KB Output is correct
20 Correct 308 ms 56524 KB Output is correct
21 Correct 306 ms 55404 KB Output is correct
22 Correct 402 ms 68392 KB Output is correct
23 Correct 523 ms 73116 KB Output is correct
24 Correct 446 ms 68056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 28852 KB Output is correct
2 Correct 51 ms 29776 KB Output is correct
3 Correct 55 ms 29680 KB Output is correct
4 Correct 55 ms 29884 KB Output is correct
5 Correct 52 ms 30024 KB Output is correct
6 Correct 45 ms 29496 KB Output is correct
7 Correct 40 ms 28848 KB Output is correct
8 Correct 111 ms 40320 KB Output is correct
9 Correct 110 ms 40276 KB Output is correct
10 Correct 42 ms 28848 KB Output is correct
11 Correct 232 ms 58068 KB Output is correct
12 Correct 250 ms 58064 KB Output is correct
13 Correct 177 ms 68184 KB Output is correct
14 Correct 45 ms 28872 KB Output is correct
15 Correct 190 ms 54088 KB Output is correct
16 Correct 250 ms 46544 KB Output is correct
17 Correct 248 ms 52092 KB Output is correct
18 Correct 264 ms 51620 KB Output is correct
19 Correct 304 ms 62956 KB Output is correct
20 Correct 321 ms 62340 KB Output is correct
21 Correct 130 ms 41936 KB Output is correct
22 Correct 126 ms 42152 KB Output is correct
23 Correct 178 ms 49036 KB Output is correct
24 Correct 210 ms 48692 KB Output is correct
25 Correct 270 ms 56356 KB Output is correct
26 Correct 284 ms 52004 KB Output is correct
27 Correct 270 ms 51984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 28852 KB Output is correct
2 Correct 51 ms 29776 KB Output is correct
3 Correct 55 ms 29680 KB Output is correct
4 Correct 55 ms 29884 KB Output is correct
5 Correct 52 ms 30024 KB Output is correct
6 Correct 45 ms 29496 KB Output is correct
7 Correct 40 ms 28848 KB Output is correct
8 Correct 111 ms 40320 KB Output is correct
9 Correct 110 ms 40276 KB Output is correct
10 Correct 42 ms 28848 KB Output is correct
11 Correct 232 ms 58068 KB Output is correct
12 Correct 250 ms 58064 KB Output is correct
13 Correct 177 ms 68184 KB Output is correct
14 Correct 45 ms 28872 KB Output is correct
15 Correct 190 ms 54088 KB Output is correct
16 Correct 250 ms 46544 KB Output is correct
17 Correct 248 ms 52092 KB Output is correct
18 Correct 264 ms 51620 KB Output is correct
19 Correct 304 ms 62956 KB Output is correct
20 Correct 321 ms 62340 KB Output is correct
21 Correct 130 ms 41936 KB Output is correct
22 Correct 126 ms 42152 KB Output is correct
23 Correct 178 ms 49036 KB Output is correct
24 Correct 210 ms 48692 KB Output is correct
25 Correct 270 ms 56356 KB Output is correct
26 Correct 284 ms 52004 KB Output is correct
27 Correct 270 ms 51984 KB Output is correct
28 Correct 42 ms 29520 KB Output is correct
29 Correct 55 ms 31948 KB Output is correct
30 Correct 50 ms 32048 KB Output is correct
31 Correct 57 ms 32052 KB Output is correct
32 Correct 61 ms 32980 KB Output is correct
33 Correct 50 ms 31672 KB Output is correct
34 Correct 41 ms 29320 KB Output is correct
35 Correct 117 ms 41904 KB Output is correct
36 Correct 101 ms 43552 KB Output is correct
37 Correct 109 ms 44208 KB Output is correct
38 Correct 43 ms 29360 KB Output is correct
39 Correct 254 ms 61528 KB Output is correct
40 Correct 246 ms 74576 KB Output is correct
41 Correct 277 ms 63208 KB Output is correct
42 Correct 281 ms 62928 KB Output is correct
43 Correct 42 ms 29336 KB Output is correct
44 Correct 254 ms 62916 KB Output is correct
45 Correct 252 ms 50616 KB Output is correct
46 Correct 304 ms 57152 KB Output is correct
47 Correct 285 ms 55896 KB Output is correct
48 Correct 403 ms 68944 KB Output is correct
49 Correct 494 ms 73784 KB Output is correct
50 Correct 436 ms 68572 KB Output is correct
51 Correct 129 ms 46588 KB Output is correct
52 Correct 122 ms 45584 KB Output is correct
53 Correct 116 ms 44068 KB Output is correct
54 Correct 112 ms 45072 KB Output is correct
55 Correct 121 ms 45808 KB Output is correct
56 Correct 173 ms 53260 KB Output is correct
57 Correct 277 ms 60304 KB Output is correct
58 Correct 373 ms 63820 KB Output is correct
59 Correct 278 ms 54976 KB Output is correct