QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#152647#4924. 蜘蛛爬树zhouhuanyi0 3977ms290832kbC++146.3kb2023-08-28 16:00:472023-08-28 16:00:47

Judging History

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

  • [2023-08-28 16:00:47]
  • 评测
  • 测评结果:0
  • 用时:3977ms
  • 内存:290832kb
  • [2023-08-28 16:00:47]
  • 提交

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[sx].size()>=2&&check(v[sx][(int)(v[x].size())-2],v[sx].back(),ds)) v[sx].pop_back();
				v[sx].push_back(ds),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;
}

詳細信息

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 4ms
memory: 90136kb

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
884891791240
319450990088
65219036300
6925745930
1667410289331
681079321297
1083946744713
910405864932
897934911835
2560158366635
1012121212963
622161887405
-520826462
7952398130
3466532462182
1938623903271
29994316851
1626068593180
693099401993
2772254992337
181282921199
...

result:

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

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Time Limit Exceeded

Test #20:

score: 0
Time Limit Exceeded

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:

920563165
390924365
348423545
342447817
431272758
740276417
559234469
658182437
325228596
996609944
386268490
857772051
341151839
340718771
412815082
574339246
494955209
583233306
1051825833
253016414
635172427
488217962
733481075
573783312
417179127
510718081
921329142
1032884265
484644483
41015524...

result:


Subtask #4:

score: 0
Runtime Error

Test #28:

score: 0
Runtime Error

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:


result:


Subtask #5:

score: 0
Time Limit Exceeded

Test #36:

score: 0
Time Limit Exceeded

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:


result:


Subtask #6:

score: 0
Wrong Answer

Test #43:

score: 0
Wrong Answer
time: 3977ms
memory: 290832kb

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:

203268883618998
114361493284029
107861670076610
121945826738366
107259974402293
163451856357263
98338128361488
157404130695236
125382686350520
83072932870308
125374219412919
140347105237488
168585065200176
151763792460734
194255913594932
150431475296355
130820548668851
138427058680683
15973526120479...

result:

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

Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%