Submission #659810

# Submission time Handle Problem Language Result Execution time Memory
659810 2022-11-19T09:56:55 Z NothingXD Inside information (BOI21_servers) C++17
100 / 100
537 ms 44468 KB
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
//typedef __uint128_t L;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << H;
	debug_out(T...);
}

#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define MP(x, y) make_pair(x, y)
#define F first
#define S second

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int maxn = 3e5 + 10;

int n, q, N, f[maxn], sz[maxn], ans[maxn];
pii mn[maxn], mx[maxn];
vector<pii> g[maxn];
bool vis[maxn], mark[maxn], flg[maxn];
vector<int> going, tmp;
vector<pii> Q1[maxn];
vector<int> Q2[maxn];

void add(int idx, int x){
	idx++;
	for (; idx <= N+1; idx += idx & -idx) f[idx] += x;
}

int get(int idx){
	idx++;
	int res = 0;
	for (; idx; idx -= idx & -idx) res += f[idx];
	return res;
}

void dfs(int v, int p = -1){
	going.push_back(v);
	mn[v] = {N, 0};
	mx[v] = {0, 0};
	sz[v] = 1;
	for (auto [u, w]: g[v]){
		if (u != p && !vis[u]){
			dfs(u, v);
			sz[v] += sz[u];
		}
	}
}

int getcen(int v, int n, int p = -1){
	for (auto [u, w]: g[v]){
		if (!vis[u] && u != p && sz[u] * 2 > n) return getcen(u, n, v);
	}
	return v;
}

void getmn(int v, pii W, int p = -1){
	mn[v] = W;
	for (auto [u, w]: g[v]){
		if (!vis[u] && u != p && w < W.S) getmn(u, {W.F, w}, v);
	}
}

void getmx(int v, pii W, int p = -1){
	mx[v] = W;
	for (auto [u, w]: g[v]){
		if (!vis[u] && u != p && w > W.S) getmx(u, {W.F, w}, v); 
	}
}

bool cmp(int x, int y){
	return mx[x].F > mx[y].F;
}

bool cmp2(int x, int y){
	return mn[x].F > mn[y].F;
}

void solve(int v){
//	debug(v);
	going.clear();
	dfs(v);
	for (auto x: going) flg[x] = true;
	v = getcen(v, sz[v]);
	vis[v] = true;
	mx[v] = {N, 0};
	mn[v] = {0, 0};
//	debug(v);
	for (auto [u, w]: g[v]){
		if (vis[u]) continue;
		getmn(u, {w, w});
		getmx(u, {w, w});
	}
/*	for (auto u: going){
		debug(u, mn[u].F, mn[u].S, mx[u].F, mx[u].S);
	}*/
	for (auto u: going){
		for (auto [t, x]: Q1[u]){
			if (!flg[x]) continue;
			if (mn[u].F > t) continue;
			if (mn[u].F < mx[x].F &&  mx[x].S <= t){
				ans[t] = 1;
			}
		}
	}
	tmp = going;
	sort(all(going), cmp);
	sort(all(tmp), cmp2);
	int ptr = 0;
	for (int i = 0; i < tmp.size(); i++){
		int x = tmp[i];
		while(ptr != going.size() && mx[going[ptr]].F > mn[x].F){
	//		debug(going[ptr], mx[going[ptr]].S);
			add(mx[going[ptr]].S, 1);
			ptr++;
		}
		for (auto t: Q2[x]){
			if (mn[x].F > t) continue;
	//		debug(t, get(t));
			ans[t] += get(t);
		}
	}
	for (int i = 0; i < ptr; i++){
		add(mx[going[i]].S, -1);
	}
	for (auto x: going) flg[x] = false;
	for (auto [u, w]: g[v]){
		if (!vis[u]) solve(u);
	}
}

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

	memset(ans, -1, sizeof ans);

	cin >> n >> q;
	N = n+q;

	for (int i = 1; i < N; i++){
		char t; cin >> t;
		if (t == 'S'){
			int u, v; cin >> u >> v;
			g[u].push_back({v, i});
			g[v].push_back({u, i});
		}
		else if (t == 'Q'){
			int x, y; cin >> x >> y;
			Q1[y].push_back({i, x});
			ans[i] = 0;
			mark[i] = true;
		}
		else{
			int x; cin >> x;
			Q2[x].push_back(i);
			ans[i] = 0;
		}
	}


	solve(1);

	int cnt = 0;

	for (int i = 1; i < N; i++){
		if (ans[i] == -1) continue;
		cnt++;
		assert(cnt <= q);
		if (mark[i]){
			cout << (ans[i]? "yes": "no");
		}
		else{
			cout << ans[i];
		}
		cout << "\n";
	}

	return 0;
}

