Submission #919478

# Submission time Handle Problem Language Result Execution time Memory
919478 2024-01-31T22:16:54 Z OAleksa Tourism (JOI23_tourism) C++14
34 / 100
5000 ms 24436 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
const int N = 1e5 + 69;
int n, m, q, c[N], ans[N], sz[N], top[N], pos[N], fenw[N];
int node, par[N], dep[N];
vector<int> g[N];
vector<pair<int, int>> qs[N];
set<tuple<int, int, int>> range;
void modify(int v, int val) {
	for (int i = v;i < N;i += (i & -i))
		fenw[i] += val;
}
int Get(int v) {
	int r = 0;
	for (int i = v;i > 0;i -= (i & -i))
		r += fenw[i];
	return r;
}
void dfs(int v, int p) {
	sz[v] = 1;
	par[v] = p;
	dep[v] = dep[p] + 1;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		dfs(u, v);
		sz[v] += sz[u];
	}
}
void hld(int v, int p, int tp) {
	pos[v] = ++node;
	top[v] = tp;
	int s = -1, mx = 0;
	for (auto u : g[v]) {
		if (u == p)
			continue;
		if (sz[u] > mx) {
			mx = sz[u];
			s = u;
		}
	}
	if (s == -1)
		return;
	hld(s, v, tp);
	for (auto u : g[v]) {
		if (u == s || u == p)
			continue;
		hld(u, v, u);
	}
}
void Modify(int l, int r, int c) {
	if (l > r)
		swap(l, r);
	while (l <= r) {
		auto u = range.upper_bound({l, N, N});
		--u;
		int levo, desno, clr;
		tie(levo, desno, clr) = *u;
		assert(l >= levo && l <= desno);
		int cnt = 0;
		range.erase(u);
		if (l > levo) 
			range.insert({levo, l - 1, clr});
		if (desno > r)
			range.insert({r + 1, desno, clr});
		cnt += min(desno, r) - l + 1;
		modify(clr, -cnt);
		modify(c, min(desno, r) - l + 1);
		range.insert({l, min(desno, r), c});
		l = desno + 1;
	}
}
void Change(int a, int b, int c) {
	while (top[a] != top[b]) {
		if (dep[top[a]] < dep[top[b]])
			swap(a, b);
		Modify(pos[top[a]], pos[a], c);
		a = par[top[a]];
	}
	Modify(pos[b], pos[a], c);
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt = 1;
  //cin >> tt;
  while (tt--) {
  	cin >> n >> m >> q;
  	for (int i = 1;i <= n - 1;i++) {
  		int a, b;
  		cin >> a >> b;
  		g[a].push_back(b);
  		g[b].push_back(a);
  	}
  	for (int i = 1;i <= m;i++ )
  		cin >> c[i];
  	for (int i = 1;i <= q;i++) {
  		int l, r;
  		cin >> l >> r;
  		qs[r].push_back({l, i});
  	}
  	dfs(1, 0);
  	hld(1, 1, 1);
  	range.insert({1, n, N});
  	for (int i = 1;i <= m;i++) {
  		if (i > 1) 
  			Change(c[i], c[i - 1], i - 1);
  		for (auto u : qs[i]) {
  			int l, ind;
  			tie(l, ind) = u;
  			if (l == i)
  				ans[ind] = 1;
  			else
  				ans[ind] = Get(N - 1) - Get(l - 1);
  		}
  	}
  	for (int i = 1;i <= q;i++)
  		cout << ans[i] << '\n';
	}
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 11040 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10840 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10840 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10840 KB Output is correct
14 Correct 3 ms 10840 KB Output is correct
15 Correct 4 ms 10840 KB Output is correct
16 Correct 4 ms 10840 KB Output is correct
17 Correct 4 ms 10840 KB Output is correct
18 Correct 4 ms 10840 KB Output is correct
19 Correct 4 ms 10844 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10840 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10840 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 11040 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10840 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10840 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10840 KB Output is correct
14 Correct 3 ms 10840 KB Output is correct
15 Correct 4 ms 10840 KB Output is correct
16 Correct 4 ms 10840 KB Output is correct
17 Correct 4 ms 10840 KB Output is correct
18 Correct 4 ms 10840 KB Output is correct
19 Correct 4 ms 10844 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10840 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10840 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10840 KB Output is correct
30 Correct 5 ms 10844 KB Output is correct
31 Correct 6 ms 10840 KB Output is correct
32 Correct 8 ms 11096 KB Output is correct
33 Correct 8 ms 11096 KB Output is correct
34 Correct 7 ms 11096 KB Output is correct
35 Correct 8 ms 11096 KB Output is correct
36 Correct 9 ms 11352 KB Output is correct
37 Correct 10 ms 11128 KB Output is correct
38 Correct 74 ms 11096 KB Output is correct
39 Correct 77 ms 11164 KB Output is correct
40 Correct 77 ms 11164 KB Output is correct
41 Correct 77 ms 11100 KB Output is correct
42 Correct 78 ms 11184 KB Output is correct
43 Correct 78 ms 11096 KB Output is correct
44 Correct 39 ms 11368 KB Output is correct
45 Correct 34 ms 11100 KB Output is correct
46 Correct 39 ms 11136 KB Output is correct
47 Correct 33 ms 11096 KB Output is correct
48 Correct 31 ms 11092 KB Output is correct
49 Correct 40 ms 11116 KB Output is correct
50 Correct 4 ms 11096 KB Output is correct
51 Correct 4 ms 11100 KB Output is correct
52 Correct 4 ms 11100 KB Output is correct
53 Correct 5 ms 11100 KB Output is correct
54 Correct 4 ms 11096 KB Output is correct
55 Correct 5 ms 11104 KB Output is correct
56 Correct 3 ms 10840 KB Output is correct
57 Correct 3 ms 10848 KB Output is correct
58 Correct 8 ms 11044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10844 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Execution timed out 5043 ms 19724 KB Time limit exceeded
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 266 ms 16076 KB Output is correct
3 Correct 428 ms 16856 KB Output is correct
4 Correct 335 ms 17300 KB Output is correct
5 Correct 543 ms 20464 KB Output is correct
6 Correct 543 ms 20352 KB Output is correct
7 Correct 597 ms 20456 KB Output is correct
8 Correct 510 ms 20236 KB Output is correct
9 Correct 555 ms 20276 KB Output is correct
10 Correct 508 ms 20260 KB Output is correct
11 Correct 510 ms 20572 KB Output is correct
12 Correct 539 ms 20560 KB Output is correct
13 Correct 563 ms 20748 KB Output is correct
14 Correct 521 ms 21332 KB Output is correct
15 Correct 560 ms 23900 KB Output is correct
16 Correct 523 ms 20696 KB Output is correct
17 Correct 517 ms 20816 KB Output is correct
18 Correct 612 ms 20748 KB Output is correct
19 Execution timed out 5047 ms 18380 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10840 KB Output is correct
3 Correct 4 ms 10840 KB Output is correct
4 Correct 605 ms 19884 KB Output is correct
5 Correct 631 ms 20056 KB Output is correct
6 Correct 730 ms 22748 KB Output is correct
7 Correct 781 ms 24436 KB Output is correct
8 Correct 776 ms 24212 KB Output is correct
9 Correct 773 ms 24280 KB Output is correct
10 Correct 780 ms 24372 KB Output is correct
11 Correct 792 ms 24260 KB Output is correct
12 Correct 799 ms 24304 KB Output is correct
13 Correct 766 ms 24272 KB Output is correct
14 Correct 37 ms 14020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 10840 KB Output is correct
2 Correct 2 ms 10844 KB Output is correct
3 Correct 2 ms 10840 KB Output is correct
4 Correct 2 ms 11040 KB Output is correct
5 Correct 2 ms 10840 KB Output is correct
6 Correct 2 ms 10840 KB Output is correct
7 Correct 2 ms 10840 KB Output is correct
8 Correct 2 ms 10840 KB Output is correct
9 Correct 2 ms 10840 KB Output is correct
10 Correct 2 ms 10844 KB Output is correct
11 Correct 3 ms 10840 KB Output is correct
12 Correct 3 ms 10844 KB Output is correct
13 Correct 3 ms 10840 KB Output is correct
14 Correct 3 ms 10840 KB Output is correct
15 Correct 4 ms 10840 KB Output is correct
16 Correct 4 ms 10840 KB Output is correct
17 Correct 4 ms 10840 KB Output is correct
18 Correct 4 ms 10840 KB Output is correct
19 Correct 4 ms 10844 KB Output is correct
20 Correct 3 ms 10840 KB Output is correct
21 Correct 2 ms 10840 KB Output is correct
22 Correct 2 ms 10844 KB Output is correct
23 Correct 2 ms 10840 KB Output is correct
24 Correct 2 ms 10844 KB Output is correct
25 Correct 2 ms 10840 KB Output is correct
26 Correct 2 ms 10844 KB Output is correct
27 Correct 2 ms 10840 KB Output is correct
28 Correct 2 ms 10844 KB Output is correct
29 Correct 2 ms 10840 KB Output is correct
30 Correct 5 ms 10844 KB Output is correct
31 Correct 6 ms 10840 KB Output is correct
32 Correct 8 ms 11096 KB Output is correct
33 Correct 8 ms 11096 KB Output is correct
34 Correct 7 ms 11096 KB Output is correct
35 Correct 8 ms 11096 KB Output is correct
36 Correct 9 ms 11352 KB Output is correct
37 Correct 10 ms 11128 KB Output is correct
38 Correct 74 ms 11096 KB Output is correct
39 Correct 77 ms 11164 KB Output is correct
40 Correct 77 ms 11164 KB Output is correct
41 Correct 77 ms 11100 KB Output is correct
42 Correct 78 ms 11184 KB Output is correct
43 Correct 78 ms 11096 KB Output is correct
44 Correct 39 ms 11368 KB Output is correct
45 Correct 34 ms 11100 KB Output is correct
46 Correct 39 ms 11136 KB Output is correct
47 Correct 33 ms 11096 KB Output is correct
48 Correct 31 ms 11092 KB Output is correct
49 Correct 40 ms 11116 KB Output is correct
50 Correct 4 ms 11096 KB Output is correct
51 Correct 4 ms 11100 KB Output is correct
52 Correct 4 ms 11100 KB Output is correct
53 Correct 5 ms 11100 KB Output is correct
54 Correct 4 ms 11096 KB Output is correct
55 Correct 5 ms 11104 KB Output is correct
56 Correct 3 ms 10840 KB Output is correct
57 Correct 3 ms 10848 KB Output is correct
58 Correct 8 ms 11044 KB Output is correct
59 Correct 2 ms 10844 KB Output is correct
60 Correct 2 ms 10844 KB Output is correct
61 Correct 2 ms 10840 KB Output is correct
62 Execution timed out 5043 ms 19724 KB Time limit exceeded
63 Halted 0 ms 0 KB -