QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#327698#3307. Query on a Tree 17WRuperDWA 5ms17424kbC++143.6kb2024-02-15 12:17:292024-02-15 12:17:29

Judging History

你现在查看的是最新测评结果

  • [2024-02-15 12:17:29]
  • 评测
  • 测评结果:WA
  • 用时:5ms
  • 内存:17424kb
  • [2024-02-15 12:17:29]
  • 提交

answer

// Problem: I. Query On A Tree 17
// URL: https://codeforces.com/gym/102759/problem/I
// Writer: WRuperD
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int long long
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 2e5 + 10;
int n;
vector <int> g[MAX];

int fa[MAX][30];
int siz[MAX], son[MAX];

void dfs(int u, int father){
	siz[u] = 1;
	fa[u][0] = father; 
	for(int v : g[u]){
		if(v == father)	continue;
		dfs(v, u);
		siz[u] += siz[v];
		if(siz[v] > siz[son[u]])	son[u] = v;
	}
}

int dfn[MAX], top[MAX], dfx[MAX], clk;
int dep[MAX];

void dfs2(int u, int topu){
	dep[u] = dep[fa[u][0]] + 1;
	top[u] = topu, dfn[u] = ++clk, dfx[clk] = u;
	if(son[u])	dfs2(son[u], topu);
	for(int v : g[u]){
		if(v == fa[u][0] or v == son[u])	continue;
		dfs2(v, v);
	}
}

int s[MAX << 1], tag[MAX << 1];

void pushup(int x){
	s[x] = s[x << 1] + s[x << 1 | 1];
}

void pushdown(int x, int l, int r){
	int mid = (l + r) >> 1;
	s[x << 1] += tag[x] * (mid - l + 1), s[x << 1 | 1] += (r - mid) * tag[x];
	tag[x << 1] += tag[x], tag[x << 1 | 1] += tag[x];
	return ;
}

void build(int l, int r, int x){
	tag[x] = 0;
	if(l == r){
		s[x] = 1;
		return ;
	}
	int mid = (l + r) >> 1;
	build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
	pushup(x);
}

void add(int l, int r, int dl, int dr, int x){
	if(dl <= l and r <= dr){
		s[x] += r - l + 1;
		tag[x]++;
		return ;
	}
	pushdown(x, l, r);
	int mid = (l + r) >> 1;
	if(dl <= mid)	add(l, mid, dl, dr, x << 1);
	if(dr > mid)	add(mid + 1, r, dl, dr, x << 1 | 1);
	pushup(x);
}

int query(int l, int r, int dl, int dr, int x){
	if(dl <= l and r <= dr)	return s[x];
	pushdown(x, l, r);
	int mid = (l + r) >> 1, ans = 0;
	if(dl <= mid)	ans += query(l, mid, dl, dr, x << 1);
	if(dr > mid)	ans += query(mid + 1, r, dl, dr, x << 1 | 1);
	pushup(x);
	return ans;
}

int search(int l, int r, int val, int x){
	if(l == r)	return dfx[l];
	int mid = (l + r) >> 1;
	if(s[x << 1] < val)	return search(mid + 1, r, val - s[x << 1], x << 1 | 1);
	return search(l, mid, val, x << 1);	
}


void Add(int u, int v){
	while(top[u] != top[v]){
		if(dep[top[u]] < dep[top[v]])	swap(u, v);
		add(1, n, dfn[top[u]], dfn[u], 1);
		u = fa[top[u]][0];
	}
	if(dfn[u] > dfn[v])	swap(u, v);
	add(1, n, dfn[u], dfn[v], 1);
}

void solve(){
	n = read();
	for(int i = 1; i < n; i++){
		int u = read(), v = read();
		g[u].pb(v), g[v].pb(u);
	}
	dfs(1, 1), dfs2(1, 1);
	// build(1, n, 1);
	for(int i = 1; i <= 29; i++){
		for(int j = 1; j <= n; j++){
			fa[j][i] = fa[fa[j][i - 1]][i - 1];
		}
	}
	int q = read();
	for(int i = 1; i <= q; i++){
		int op = read();
		if(op == 1){
			int x = read();
			add(1, n, dfn[x], dfn[x] + siz[x] - 1, 1);
		}else{
			int u = read(), v = read();
			Add(u, v);
		}
		// write(s[1]), endl;
		int x = search(1, n, (s[1]) / 2 + 1, 1);
		for(int i = 29; i >= 0; i--){
			while (x != fa[x][i] and query(1, n, dfn[fa[x][i]], dfn[fa[x][i]] + siz[fa[x][i]] - 1, 1) <= s[1] / 2){
				x = fa[x][i];
			}
		}
		// write(x), endl;
		if(query(1, n, dfn[x], dfn[x] + siz[x] - 1, 1) <= s[1] / 2){
			x = fa[x][0];
		}
		write(x), endl;
	}
} 

signed main(){
	int t = 1;
	while(t--)	solve();
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 14412kb

input:

7
1 6
1 7
7 3
3 2
7 5
5 4
4
1 2
1 4
1 6
2 6 7

output:

2
7
7
1

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 3ms
memory: 16312kb

input:

15
1 2
1 3
2 4
3 7
6 2
3 8
4 9
2 5
5 11
7 13
10 4
11 15
12 5
14 7
11
2 6 11
1 6
2 10 9
1 9
2 2 6
1 4
2 7 6
1 2
2 8 13
1 10
2 8 15

output:

2
2
2
2
2
2
2
2
2
2
2

result:

ok 11 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 17424kb

input:

2
1 2
1
1 1

output:

1

result:

ok single line: '1'

Test #4:

score: 0
Accepted
time: 2ms
memory: 16144kb

input:

2
1 2
1
2 1 2

output:

1

result:

ok single line: '1'

Test #5:

score: -100
Wrong Answer
time: 5ms
memory: 15796kb

input:

100
1 2
58 2
2 87
63 87
65 63
65 19
6 87
58 21
87 8
87 74
91 6
95 65
2 61
87 59
3 61
37 87
67 87
23 2
63 9
87 46
40 67
70 2
12 58
46 75
75 36
28 3
12 33
72 87
39 6
75 52
85 70
1 76
26 40
47 28
2 49
41 65
66 28
51 37
15 47
86 51
8 60
97 19
48 58
72 90
33 83
97 54
36 5
23 14
78 52
90 7
99 2
48 82
48 6...

output:

72
3
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
28
2...

result:

wrong answer 3rd lines differ - expected: '3', found: '28'