QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#152644#4924. 蜘蛛爬树zhouhuanyi0 1869ms253208kbC++146.3kb2023-08-28 15:50:112023-08-28 15:50:12

Judging History

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

  • [2023-08-28 15:50:12]
  • 评测
  • 测评结果:0
  • 用时:1869ms
  • 内存:253208kb
  • [2023-08-28 15:50:11]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 800000
#define M 30
using namespace std;
long long read()
{
	char c=0;
	long long sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
const int inf=(int)(1e9);
struct reads
{
	int num,snum;
	long long data;
};
struct points
{
	int x;
	long long y;
};
int n,m,q,maxn,leng,length,rt,SD[N+1],X[N+1],Y[N+1],L[N+1],lg[N+1],p[N+1],fa[N+1],ls[N+1],rs[N+1],a[N+1],depth[N+1],smz,sz[N+1],minn,sx,sy,sd,sw;
long long ans[N+1],dis[N+1][M+1],s[N+1],t[N+1];
vector<reads>E[N+1];
vector<reads>ES[N+1];
vector<points>v[N+1];
bool used[N+1],vis[N+1];
void add(int x,int y,int z,long long w)
{
	E[x].push_back((reads){y,z,w}),E[y].push_back((reads){x,z,w});
	return;
}
void add2(int x,int y,long long z)
{
	++leng,ES[x].push_back((reads){y,leng,z}),ES[y].push_back((reads){x,leng,z});
	return;
}
bool cmp(int x,int y)
{
	return a[x]<a[y];
}
void dfs(int x)
{
	int nw=0,d=0;
	used[x]=1;
	for (int i=0;i<E[x].size();++i)
		if (!used[E[x][i].num])
		{
			dfs(E[x][i].num);
			if (!nw) nw=E[x][i].num,d=E[x][i].data;
			else ++length,add2(length,nw,d),add2(length,E[x][i].num,E[x][i].data),nw=length,d=0;
		}
	if (nw) add2(x,nw,d);
	return;
}
void get_rt(int x)
{
	vis[x]=sz[x]=1;
	for (int i=0;i<ES[x].size();++i)
		if (!used[ES[x][i].snum]&&!vis[ES[x][i].num])
		{
			get_rt(ES[x][i].num),sz[x]+=sz[ES[x][i].num];
			if (max(sz[ES[x][i].num],smz-sz[ES[x][i].num])<minn) minn=max(sz[ES[x][i].num],smz-sz[ES[x][i].num]),sx=x,sy=ES[x][i].num,sd=ES[x][i].snum,sw=ES[x][i].data;
		}
	vis[x]=0;
	return;
}
void dfs2(int x,int d)
{
	vis[x]=1;
	for (int i=0;i<ES[x].size();++i)
		if (!used[ES[x][i].snum]&&!vis[ES[x][i].num])
			dis[ES[x][i].num][d]=dis[x][d]+ES[x][i].data,dfs2(ES[x][i].num,d);
	vis[x]=0;
	return;
}
int solve(int x,int szt,int d)
{
	maxn=max(maxn,d),smz=szt,minn=inf,get_rt(x);
	if (minn==inf)
	{
		depth[x]=d;
		return x;
	}
	int nw=++length,tx=sx,ty=sy,dx=szt-sz[sy],dy=sz[sy];
	depth[nw]=d,used[sd]=1,dfs2(sx,d),dis[sy][d]=sw,dfs2(sy,d),ls[nw]=solve(tx,dx,d+1),rs[nw]=solve(ty,dy,d+1),fa[ls[nw]]=fa[rs[nw]]=nw;
	return nw;
}
points operator + (points a,points b)
{
	return (points){a.x+b.x,a.y+b.y};
}
points operator - (points a,points b)
{
	return (points){a.x-b.x,a.y-b.y};
}
long long operator * (points a,points b)
{
	return 1ll*a.x*b.y-1ll*a.y*b.x;
}
bool check(points a,points b,points c)
{
	return (b-a)*(c-a)<=0;
}
int lca(int x,int y)
{
	if (depth[x]<depth[y]) swap(x,y);
	while (depth[x]!=depth[y]) x=fa[x];
	while (x!=y) x=fa[x],y=fa[y];
	return x;
}
int main()
{
	int x,y,sx,tx,d1,d2,d,sd,ps,l;
	points ds;
	long long z;
	for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
	length=n=read(),m=read(),q=read();
	for (int i=1;i<=n;++i) a[i]=read(),p[i]=i;
	sort(p+1,p+n+1,cmp);
	for (int i=1;i<=n-1;++i) x=read(),y=read(),z=read(),add(x,y,i,z);
	dfs(1);
	for (int i=1;i<=n;++i) used[i]=0;
	rt=solve(1,length,1);
	for (int i=1;i<=q;++i) s[i]=read(),t[i]=read(),d1=(s[i]-1)/n+1,d2=(t[i]-1)/n+1,sd=SD[i]=abs(d1-d2),x=X[i]=s[i]-(d1-1)*n,y=Y[i]=t[i]-(d2-1)*n,l=L[i]=lca(X[i],Y[i]),ans[i]=dis[x][depth[l]]+dis[y][depth[l]]+1ll*min(a[x],a[y])*sd;
	for (int w=1;w<=maxn;++w)
	{
		for (int i=1;i<=n;++i) v[i].clear();
		for (int i=1;i<=n;++i)
		{
			sx=p[i];
			for (int j=depth[p[i]];j>=w+1;--j)
			{
				ds=(points){a[p[i]],dis[p[i]][j-1]+dis[p[i]][w]};
				while (v[x].size()>=2&&check(v[x][(int)(v[x].size())-2],v[x].back(),ds)) v[x].pop_back();
				sx=fa[sx];
			}
		}
		for (int i=1;i<=q;++i)
		{
			x=X[i],y=Y[i],sd=SD[i],sx=l=L[i];
			for (int j=depth[l];j>=2;--j)
			{
				if (ls[fa[sx]]==sx)
				{
					d=depth[fa[sx]],tx=rs[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][j-1]);
					}
				}
				else
				{
					d=depth[fa[sx]],tx=ls[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][j-1]);
					}
				}
				sx=fa[sx];
			}
			sx=x;
			for (int j=depth[x];j>=depth[l]+2;--j)
			{
				if (ls[fa[sx]]==sx)
				{
					d=depth[l],tx=rs[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][depth[l]]);
					}
				}
				else
				{
					d=depth[l],tx=ls[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][j-1]+dis[y][depth[l]]);
					}
				}
				sx=fa[sx];
			}
			sx=y;
			for (int j=depth[y];j>=depth[l]+2;--j)
			{
				if (ls[fa[sx]]==sx)
				{
					d=depth[l],tx=rs[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][depth[l]]+dis[y][j-1]);
					}
				}
				else
				{
					d=depth[l],tx=ls[fa[sx]],ps=0;
					if (d==w&&!v[tx].empty())
					{
						for (int k=lg[v[tx].size()];k>=0;--k)
							if (ps+(1<<k)<v[tx].size()&&1ll*v[tx][ps+(1<<k)].x*sd+v[tx][ps+(1<<k)].y<1ll*v[tx][ps+(1<<k)-1].x*sd+v[tx][ps+(1<<k)-1].y)
								ps+=(1<<k);
						ans[i]=min(ans[i],1ll*v[tx][ps].x*sd+v[tx][ps].y+dis[x][depth[l]]+dis[y][j-1]);
					}
				}
				sx=fa[sx];
			}
		}
	}
	for (int i=1;i<=q;++i) printf("%lld\n",ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 7ms
memory: 90132kb

input:

97 99 1000
763118300 558295517 80676328 362318619 473105413 468927175 311496507 936935430 176032966 304576040 308583326 681580095 549392233 518152994 829474320 751189715 542810029 587869244 878512027 530678371 832766129 535259635 799122942 596982955 884696876 605325753 495661541 506105495 561218313 ...

output:

205112270105
2573306299195
1143425975976
1720432339160
242690454612
46240877616
2485867097465
691865755167
1974616351411
1098297412216
1115876176668
3160389925594
1609778516122
791835654789
-520826462
7952398130
4317533546168
1965043763112
219913594425
1809927378972
693099401993
2785194157201
212033...

result:

wrong answer 1st lines differ - expected: '6130845802041', found: '205112270105'

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Wrong Answer

Test #20:

score: 0
Wrong Answer
time: 1869ms
memory: 201724kb

input:

200000 20 200000
679416469 548913625 468159997 137709609 883140368 682558021 473174374 400192350 124143873 825920417 372498686 851213321 822264481 78195915 5427143 453304163 233551905 810910186 810046144 52603791 282167184 385032797 81387991 747194790 917579656 585184539 12659388 249218417 158295502...

output:

3980024163
1784619454
1949784467
454184150
1896194822
2885546876
6012349070
9831099599
325228596
1247964128
11444312288
1496800365
900128416
368700872
2262074037
6485693162
6403382585
9345435648
1080812812
499629330
1563297347
2611426970
1585595527
573783312
677972557
5775768009
1593736387
121904394...

result:

wrong answer 1st lines differ - expected: '920563165', found: '3980024163'

Subtask #4:

score: 0
Wrong Answer

Test #28:

score: 0
Wrong Answer
time: 1524ms
memory: 187712kb

input:

200000 1000000000 200000
28270302 472359923 262785485 923929785 393684160 761485431 72038469 116384740 426631758 437934930 610834083 455314140 734276543 903544756 220163018 756113376 732404264 947339315 109784905 625451008 794076307 818852312 758007217 124450858 674924509 311761991 507260538 7032362...

output:

408620964552639518
137275910048870869
245032378632876484
226912453065478160
172639164082682308
3961122010483350
5826658254003430
272405607856442849
314815472729450377
131060845599243198
5318303819780363
123438875226839753
107190538183339166
57673037634187303
39615161809282950
251852939015636
7904010...

result:

wrong answer 1st lines differ - expected: '29294995135992468', found: '408620964552639518'

Subtask #5:

score: 0
Wrong Answer

Test #36:

score: 0
Wrong Answer
time: 1717ms
memory: 253208kb

input:

199918 1000000000 199903
1496 2382 3896 3664 1177 1627 2821 4200 3074 3783 2069 4403 629 2610 4991 4074 3033 2798 4333 3501 3667 3064 663 2821 2818 458 2950 4020 2665 3578 63 4855 4941 3492 2423 4510 1489 1018 4829 1912 3133 3174 309 287 2909 4102 4296 4526 3170 3683 4960 4863 4738 2927 2405 3600 44...

output:

2071007920615
1839029856826
1066241782525
2157818060603
1662886239175
969145405500
1339592458136
1933288585261
1443618884931
191026884875
1360674048895
3288261183838
844307581832
1057272200593
415661809139
1348580200040
1807520375013
1750561360212
1992016378199
1118996679555
952936773828
30703401485...

result:

wrong answer 1st lines differ - expected: '1352416884531', found: '2071007920615'

Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 1404ms
memory: 192476kb

input:

200000 1000000000 200000
81882094 47220813 43282454 17633207 52769165 4830673 31396360 64793163 9174729 36727563 71268262 24662923 40146030 1430053 62926106 30042905 1330107 81817720 98841078 87766129 51155045 23216268 79896310 66625868 87854925 42976560 86542933 28336449 34932261 19698851 584453 90...

output:

36292263825666071
1763117959335124
70435143535497547
29019745040883107
4262447355675939
9251048995732454
237490078556628
10552155591925951
15410082649675867
3386074704304205
12593925717420146
50874589326787510
7958779216393356
64893851546150475
22211363504306354
1028961791274806
10920709650662609
26...

result:

wrong answer 1st lines differ - expected: '516260625003899', found: '36292263825666071'

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%