Submission #878206

# Submission time Handle Problem Language Result Execution time Memory
878206 2023-11-24T07:17:07 Z TAhmed33 Tourism (JOI23_tourism) C++
18 / 100
114 ms 111776 KB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
const int MAXN = 4e5 + 25;
int in[MAXN], out[MAXN], depth[MAXN];
pair <int, int> sparse[__lg(MAXN)][MAXN];
vector <int> adj[MAXN];
int n, m, q;
int cnt = 0;
void dfs (int pos, int par, int dep = 1) {
	depth[pos] = dep;
	sparse[0][++cnt] = {dep, pos};
	in[pos] = cnt;
	for (auto j : adj[pos]) {
		if (j != par) {
			dfs(j, pos, dep + 1);
			sparse[0][++cnt] = {dep, pos};
		}
	}
	out[pos] = dep;
}
int sparse2[__lg(MAXN)][MAXN];
int query (int l, int r) {
	if (l > r) swap(l, r);
	int x = __lg(r - l + 1);
	return min(sparse[x][l], sparse[x][r - (1 << x) + 1]).second;
}
int query2 (int l, int r) {
	int x = __lg(r - l + 1);
	return query(in[sparse2[x][l]], in[sparse2[x][r - (1 << x) + 1]]);
}
int arr[MAXN];
array <int, 3> queries[MAXN];
int ret[MAXN];
int ans = 0;
set <int> dd;
void add (int x) {
	if (dd.empty()) {
		dd.insert(in[x]);
		ans = depth[x];
		return;
	}
    ans += depth[x];
	auto t = dd.upper_bound(in[x]);
	int r = (t == dd.end() ? -1 : *t);
	int l = -1;
	if (t != dd.begin()) l = *(--t);
	if (r != -1) {
		if (l != -1) ans += depth[query(l, r)];
		ans -= depth[query(in[x], r)];
	}
	if (l != -1) ans -= depth[query(l, in[x])];
	dd.insert(in[x]);
}
void rem (int x) {
	dd.erase(in[x]);
	if (dd.empty()) {
		ans = 0; return;
	}
    ans -= depth[x];
	auto t = dd.upper_bound(in[x]);
	int r = (t == dd.end() ? -1 : *t);
	int l = -1;
	if (t != dd.begin()) l = *(--t);
	if (r != -1) {
        if (l != -1) ans -= depth[query(l, r)];
		ans += depth[query(in[x], r)];
	}
	if (l != -1) ans += depth[query(l, in[x])];
}
int main () {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> m >> q;
	for (int i = 1; i < n; i++) {
		int a, b;
		cin >> a >> b;
		adj[a].push_back(b);
		adj[b].push_back(a);
	}
	dfs(1, -1);
	for (int i = 1; i <= __lg(cnt); i++) {
		for (int j = 1; j + (1 << i) - 1 <= cnt; j++) {
			sparse[i][j] = min(sparse[i - 1][j], sparse[i - 1][j + (1 << (i - 1))]);
		}
	}
	for (int i = 1; i <= m; i++) {
		cin >> arr[i];
		sparse2[0][i] = arr[i];
	}
	for (int i = 1; i <= __lg(m); i++) {
		for (int j = 1; j + (1 << i) - 1 <= m; j++) {
			sparse2[i][j] = query(in[sparse2[i - 1][j]], in[sparse2[i - 1][j + (1 << (i - 1))]]);
		}
	}
	/*for (int i = 1; i <= q; i++) {
		int l, r;
		cin >> l >> r;
		vector <int> g;
		for (int j = l; j <= r; j++) g.push_back(arr[j]);
		sort(g.begin(), g.end(), [&] (int a, int b) {
			return in[a] < in[b];
		});
		int ans = depth[g[0]], lca = g[0];
		for (int j = 1; j < (int)g.size(); j++) {
			ans += depth[g[j]] - depth[query(in[g[j]], in[g[j - 1]])];
			lca = query(in[lca], in[g[j]]);
		}
		ans -= depth[lca]; ans++;
		cout << ans << '\n';
	}*/
	for (int i = 1; i <= q; i++) {
		cin >> queries[i][0] >> queries[i][1]; queries[i][2] = i;
	}
	int x = sqrt(m); while (x * x < m) x++; while (x * x > m) x--;
	sort(queries + 1, queries + q + 1, [&] (array <int, 3> &a, array <int, 3> &b) {
		return a[0] / x == b[0] / x ? a[1] < b[1] : a[0] / x < b[0] / x;
	});
	int l = queries[1][0], r = queries[1][1];
    for (int i = l; i <= r; i++) add(arr[i]);
    ret[queries[1][2]] = ans - depth[query2(l, r)] + 1;
	for (int i = 2; i <= q; i++) {
		int tl = queries[i][0], tr = queries[i][1];
		while (l < tl) rem(arr[l++]);
		while (l > tl) add(arr[--l]);
		while (r < tr) add(arr[++r]);
		while (r > tr) rem(arr[r--]);
		ret[queries[i][2]] = ans - depth[query2(l, r)] + 1;
	}
	for (int i = 1; i <= q; i++) cout << ret[i] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 33112 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Incorrect 5 ms 35164 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 33112 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Incorrect 5 ms 35164 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 35416 KB Output is correct
2 Incorrect 5 ms 35164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 33116 KB Output is correct
2 Correct 56 ms 97372 KB Output is correct
3 Correct 76 ms 100264 KB Output is correct
4 Correct 64 ms 102480 KB Output is correct
5 Correct 97 ms 109168 KB Output is correct
6 Correct 114 ms 107752 KB Output is correct
7 Correct 111 ms 107060 KB Output is correct
8 Correct 103 ms 106324 KB Output is correct
9 Correct 104 ms 108512 KB Output is correct
10 Correct 100 ms 108372 KB Output is correct
11 Correct 103 ms 108528 KB Output is correct
12 Correct 90 ms 108372 KB Output is correct
13 Correct 87 ms 108580 KB Output is correct
14 Correct 84 ms 108944 KB Output is correct
15 Correct 103 ms 110364 KB Output is correct
16 Correct 106 ms 110436 KB Output is correct
17 Correct 103 ms 110160 KB Output is correct
18 Correct 100 ms 110160 KB Output is correct
19 Correct 86 ms 111192 KB Output is correct
20 Correct 99 ms 110124 KB Output is correct
21 Correct 103 ms 109176 KB Output is correct
22 Correct 101 ms 108816 KB Output is correct
23 Correct 98 ms 108372 KB Output is correct
24 Correct 106 ms 108628 KB Output is correct
25 Correct 94 ms 108372 KB Output is correct
26 Correct 93 ms 108368 KB Output is correct
27 Correct 94 ms 108384 KB Output is correct
28 Correct 91 ms 108376 KB Output is correct
29 Correct 87 ms 108368 KB Output is correct
30 Correct 87 ms 108660 KB Output is correct
31 Correct 84 ms 108380 KB Output is correct
32 Correct 79 ms 108736 KB Output is correct
33 Correct 92 ms 109396 KB Output is correct
34 Correct 98 ms 111776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 33112 KB Output is correct
2 Incorrect 5 ms 35164 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 5 ms 33112 KB Output is correct
2 Correct 5 ms 35164 KB Output is correct
3 Incorrect 5 ms 35164 KB Output isn't correct
4 Halted 0 ms 0 KB -