Submission #416981

# Submission time Handle Problem Language Result Execution time Memory
416981 2021-06-03T09:26:50 Z aryan12 Inside information (BOI21_servers) C++17
52.5 / 100
419 ms 83764 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 120005;
vector<pair<int, int> > g[N];
int cnt = 0;
int DP[20][N], isDecreasing[20][N], isIncreasing[20][N];
int depth[N];

void dfs(int node, int par) {
	//cout << "node = " << node << "\n";
	DP[0][node] = par;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i].first != par) {
			isDecreasing[0][g[node][i].first] = g[node][i].second;
			isIncreasing[0][g[node][i].first] = g[node][i].second;
			depth[g[node][i].first] = depth[node] + 1;
			dfs(g[node][i].first, node);
		}
	}
}

int LCA(int x, int y) {
	if(depth[x] < depth[y]) {
		swap(x, y);
	}
	int diff = depth[x] - depth[y];
	for(int i = 19; i >= 0; i--) {
		if(diff >= (1 << i)) {
			diff -= (1 << i);
			x = DP[i][x];
		}
	}
	if(x == y)
		return x;
	for(int i = 19; i >= 0; i--) {
		if(DP[i][x] != DP[i][y]) {
			x = DP[i][x];
			y = DP[i][y];
		}
	}
	return DP[0][x];
}

bool Satisfied(int a, int b, int cnt) {
	int lca = LCA(a, b);
	int diff = depth[b] - depth[lca];
	int maxval = -1;
	int lastvalhere = -1;
	int prevval = -1;
	for(int i = 19; i >= 0; i--) {
		if(diff >= (1 << i)) {
			diff -= (1 << i);
			if(isIncreasing[i][b] == -1 || isIncreasing[0][b] <= prevval) {
				return false;
			}
			maxval = max(maxval, isIncreasing[i][b]);
			lastvalhere = max(lastvalhere, isIncreasing[i][b]);
			prevval = isIncreasing[i][b];
			b = DP[i][b];
		}
	}
	diff = depth[a] - depth[b];
	if(diff != 0) {
		maxval = max(maxval, isDecreasing[0][a]);
	}
	//cout << "lastvalhere = " << lastvalhere << "\n";
	prevval = INT_MAX;
	for(int i = 19; i >= 0; i--) {
		if(diff >= (1 << i)) {
			diff -= (1 << i);
			if(isDecreasing[i][a] == -1 || isDecreasing[0][a] >= prevval) {
				return false;
			}
			if(diff == 0) {
				if(lastvalhere >= isDecreasing[i][a]) {
					return false;
				}
			}
			prevval = isDecreasing[i][a];
			a = DP[i][a];
		}
	}
	//cout << "cnt = " << cnt << ", maxval = " << maxval << "\n";
	if(maxval > cnt)
		return false;
	return true;
}

int dfs2(int node, int par, int curVal, int cur) {
	int ans = 1;
	for(int i = 0; i < g[node].size(); i++) {
		if(g[node][i].first != par) {
			if(g[node][i].second <= curVal || g[node][i].second > cur) {
				continue;
			}
			else {
				ans += dfs2(g[node][i].first, node, g[node][i].second, cur);
			}
		}
	}
	return ans;
}

