QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#621846#8235. Top ClusterniolleWA 602ms96932kbC++232.3kb2024-10-08 17:34:072024-10-08 17:34:08

Judging History

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

  • [2024-10-08 17:34:08]
  • 评测
  • 测评结果:WA
  • 用时:602ms
  • 内存:96932kb
  • [2024-10-08 17:34:07]
  • 提交

answer

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dwn(i,a,b) for(int i=a;i>=b;i--)
#define lowbit(x) (x&(-x))
#define MAXN 502501
#define mp(x,y) make_pair(x,y)
using namespace std;
typedef long long ll;
inline int read(){
	int x=0,f=1;
	char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-') f=-1; ch=getchar();}
	while('0'<=ch && ch<='9') x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
	return x*f;
}
int dfn[MAXN],cnt,n,m,a[MAXN],w[MAXN],p[MAXN][20],pos[MAXN],lg[MAXN],K;
ll D[MAXN];
vector<pair<int,ll> > v[MAXN];
int get(int x,int y){
	if(dfn[x] < dfn[y]) return x;
	return y;
}

int lca(int x,int y){
	if(x == y) return x;
	x = dfn[x], y = dfn[y];
	if(x > y) swap(x,y);
	int k = lg[y - x];
	x++;
	return get(p[x][k], p[y - (1<<k) + 1][k]);
}
void dfs(int x,int y){
	dfn[x] = ++cnt;
	p[x][0] = y;
	for(auto tmp : v[x]){
		int u = tmp.first;
		ll z = tmp.second;
		if(u == y) continue;
		D[u] = D[x] + z;
		dfs(u,x);
	}
}
int L[MAXN],R[MAXN];
ll calc(int x,int y){
	if(x == y) return 0;
	int z = lca(x,y);
	ll res = D[x] + D[y] - D[z] * 2;
	return res;
}
void pre(){
	lg[0] = -1;
	rep(i,1,n) lg[i] = lg[i/2] + 1;
	rep(j,1,19){
		rep(i,1,n) p[i][j] = get(p[i][j-1], p[min(i+(1<<(j-1)),n)][j-1]);
	}
	if(!K) return;
	L[0] = R[0] = pos[0];
	rep(i,1,K-1){
		if(calc(L[i-1],R[i-1]) < calc(L[i-1],pos[i]) && calc(pos[i],R[i-1]) < calc(L[i-1],pos[i])){
			L[i] = L[i-1];
			R[i] = pos[i];
		}
		else if(calc(L[i-1],R[i-1]) < calc(pos[i],R[i-1])){
			L[i] = pos[i];
			R[i] = R[i-1];
		}
		else{
			L[i] = L[i-1];
			R[i] = R[i-1];
		}
	}
}
int main(){
	n = read(); m = read();
	rep(i,1,n) a[i] = read();
	rep(i,1,n) if(a[i] <= n) pos[a[i]] = i;
	while(pos[K]) ++K;
	rep(i,1,n-1){
		int x = read(), y = read(),z = read();
		v[x].emplace_back(y,z);
		v[y].emplace_back(x,z);
	}
	dfs(1,0);
	pre();
//	rep(i,0,K-1) cout<<L[i] << " ";cout<<endl;
//	rep(i,0,K-1) cout<<R[i] << " "; cout<<endl;
	if(!K){
		rep(i,1,m){
			int x = read();
			puts("0");
		}
		return 0;
	}
	rep(i,1,m){
		int x = read();
		ll k; scanf("%lld",&k);
		int l = 0, r = K-1;
		if(calc(pos[0],x) > k){
			puts("0");
			continue;
		}
		while(l < r){
			int mid = (l+r+1) >> 1;
			if(max(calc(L[mid], x), calc(R[mid],x)) <= k)  l = mid;
			else r = mid - 1;
		}
		printf("%d\n",l+1);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 14192kb

input:

5 4
3 9 0 1 2
1 2 10
3 1 4
3 4 3
3 5 2
3 0
1 0
4 6
4 7

output:

1
0
3
4

result:

ok 4 number(s): "1 0 3 4"

Test #2:

score: -100
Wrong Answer
time: 602ms
memory: 96932kb

input:

500000 500000
350828 420188 171646 209344 4 999941289 289054 79183 999948352 427544 160827 138994 192204 108365 99596 999987124 292578 2949 384841 269390 999920664 315611 163146 51795 265839 34188 999939494 145387 366234 86466 220368 357231 347706 332064 279036 173185 5901 217061 112848 37915 377359...

output:

0
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
249999
2499...

result:

wrong answer 36936th numbers differ - expected: '14', found: '4'