답안 #1087113

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087113 2024-09-12T08:28:04 Z quangminh412 경주 (Race) (IOI11_race) C++14
21 / 100
3000 ms 23120 KB
#include <bits/stdc++.h>
using namespace std;

/*
  John Watson
  https://codeforces.com/profile/quangminh98

  Mua Code nhu mua Florentino !!
*/

#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define ll long long

const int maxn = 2e5 + 9;

vector<int> adj[maxn];
int sz[maxn], par[20][maxn], h[maxn], tp[maxn], id[maxn];
ll val[maxn];
int n;
ll k;

//
ll st[4 * maxn];
void update(int head, int l, int r, int pos, ll val)
{
	if (l == r)
	{
		st[head] = val;
		return;
	}
	int mid = l + r >> 1;
	if (pos <= mid)
		update(2 * head, l, mid, pos, val);
	else
		update(2 * head + 1, mid + 1, r, pos, val);
		
	st[head] = st[2 * head] + st[2 * head + 1];
}
void update(int pos, ll val) { update(1, 1, n, pos, val); }

ll query(int head, int l, int r, int u, int v) 
{
	if (l > v || r < u) return 0ll;
	if (u <= l && r <= v) return st[head];
	
	int mid = l + r >> 1;
	return query(2 * head, l, mid, u, v) + query(2 * head + 1, mid + 1, r, u, v);
}
ll query(int u, int v) { return query(1, 1, n, u, v); } 
//

void DFS(int u, int p = -1)
{
	sz[u] = 1;
	
	for (int v : adj[u])
	{
		if (v == p) continue;
		
		h[v] = h[u] + 1;
		par[0][v] = u;
		for (int i = 1; i < 20; i++)
			par[i][v] = par[i - 1][par[i - 1][v]];
			
		DFS(v, u);
		
		sz[u] += sz[v];
	}
}

int LCA(int u, int v)
{
	if (h[u] < h[v]) swap(u, v);
	int k = h[u] - h[v];
	
	for (int i = 0; i < 20; i++)
		if (k >> i & 1)
			u = par[i][u];
			
	if (u == v) return u;
	
	for (int i = 19; i >= 0; i--)
		if (par[i][u] != par[i][v])
		{
			u = par[i][u];
			v = par[i][v];
		}
	
	return par[0][u];
}

int timeDFS = 0;
void HLD(int u, int p, int head)
{
	id[u] = ++timeDFS;
	tp[u] = head;
	update(id[u], val[u]);
	
	int nxt = -1;
	for (int v : adj[u])
	{
		if (v == p) continue;
		
		if (nxt == -1 || sz[nxt] < sz[v])
			nxt = v;
	}
	
	if (nxt != -1) HLD(nxt, u, head);
	
	for (int v : adj[u])
	{
		if (v == p || v == nxt) continue;
		
		HLD(v, u, v);
	}
}

ll queryPath(int u, int p)
{
	ll ans = 0;
	while (u != p)
		if (tp[u] == u)
		{
			ans += query(id[u], id[u]);
			u = par[0][u];
		} else
		if (h[tp[u]] > h[p])
		{
			ans += query(id[tp[u]], id[u]);
			u = par[0][tp[u]];
		} else
		{
			ans += query(id[p] + 1, id[u]);
			break;
		}
	return ans;
}

