답안 #873764

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873764 2023-11-15T17:05:17 Z MinaRagy06 Tourism (JOI23_tourism) C++17
0 / 100
308 ms 91724 KB
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)

int lazy[18][1 << 18];
void inc(int lvl, int i, int l, int r, int s, int e) {
	if (s <= l && r <= e) {
		lazy[lvl][i]++;
		return;
	}
	if (r < s || e < l) {
		return;
	}
	inc(lvl, i << 1, l, md, s, e);
	inc(lvl, i << 1 | 1, md + 1, r, s, e);
}
int get(int lvl, int i, int l, int r, int x) {
	if (l == r) {
		return lazy[lvl][i];
	}
	if (x <= md) {
		return get(lvl, i << 1, l, md, x) + lazy[lvl][i];
	} else {
		return get(lvl, i << 1 | 1, md + 1, r, x) + lazy[lvl][i];
	}
}
const int N = 100'005;
int anc[18][N], h[N], height;
vector<int> adj[N];
void dfs(int i, int d, int p) {
	h[i] = d;
	height = max(height, d);
	anc[0][i] = p;
	for (int j = 1; j < 18; j++) {
		anc[j][i] = anc[j - 1][anc[j - 1][i]];
	}
	for (auto nxt : adj[i]) {
		if (nxt == p) continue;
		dfs(nxt, d + 1, i);
	}
}
int lst[18][N], sparse[18][N];
int getlca(int u, int v) {
	if (h[u] > h[v]) {
		swap(u, v);
	}
	if (v == 0) return u;
	if (h[u] != h[v]) {
		for (int j = 17; j >= 0; j--) {
			if (h[anc[j][v]] > h[u]) {
				v = anc[j][v];
			}
		}
		v = anc[0][v];
	}
	if (u != v) {
		for (int j = 17; j >= 0; j--) {
			if (anc[j][u] != anc[j][v]) {
				u = anc[j][u];
				v = anc[j][v];
			}
		}
		u = anc[0][u];
	}
	return u;
}
int query(int l, int r) {
	int lg = __lg(r - l + 1);
	return getlca(sparse[lg][l], sparse[lg][r - (1 << lg) + 1]);
}
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n, m, q;
	cin >> n >> m >> q;
	for (int i = 1, u, v; i < n; i++) {
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	dfs(1, 0, 1);
	int a[m];
	for (int i = 0; i < m; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < m; i++) {
		sparse[0][i] = a[i];
	}
	for (int j = 1; j < 18; j++) {
		for (int i = 0; i < m; i++) {
			sparse[j][i] = getlca(sparse[j - 1][i], sparse[j - 1][i + (1 << (j - 1))]);
		}
	}
	vector<array<int, 2>> ask[m];
	for (int i = 0; i < q; i++) {
		int l, r;
		cin >> l >> r;
		l--, r--;
		ask[r].push_back({l, i});
	}
	memset(lst, -1, sizeof lst);
	int ans[q]{};
	for (int r = 0; r < m; r++) {
		for (int j = h[a[r]]; j >= 0; j--) {
			inc(j, 1, 0, n - 1, lst[j][a[r]] + 1, r);
			lst[j][a[r]] = r;
			a[r] = anc[0][a[r]];
		}
		for (auto [l, idx] : ask[r]) {
			int lca = query(l, r);
			int mn = h[lca];
			for (int j = height; j >= mn; j--) {
				int v = get(j, 1, 0, n - 1, l);
				ans[idx] += v;
			}
		}
	}
	for (auto i : ans) {
		cout << i << '\n';
	}
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 26204 KB Output is correct
2 Correct 6 ms 32492 KB Output is correct
3 Correct 4 ms 28252 KB Output is correct
4 Incorrect 8 ms 38748 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 26204 KB Output is correct
2 Correct 6 ms 32492 KB Output is correct
3 Correct 4 ms 28252 KB Output is correct
4 Incorrect 8 ms 38748 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 32352 KB Output is correct
2 Correct 4 ms 24408 KB Output is correct
3 Correct 4 ms 24412 KB Output is correct
4 Runtime error 165 ms 65024 KB Execution killed with signal 11
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26200 KB Output is correct
2 Runtime error 103 ms 91724 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 26332 KB Output is correct
2 Correct 4 ms 24400 KB Output is correct
3 Correct 4 ms 24412 KB Output is correct
4 Incorrect 308 ms 50860 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 26204 KB Output is correct
2 Correct 6 ms 32492 KB Output is correct
3 Correct 4 ms 28252 KB Output is correct
4 Incorrect 8 ms 38748 KB Output isn't correct
5 Halted 0 ms 0 KB -