QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#449948#8716. 树JEdwardWA 0ms14136kbC++172.4kb2024-06-21 20:08:202024-06-21 20:08:20

Judging History

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

  • [2024-06-21 20:08:20]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:14136kb
  • [2024-06-21 20:08:20]
  • 提交

answer

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0), cin.tie(0)
#define int long long
#define endl '\n'
#define lowbit(x) (x)&(-x)
#define pii pair<int,int>
#define all(s) s.begin(), s.end()
#define ls(x) (x<<1)
#define rs(x) (x<<1|1) 
#define here system("pause")
using namespace std;

const int N = 2e5+5;
const int mod = 1e9+7;
const int INF = 1e15+7;
const double eps = 1e-6;
inline int pow(int a, int b, int p){ int res = 1%p; while(b){ if(b&1) res=res*a%p; a=a*a%p, b>>=1;} return res;}
inline int inv(int a, int p){ return pow(a,p-2,p)%p;}

int n, m, q;
vector<int> g[N];
int b[N], ok[N];
int f[N][20], dep[N];
inline void dfs(int u, int fa){
	f[u][0] = fa;
	dep[u] = dep[fa] + 1;
	for(int i=1;i<20 && f[u][i-1];i++){
		f[u][i] = f[f[u][i-1]][i-1];
	}
	for(auto v:g[u]){
		if(v==fa) continue;
		dfs(v, u);
	}
}

inline int getlca(int u, int v){
	if(dep[u]<dep[v]) swap(u, v);
	for(int i=19;i>=0;i--){
		if(dep[f[u][i]]>=dep[v]) u = f[u][i];
	}
	
	if(u==v) return u;
	for(int i=19;i>=0;i--){
		if(f[u][i]!=f[v][i]) u = f[u][i], v = f[v][i];
	}
	return f[v][0];
}

inline int check(int i){ //共链返回0 
	int x = b[i-1], y = b[i], z = b[i+1];
	if(getlca(x, y)==y && getlca(y, z)==y){
		if(getlca(x, z)==x || getlca(x, z)==z){
			return 1;
		}
	}
	if(getlca(x, y)==y || getlca(y, z)==y) return 0;
	else return 1; 
}

inline void sol(){
	cin >> n >> m >> q;
	for(int i=1;i<n;i++){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	
	dfs(1, 0);
	
	for(int i=1;i<=m;i++) cin >> b[i];
	
	for(int i=2;i<m;i++){
		ok[i] = check(i);
	}
	
	int ans = accumulate(ok+1,ok+1+m,0LL);
	while(q--){
		int idx, now;
		cin >> idx >> now;
		b[idx] = now;
		
		if(idx>2){
			int delta = ok[idx-1];
			ok[idx-1] = check(idx-1);
			delta -= ok[idx-1];
			ans -= delta;
		}
		if(idx<m-1){
			int delta = ok[idx+1];
			ok[idx+1] = check(idx+1);
			delta -= ok[idx+1];
			ans -= delta;
		}
		if(idx!=1 && idx!=m){
			int delta = ok[idx];
			ok[idx] = check(idx);
			delta -= ok[idx];
			ans -= delta;
		}

		
//		for(int i=1;i<=m;i++){
//			cout << " " << b[i];
//		}cout << endl;
//		for(int i=1;i<=m;i++){
//			cout << " " << ok[i];
//		}cout << endl;
		
		cout << ans+2 << endl;
	}
}

signed main(){
	IOS;
	int tc = 1;
	while(tc--){
		sol();
	}
	return 0;
}
/*
10 9 3
1 2
2 3
2 6
6 7
3 5
3 4
1 8
8 9
9 10
4 3 2 7 2 5 3 5 8
9 8
9 10
3 10
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

5 5 3
2 1
3 2
1 4
5 1
1 5 4 2 3
1 3
5 3
3 3

output:

4
4
5

result:

ok 3 number(s): "4 4 5"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 13620kb

input:

30 200 200
10 24
10 13
10 26
13 29
27 26
17 24
27 21
17 15
13 5
13 30
27 3
18 21
9 21
2 24
10 4
11 5
2 8
10 23
1 18
21 25
4 20
12 23
22 27
28 27
18 7
13 6
14 30
10 19
16 21
14 29 25 30 1 17 22 21 11 19 21 30 13 1 22 10 14 7 29 7 15 21 25 29 25 7 29 7 1 23 3 17 2 7 4 27 18 26 3 6 5 3 16 26 20 19 16 2...

output:

156
157
157
157
156
156
156
157
157
157
158
158
158
158
157
157
156
156
156
156
155
155
155
155
155
154
154
154
154
154
154
153
153
154
154
154
155
155
155
155
155
155
154
154
154
154
154
155
156
155
156
156
156
155
156
156
156
157
157
157
158
159
159
159
159
159
159
160
160
161
161
160
161
159
158
...

result:

wrong answer 1st numbers differ - expected: '174', found: '156'