void Solve() {
	int n, q;
	cin >> n >> q;
	q += (n - 1);
	vector<pair<char, pair<int, int> > > queries;
	while(q--) {
		char c;
		cin >> c;
		int a, b;
		if(c == 'S') {
			cin >> a >> b;
			g[a].push_back({b, ++cnt});
			g[b].push_back({a, cnt});
		}
		else if(c == 'Q') {
			cin >> a >> b;
		}
		else {
			cin >> a;
			b = -1;
		}
		queries.push_back({c, {a, b}});
	}
	depth[1] = 0;
	g[1].push_back({-1, 0});
	isIncreasing[0][1] = -1;
	isDecreasing[0][1] = -1;
	dfs(1, -1);
	for(int i = 1; i < 20; i++) {
		for(int j = 1; j <= n; j++) {
			if(DP[i - 1][j] == -1) {
				DP[i][j] = -1;
			}
			else {
				DP[i][j] = DP[i - 1][DP[i - 1][j]];
			}
			if(DP[i][j] == -1) {
				isDecreasing[i][j] = -1;
				isIncreasing[i][j] = -1;
			}
			else {
				if(isDecreasing[i - 1][j] == -1 || isDecreasing[i - 1][DP[i - 1][j]] == -1) {
					isDecreasing[i][j] = -1;
				}
				else if(isDecreasing[i - 1][j] <= isDecreasing[0][DP[i - 1][j]]) {
					isDecreasing[i][j] = -1;
				}
				else {
					isDecreasing[i][j] = isDecreasing[i - 1][DP[i - 1][j]];
				}


				if(isIncreasing[i - 1][j] == -1 || isIncreasing[i - 1][DP[i - 1][j]] == -1) {
					isIncreasing[i][j] = -1;
				}
				else if(isIncreasing[i - 1][j] >= isIncreasing[0][DP[i - 1][j]]) {
					isIncreasing[i][j] = -1;
				}
				else {
					isIncreasing[i][j] = isIncreasing[i - 1][DP[i - 1][j]];
				}
			}
		}
	}
	/*for(int i = 0; i < 20; i++) {
		for(int j = 1; j <= n; j++) {
			cout << isIncreasing[i][j] << " ";
		}
		cout << "\n";
	}
	for(int i = 0; i < 20; i++) {
		for(int j = 1; j <= n; j++) {
			cout << isDecreasing[i][j] << " ";
		}
		cout << "\n";
	}*/
	cnt = 0;
	int val[n + 1];
	for(int i = 0; i <= n; i++) {
		val[i] = -1;
	}
	for(int i = 0; i < queries.size(); i++) {
		char c = queries[i].first;
		int a = queries[i].second.first, b = queries[i].second.second;
		if(c == 'S') {
			cnt++;
			if(a == 1)
				swap(a, b);
			val[a] = cnt - 1;
		}
		else if(c == 'Q') {
			if(Satisfied(a, b, cnt)) {
				cout << "yes\n";
			}
			else {
				cout << "no\n";
			}
		}
		else {
			if(a == 1) {
				cout << cnt + 1 << "\n";
			}
			else {
				if(val[a] == -1) {
					cout << "1\n";
				}
				else {
					cout << cnt - val[a] + 1 << "\n";
				}
			}
		}
	}
}

int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	return 0;
}

Compilation message