Compilation message

servers.cpp: In function 'void solve(int)':
servers.cpp:122:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  122 |  for (int i = 0; i < tmp.size(); i++){
      |                  ~~^~~~~~~~~~~~
servers.cpp:124:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |   while(ptr != going.size() && mx[going[ptr]].F > mn[x].F){
      |         ~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25044 KB Output is correct
2 Correct 42 ms 25552 KB Output is correct
3 Correct 43 ms 25264 KB Output is correct
4 Correct 49 ms 25648 KB Output is correct
5 Correct 44 ms 25420 KB Output is correct
6 Correct 39 ms 25288 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25044 KB Output is correct
2 Correct 42 ms 25552 KB Output is correct
3 Correct 43 ms 25264 KB Output is correct
4 Correct 49 ms 25648 KB Output is correct
5 Correct 44 ms 25420 KB Output is correct
6 Correct 39 ms 25288 KB Output is correct
7 Correct 32 ms 25048 KB Output is correct
8 Correct 49 ms 25148 KB Output is correct
9 Correct 39 ms 25040 KB Output is correct
10 Correct 47 ms 25216 KB Output is correct
11 Correct 53 ms 25020 KB Output is correct
12 Correct 39 ms 24780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 25052 KB Output is correct
2 Correct 160 ms 34748 KB Output is correct
3 Correct 173 ms 34748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 25052 KB Output is correct
2 Correct 160 ms 34748 KB Output is correct
3 Correct 173 ms 34748 KB Output is correct
4 Correct 32 ms 25044 KB Output is correct
5 Correct 159 ms 35132 KB Output is correct
6 Correct 132 ms 33556 KB Output is correct
7 Correct 122 ms 33732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 25004 KB Output is correct
2 Correct 339 ms 43540 KB Output is correct
3 Correct 335 ms 43452 KB Output is correct
4 Correct 283 ms 43384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 25004 KB Output is correct
2 Correct 339 ms 43540 KB Output is correct
3 Correct 335 ms 43452 KB Output is correct
4 Correct 283 ms 43384 KB Output is correct
5 Correct 42 ms 24956 KB Output is correct
6 Correct 354 ms 44152 KB Output is correct
7 Correct 305 ms 44056 KB Output is correct
8 Correct 334 ms 43652 KB Output is correct
9 Correct 340 ms 43564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 25044 KB Output is correct
2 Correct 269 ms 34888 KB Output is correct
3 Correct 297 ms 35016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 25044 KB Output is correct
2 Correct 269 ms 34888 KB Output is correct
3 Correct 297 ms 35016 KB Output is correct
4 Correct 32 ms 25036 KB Output is correct
5 Correct 292 ms 35548 KB Output is correct
6 Correct 317 ms 35652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 25004 KB Output is correct
2 Correct 327 ms 43528 KB Output is correct
3 Correct 345 ms 43416 KB Output is correct
4 Correct 336 ms 43244 KB Output is correct
5 Correct 35 ms 25036 KB Output is correct
6 Correct 281 ms 34804 KB Output is correct
7 Correct 282 ms 35072 KB Output is correct
8 Correct 334 ms 35468 KB Output is correct
9 Correct 364 ms 35700 KB Output is correct
10 Correct 397 ms 39392 KB Output is correct
11 Correct 382 ms 37732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 25004 KB Output is correct
2 Correct 327 ms 43528 KB Output is correct
3 Correct 345 ms 43416 KB Output is correct
4 Correct 336 ms 43244 KB Output is correct
5 Correct 35 ms 25036 KB Output is correct
6 Correct 281 ms 34804 KB Output is correct
7 Correct 282 ms 35072 KB Output is correct
8 Correct 334 ms 35468 KB Output is correct
9 Correct 364 ms 35700 KB Output is correct
10 Correct 397 ms 39392 KB Output is correct
11 Correct 382 ms 37732 KB Output is correct
12 Correct 31 ms 25036 KB Output is correct
13 Correct 344 ms 44296 KB Output is correct
14 Correct 316 ms 44468 KB Output is correct
15 Correct 330 ms 43888 KB Output is correct
16 Correct 360 ms 43848 KB Output is correct
17 Correct 32 ms 25548 KB Output is correct
18 Correct 297 ms 35840 KB Output is correct
19 Correct 305 ms 35756 KB Output is correct
20 Correct 327 ms 36464 KB Output is correct
21 Correct 331 ms 36676 KB Output is correct
22 Correct 390 ms 38928 KB Output is correct
23 Correct 537 ms 39816 KB Output is correct
24 Correct 506 ms 39092 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25044 KB Output is correct
2 Correct 43 ms 25460 KB Output is correct
3 Correct 39 ms 25288 KB Output is correct
4 Correct 45 ms 25568 KB Output is correct
5 Correct 46 ms 25420 KB Output is correct
6 Correct 39 ms 25208 KB Output is correct
7 Correct 30 ms 24992 KB Output is correct
8 Correct 171 ms 34740 KB Output is correct
9 Correct 161 ms 34684 KB Output is correct
10 Correct 31 ms 25008 KB Output is correct
11 Correct 331 ms 43460 KB Output is correct
12 Correct 341 ms 43460 KB Output is correct
13 Correct 288 ms 43328 KB Output is correct
14 Correct 33 ms 25036 KB Output is correct
15 Correct 269 ms 34824 KB Output is correct
16 Correct 290 ms 35044 KB Output is correct
17 Correct 290 ms 35368 KB Output is correct
18 Correct 298 ms 35656 KB Output is correct
19 Correct 401 ms 39468 KB Output is correct
20 Correct 452 ms 37800 KB Output is correct
21 Correct 201 ms 34868 KB Output is correct
22 Correct 180 ms 34800 KB Output is correct
23 Correct 287 ms 35268 KB Output is correct
24 Correct 256 ms 35272 KB Output is correct
25 Correct 431 ms 39124 KB Output is correct
26 Correct 320 ms 33988 KB Output is correct
27 Correct 325 ms 33936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 25044 KB Output is correct
2 Correct 43 ms 25460 KB Output is correct
3 Correct 39 ms 25288 KB Output is correct
4 Correct 45 ms 25568 KB Output is correct
5 Correct 46 ms 25420 KB Output is correct
6 Correct 39 ms 25208 KB Output is correct
7 Correct 30 ms 24992 KB Output is correct
8 Correct 171 ms 34740 KB Output is correct
9 Correct 161 ms 34684 KB Output is correct
10 Correct 31 ms 25008 KB Output is correct
11 Correct 331 ms 43460 KB Output is correct
12 Correct 341 ms 43460 KB Output is correct
13 Correct 288 ms 43328 KB Output is correct
14 Correct 33 ms 25036 KB Output is correct
15 Correct 269 ms 34824 KB Output is correct
16 Correct 290 ms 35044 KB Output is correct
17 Correct 290 ms 35368 KB Output is correct
18 Correct 298 ms 35656 KB Output is correct
19 Correct 401 ms 39468 KB Output is correct
20 Correct 452 ms 37800 KB Output is correct
21 Correct 201 ms 34868 KB Output is correct
22 Correct 180 ms 34800 KB Output is correct
23 Correct 287 ms 35268 KB Output is correct
24 Correct 256 ms 35272 KB Output is correct
25 Correct 431 ms 39124 KB Output is correct
26 Correct 320 ms 33988 KB Output is correct
27 Correct 325 ms 33936 KB Output is correct
28 Correct 51 ms 25036 KB Output is correct
29 Correct 49 ms 25628 KB Output is correct
30 Correct 42 ms 25468 KB Output is correct
31 Correct 58 ms 25672 KB Output is correct
32 Correct 53 ms 25544 KB Output is correct
33 Correct 45 ms 25360 KB Output is correct
34 Correct 33 ms 25504 KB Output is correct
35 Correct 239 ms 35004 KB Output is correct
36 Correct 142 ms 33460 KB Output is correct
37 Correct 167 ms 33612 KB Output is correct
38 Correct 35 ms 25472 KB Output is correct
39 Correct 395 ms 44404 KB Output is correct
40 Correct 327 ms 44348 KB Output is correct
41 Correct 364 ms 43776 KB Output is correct
42 Correct 358 ms 43876 KB Output is correct
43 Correct 48 ms 25484 KB Output is correct
44 Correct 319 ms 35900 KB Output is correct
45 Correct 348 ms 35852 KB Output is correct
46 Correct 335 ms 36328 KB Output is correct
47 Correct 341 ms 36732 KB Output is correct
48 Correct 481 ms 38876 KB Output is correct
49 Correct 521 ms 39816 KB Output is correct
50 Correct 479 ms 39036 KB Output is correct
51 Correct 145 ms 33952 KB Output is correct
52 Correct 143 ms 33840 KB Output is correct
53 Correct 140 ms 33388 KB Output is correct
54 Correct 147 ms 33584 KB Output is correct
55 Correct 131 ms 33640 KB Output is correct
56 Correct 234 ms 35144 KB Output is correct
57 Correct 297 ms 37824 KB Output is correct
58 Correct 372 ms 35480 KB Output is correct
59 Correct 281 ms 34544 KB Output is correct