int best_path(int _n, int _k, int H[][2], int l[]) 
{
	n = _n;
	k = _k;
	for (int i = 0; i < n - 1; i++)
	{
		int u = H[i][0], v = H[i][1];
		u++; v++;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	
	DFS(1);
	for (int i = 0; i < n - 1; i++)
	{
		ll w = l[i];
		int u = H[i][0], v = H[i][1];
		u++; v++;
		val[h[u] > h[v] ? u : v] = w;
	}
	HLD(1, -1, 1);
	
	int ans = 1e9;
	for (int u = 1; u <= n; u++)
		for (int v = u + 1; v <= n; v++)
		{
			int lca = LCA(u, v);
			ll path = queryPath(u, lca) + queryPath(v, lca);
			if (path == k)
        ans = min(ans, h[u] + h[v] - 2 * h[lca]);
		}
  // for (int i = 1; i <= n; i++) cout << val[i] << ' '; cout << '\n';
	return (ans == 1e9 ? -1 : ans);
}

Compilation message

race.cpp: In function 'void update(int, int, int, int, long long int)':
race.cpp:31:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   31 |  int mid = l + r >> 1;
      |            ~~^~~
race.cpp: In function 'long long int query(int, int, int, int, int)':
race.cpp:46:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   46 |  int mid = l + r >> 1;
      |            ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5144 KB Output is correct
3 Correct 3 ms 5140 KB Output is correct
4 Correct 3 ms 5208 KB Output is correct
5 Correct 2 ms 5180 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5112 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5240 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5208 KB Output is correct
13 Correct 3 ms 5140 KB Output is correct
14 Correct 3 ms 5208 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5144 KB Output is correct
3 Correct 3 ms 5140 KB Output is correct
4 Correct 3 ms 5208 KB Output is correct
5 Correct 2 ms 5180 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5112 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5240 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5208 KB Output is correct
13 Correct 3 ms 5140 KB Output is correct
14 Correct 3 ms 5208 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 147 ms 5404 KB Output is correct
22 Correct 182 ms 5468 KB Output is correct
23 Correct 179 ms 5464 KB Output is correct
24 Correct 152 ms 5212 KB Output is correct
25 Correct 147 ms 5468 KB Output is correct
26 Correct 153 ms 5468 KB Output is correct
27 Correct 144 ms 5468 KB Output is correct
28 Correct 151 ms 5468 KB Output is correct
29 Correct 150 ms 5212 KB Output is correct
30 Correct 155 ms 5464 KB Output is correct
31 Correct 157 ms 5468 KB Output is correct
32 Correct 148 ms 5464 KB Output is correct
33 Correct 169 ms 5468 KB Output is correct
34 Correct 104 ms 5212 KB Output is correct
35 Correct 93 ms 5464 KB Output is correct
36 Correct 75 ms 5464 KB Output is correct
37 Correct 89 ms 5208 KB Output is correct
38 Correct 139 ms 5212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5144 KB Output is correct
3 Correct 3 ms 5140 KB Output is correct
4 Correct 3 ms 5208 KB Output is correct
5 Correct 2 ms 5180 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5112 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5240 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5208 KB Output is correct
13 Correct 3 ms 5140 KB Output is correct
14 Correct 3 ms 5208 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Execution timed out 3081 ms 23120 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5212 KB Output is correct
2 Correct 3 ms 5144 KB Output is correct
3 Correct 3 ms 5140 KB Output is correct
4 Correct 3 ms 5208 KB Output is correct
5 Correct 2 ms 5180 KB Output is correct
6 Correct 3 ms 5212 KB Output is correct
7 Correct 3 ms 5112 KB Output is correct
8 Correct 3 ms 5212 KB Output is correct
9 Correct 3 ms 5240 KB Output is correct
10 Correct 3 ms 5212 KB Output is correct
11 Correct 3 ms 5208 KB Output is correct
12 Correct 3 ms 5208 KB Output is correct
13 Correct 3 ms 5140 KB Output is correct
14 Correct 3 ms 5208 KB Output is correct
15 Correct 3 ms 5208 KB Output is correct
16 Correct 3 ms 5212 KB Output is correct
17 Correct 3 ms 5208 KB Output is correct
18 Correct 3 ms 5208 KB Output is correct
19 Correct 2 ms 5212 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 147 ms 5404 KB Output is correct
22 Correct 182 ms 5468 KB Output is correct
23 Correct 179 ms 5464 KB Output is correct
24 Correct 152 ms 5212 KB Output is correct
25 Correct 147 ms 5468 KB Output is correct
26 Correct 153 ms 5468 KB Output is correct
27 Correct 144 ms 5468 KB Output is correct
28 Correct 151 ms 5468 KB Output is correct
29 Correct 150 ms 5212 KB Output is correct
30 Correct 155 ms 5464 KB Output is correct
31 Correct 157 ms 5468 KB Output is correct
32 Correct 148 ms 5464 KB Output is correct
33 Correct 169 ms 5468 KB Output is correct
34 Correct 104 ms 5212 KB Output is correct
35 Correct 93 ms 5464 KB Output is correct
36 Correct 75 ms 5464 KB Output is correct
37 Correct 89 ms 5208 KB Output is correct
38 Correct 139 ms 5212 KB Output is correct
39 Execution timed out 3081 ms 23120 KB Time limit exceeded
40 Halted 0 ms 0 KB -