QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#188337#5148. Tree DistancezhouhuanyiWA 472ms86104kbC++142.9kb2023-09-25 19:10:242023-09-25 19:10:24

Judging History

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

  • [2023-09-25 19:10:24]
  • 评测
  • 测评结果:WA
  • 用时:472ms
  • 内存:86104kb
  • [2023-09-25 19:10:24]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 200000
#define M 1000000
using namespace std;
const int inf=(int)(1e9);
const long long INF=(long long)(1e18);
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
struct reads
{
	int num;
	long long data;
	bool operator < (const reads &t)const
	{
		return num<t.num;	
	}
};
reads tong[N+1];
int n,q,rt,leng,length,smz,sz[N+1],szt[N+1],maxp[N+1],dque[N+1],top;
long long dis[N+1],ans[M+1],c[N+1];
vector<reads>E[N+1];
vector<reads>p[N+1];
vector<reads>v[N+1];
bool used[N+1],vis[N+1];
void add(int x,int y,int z)
{
	E[x].push_back((reads){y,z}),E[y].push_back((reads){x,z});
	return;
}
void get_rt(int x)
{
	vis[x]=sz[x]=1,maxp[x]=0;
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i].num]&&!vis[E[x][i].num])
			get_rt(E[x][i].num),sz[x]+=sz[E[x][i].num],maxp[x]=max(maxp[x],sz[E[x][i].num]);
	maxp[x]=max(maxp[x],smz-sz[x]);
	if (maxp[x]<maxp[rt]) rt=x;
	vis[x]=0;
	return;
}
void dfs(int x)
{
	vis[x]=szt[x]=1,tong[++length]=(reads){x,dis[x]};
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i].num]&&!vis[E[x][i].num])
			dis[E[x][i].num]=dis[x]+E[x][i].data,dfs(E[x][i].num),szt[x]+=szt[E[x][i].num];	
	return;
}
void solve(int x)
{
	used[x]=1,dis[x]=0,tong[length=1]=(reads){x,0};
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i].num])
			dis[E[x][i].num]=E[x][i].data,dfs(E[x][i].num);
	sort(tong+1,tong+length+1),top=0;
	for (int i=1;i<=length;++i)
	{
		while (top&&tong[dque[top]].data>=tong[i].data) top--;
		if (top) p[tong[i].num].push_back((reads){tong[dque[top]].num,tong[dque[top]].data+tong[i].data});
		dque[++top]=i;
	}
	top=0;
	for (int i=length;i>=1;--i)
	{
		while (top&&tong[dque[top]].data>tong[i].data) top--;
		if (top) p[tong[dque[top]].num].push_back((reads){tong[i].num,tong[i].data+tong[dque[top]].data});
		dque[++top]=i;
	}
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i].num])
			smz=szt[E[x][i].num],rt=0,get_rt(E[x][i].num),solve(rt);
	return;
}
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,long long d)
{
	for (;x<=n;x+=lowbit(x)) c[x]=min(c[x],d);
	return;
}
long long query(int x)
{
	long long res=INF;
	for (;x>=1;x-=lowbit(x)) res=min(res,c[x]);
	return res;
}
int main()
{
	int x,y,z,l,r;
	n=read(),maxp[0]=inf;
	for (int i=1;i<=n;++i) c[i]=INF;
	for (int i=1;i<=n-1;++i) x=read(),y=read(),z=read(),add(x,y,z);
	q=read();
	for (int i=1;i<=q;++i)
	{
		l=read(),r=read();
		if (l==r) ans[i]=-1;
		else v[r].push_back((reads){l,i});
	}
	smz=n,get_rt(1),solve(rt);
	for (int i=1;i<=n;++i)
	{
		for (int j=0;j<p[i].size();++j) add(n-p[i][j].num+1,p[i][j].data);
		for (int j=0;j<v[i].size();++j) ans[v[i][j].data]=query(n-v[i][j].num+1);
	}
	for (int i=1;i<=q;++i) printf("%lld\n",ans[i]);
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 6ms
memory: 25884kb

input:

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

output:

-1
3
7
7
2

result:

ok 5 number(s): "-1 3 7 7 2"

Test #2:

score: -100
Wrong Answer
time: 472ms
memory: 86104kb

input:

199999
31581 23211 322548833
176307 196803 690953895
34430 82902 340232856
36716 77480 466375266
7512 88480 197594480
95680 61864 679567992
19572 14126 599247796
188006 110716 817477802
160165 184035 722372640
23173 188594 490365246
54801 56250 304741654
10103 45884 643490340
127469 154479 214399361...

output:

29573323
3275382623493
4088
65959
4366
262663537528
38965457491
4867978
328055210
398008279281
25718730696
339719
92126
92126
4366
138216269
8212187
312114426361
2681285
4366
710854
114886
65959
27329726136
92126
91087
319082572106
26776689
5718199
710854
92126
195441906630
470023670
92126
710854
92...

result:

wrong answer 2nd numbers differ - expected: '1178569098929', found: '3275382623493'