servers.cpp: In function 'void dfs(long long int, long long int)':
servers.cpp:14:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   14 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
servers.cpp: In function 'long long int dfs2(long long int, long long int, long long int, long long int)':
servers.cpp:93:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for(int i = 0; i < g[node].size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
servers.cpp: In function 'void Solve()':
servers.cpp:187:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<char, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  187 |  for(int i = 0; i < queries.size(); i++) {
      |                 ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 7664 KB Output is correct
2 Correct 83 ms 10204 KB Output is correct
3 Correct 82 ms 10408 KB Output is correct
4 Correct 80 ms 10296 KB Output is correct
5 Correct 54 ms 10440 KB Output is correct
6 Correct 86 ms 10216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 7664 KB Output is correct
2 Correct 83 ms 10204 KB Output is correct
3 Correct 82 ms 10408 KB Output is correct
4 Correct 80 ms 10296 KB Output is correct
5 Correct 54 ms 10440 KB Output is correct
6 Correct 86 ms 10216 KB Output is correct
7 Incorrect 48 ms 7684 KB Extra information in the output file
8 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 49 ms 7640 KB Output is correct
2 Correct 419 ms 75900 KB Output is correct
3 Correct 417 ms 75860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 7640 KB Output is correct
2 Correct 419 ms 75900 KB Output is correct
3 Correct 417 ms 75860 KB Output is correct
4 Correct 48 ms 7696 KB Output is correct
5 Correct 369 ms 75764 KB Output is correct
6 Correct 106 ms 74992 KB Output is correct
7 Correct 110 ms 75136 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 7736 KB Output is correct
2 Correct 175 ms 83556 KB Output is correct
3 Correct 182 ms 83692 KB Output is correct
4 Correct 198 ms 82788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 7736 KB Output is correct
2 Correct 175 ms 83556 KB Output is correct
3 Correct 182 ms 83692 KB Output is correct
4 Correct 198 ms 82788 KB Output is correct
5 Incorrect 41 ms 7748 KB Extra information in the output file
6 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7620 KB Output is correct
2 Correct 330 ms 77548 KB Output is correct
3 Correct 213 ms 77832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7620 KB Output is correct
2 Correct 330 ms 77548 KB Output is correct
3 Correct 213 ms 77832 KB Output is correct
4 Incorrect 46 ms 7572 KB Extra information in the output file
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 7724 KB Output is correct
2 Correct 180 ms 83576 KB Output is correct
3 Correct 182 ms 83764 KB Output is correct
4 Correct 196 ms 82828 KB Output is correct
5 Correct 46 ms 7732 KB Output is correct
6 Correct 334 ms 77540 KB Output is correct
7 Correct 211 ms 77776 KB Output is correct
8 Correct 374 ms 78564 KB Output is correct
9 Correct 320 ms 78564 KB Output is correct
10 Correct 251 ms 80996 KB Output is correct
11 Correct 375 ms 80428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 43 ms 7724 KB Output is correct
2 Correct 180 ms 83576 KB Output is correct
3 Correct 182 ms 83764 KB Output is correct
4 Correct 196 ms 82828 KB Output is correct
5 Correct 46 ms 7732 KB Output is correct
6 Correct 334 ms 77540 KB Output is correct
7 Correct 211 ms 77776 KB Output is correct
8 Correct 374 ms 78564 KB Output is correct
9 Correct 320 ms 78564 KB Output is correct
10 Correct 251 ms 80996 KB Output is correct
11 Correct 375 ms 80428 KB Output is correct
12 Incorrect 42 ms 7700 KB Extra information in the output file
13 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 51 ms 7600 KB Output is correct
2 Correct 86 ms 10264 KB Output is correct
3 Correct 79 ms 10280 KB Output is correct
4 Correct 81 ms 10300 KB Output is correct
5 Correct 56 ms 10428 KB Output is correct
6 Correct 85 ms 10224 KB Output is correct
7 Correct 49 ms 7676 KB Output is correct
8 Correct 401 ms 76032 KB Output is correct
9 Correct 403 ms 75860 KB Output is correct
10 Correct 41 ms 7604 KB Output is correct
11 Correct 182 ms 83728 KB Output is correct
12 Correct 170 ms 83532 KB Output is correct
13 Correct 187 ms 82788 KB Output is correct
14 Correct 45 ms 7632 KB Output is correct
15 Correct 318 ms 77544 KB Output is correct
16 Correct 212 ms 77796 KB Output is correct
17 Correct 371 ms 78520 KB Output is correct
18 Correct 307 ms 78564 KB Output is correct
19 Correct 247 ms 81088 KB Output is correct
20 Correct 382 ms 80416 KB Output is correct
21 Correct 395 ms 76312 KB Output is correct
22 Correct 399 ms 77392 KB Output is correct
23 Correct 403 ms 77284 KB Output is correct
24 Correct 395 ms 77304 KB Output is correct
25 Correct 300 ms 77764 KB Output is correct
26 Correct 181 ms 76956 KB Output is correct
27 Correct 175 ms 77028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 51 ms 7600 KB Output is correct
2 Correct 86 ms 10264 KB Output is correct
3 Correct 79 ms 10280 KB Output is correct
4 Correct 81 ms 10300 KB Output is correct
5 Correct 56 ms 10428 KB Output is correct
6 Correct 85 ms 10224 KB Output is correct
7 Correct 49 ms 7676 KB Output is correct
8 Correct 401 ms 76032 KB Output is correct
9 Correct 403 ms 75860 KB Output is correct
10 Correct 41 ms 7604 KB Output is correct
11 Correct 182 ms 83728 KB Output is correct
12 Correct 170 ms 83532 KB Output is correct
13 Correct 187 ms 82788 KB Output is correct
14 Correct 45 ms 7632 KB Output is correct
15 Correct 318 ms 77544 KB Output is correct
16 Correct 212 ms 77796 KB Output is correct
17 Correct 371 ms 78520 KB Output is correct
18 Correct 307 ms 78564 KB Output is correct
19 Correct 247 ms 81088 KB Output is correct
20 Correct 382 ms 80416 KB Output is correct
21 Correct 395 ms 76312 KB Output is correct
22 Correct 399 ms 77392 KB Output is correct
23 Correct 403 ms 77284 KB Output is correct
24 Correct 395 ms 77304 KB Output is correct
25 Correct 300 ms 77764 KB Output is correct
26 Correct 181 ms 76956 KB Output is correct
27 Correct 175 ms 77028 KB Output is correct
28 Incorrect 49 ms 7588 KB Extra information in the output file
29 Halted 0 ms 0 KB -