QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#143811#4924. 蜘蛛爬树1kri0 1ms27496kbC++143.5kb2023-08-21 14:46:592023-08-21 14:47:14

Judging History

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

  • [2023-08-21 14:47:14]
  • 评测
  • 测评结果:0
  • 用时:1ms
  • 内存:27496kb
  • [2023-08-21 14:46:59]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define ll long long
#define inf 5000000000000000000ll
using namespace std;
ll read(){
	ll x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
	return x;
}
void write(ll x){
	if (x<10){
		putchar(x+'0');
		return;
	}
	write(x/10);
	putchar(x%10+'0');
	return;
}
int n,m,q;
int u[400005],v[400005],first[200005],nxt[400005];
ll a[200005],w[400005];
struct node{
	int c,s,t,l;
}d[200005];
int fa[200005],sz[200005],son[200005];
ll depth[200005];
void dfs1(int now,int f,ll d){
	fa[now]=f,depth[now]=d;
	sz[now]=1;
	son[now]=0;
	for (int i=first[now];i;i=nxt[i])
		if (v[i]!=f){
			dfs1(v[i],now,d+w[i]);
			sz[now]+=sz[v[i]];
			if (son[now]==0||sz[v[i]]>sz[son[now]])son[now]=v[i];
		}
	return;
}
int top[200005],idx,dfn[200005],nfd[200005],dfo[200005];
void dfs2(int now,int f,int t){
	top[now]=t;
	dfn[now]=++idx;
	nfd[idx]=now;
	if (son[now]!=0)dfs2(son[now],now,t);
	for (int i=first[now];i;i=nxt[i])
		if (v[i]!=f&&v[i]!=son[now])dfs2(v[i],now,v[i]);
	dfo[now]=idx;
	return;
}
int lca(int a,int b){
	while(top[a]!=top[b]){
		if (depth[top[a]]<depth[top[b]])swap(a,b);
		a=fa[top[a]];
	}
	if (depth[a]>depth[b])swap(a,b);
	return a;
}
ll dis(int a,int b){
	return depth[a]+depth[b]-2*depth[lca(a,b)];
}
ll ans[200005];
namespace Case1{
	void work(){
		for (int i=1;i<=q;i++){
			for (int j=1;j<=n;j++)
				ans[i]=min(ans[i],2*dis(d[i].l,j)+a[j]*d[i].c);
		}
		return;
	}
}
namespace Case2{
	ll ask(int c,int l,int r){
		ll ans=inf;
		for (int i=l;i<=r;i++){
			int x=nfd[i];
			ans=min(ans,2*depth[x]+a[x]*c);
		}
		return ans;
	}
	void work(){
		for (int i=1;i<=q;i++){
			int x=d[i].s;
			while(x!=0&&depth[x]>depth[d[i].l]){
				ans[i]=min(ans[i],ask(d[i].c,dfn[x],dfo[x])-2*depth[d[i].l]);
				x=fa[top[x]];
			}
			x=d[i].t;
			while(x!=0&&depth[x]>depth[d[i].l]){
				ans[i]=min(ans[i],ask(d[i].c,dfn[x],dfo[x])-2*depth[d[i].l]);
				x=fa[top[x]];
			}
		}
		return;
	}
}
namespace Case3{
	vector<pair<ll,ll>> qwq[200005];
	ll ask(int c,int l,int r){
		ll ans=inf;
		for (int i=l;i<=r;i++)
			for (int j=0;j<(int)qwq[i].size();j++)
				ans=min(ans,qwq[i][j].second+qwq[i][j].first*c);
		return ans;
	}
	void work(){
		for (int i=1;i<=n;i++){
			int x=i;
			while(x!=0)qwq[dfn[x]].push_back(make_pair(a[i],2*(depth[i]-depth[x]))),x=fa[top[x]];
		}
		for (int i=1;i<=q;i++){
			int x=d[i].s;
			while(x!=0&&depth[x]>depth[d[i].l]){
				ans[i]=min(ans[i],ask(d[i].c,max(dfn[top[x]],dfn[d[i].l]),dfn[x]));
				x=fa[top[x]];
			}
			x=d[i].t;
			while(x!=0&&depth[x]>depth[d[i].l]){
				ans[i]=min(ans[i],ask(d[i].c,max(dfn[top[x]],dfn[d[i].l]),dfn[x]));
				x=fa[top[x]];
			}
		}
		return;
	}
}
int main(){
	n=read(),m=read(),q=read();
	for (int i=1;i<=n;i++)a[i]=read();
	for (int i=1;i<n;i++){
		u[i]=read(),v[i]=read(),w[i]=read();
		nxt[i]=first[u[i]],first[u[i]]=i;
		u[i+n]=v[i],v[i+n]=u[i],w[i+n]=w[i];
		nxt[i+n]=first[u[i+n]],first[u[i+n]]=i+n;
	}
	dfs1(1,0,0);
	dfs2(1,0,1);
	for (int i=1;i<=q;i++){
		ll x=read(),y=read();
		d[i].s=x%n,d[i].t=y%n;
		if (d[i].s==0)d[i].s=n;
		if (d[i].t==0)d[i].t=n;
		d[i].l=lca(d[i].s,d[i].t);
		if (x<y)swap(x,y);
		d[i].c=(x-1)/n-(y-1)/n;
	}
	for (int i=1;i<=q;i++)ans[i]=inf;
	Case1::work();
	Case2::work();
	Case3::work();
	for (int i=1;i<=q;i++)write(ans[i]+depth[d[i].s]+depth[d[i].t]-2*depth[d[i].l]),putchar('\n');
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 0
Wrong Answer

Test #1:

score: 3
Accepted
time: 1ms
memory: 27248kb

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:

6130845802041
10806758605627
3440559556796
5426989115608
4458875959622
1566659300400
7908007295597
1846030561682
5112206409383
6968388472340
4706970599850
5270158948178
4633066810868
3176122148295
2331646415266
961435137842
14353365296423
9675072605938
4256954122467
7333255321628
8376795894537
12319...

result:

ok 1000 lines

Test #2:

score: 0
Accepted
time: 1ms
memory: 27496kb

input:

96 100 1000
31199641 534644486 57980198 794620020 322548308 524438614 467991232 68617179 243820212 229414440 471419229 316085673 271698528 136252783 926625435 615138376 200446739 551914057 483498389 879166147 512229523 45850421 337184110 799426876 46405170 427929494 235848997 861270528 291868400 616...

output:

2436336301732
4467388472930
6498834342013
6450642313333
4049880787954
7509100670176
5831628235154
4150554274586
3112250048344
202594784082
2974050754796
8714737807242
7727115169865
1321297431165
3071603311467
4662413775237
5469193332429
2306749862693
6240860740176
1297819731517
5602374629655
5876108...

result:

ok 1000 lines

Test #3:

score: -3
Wrong Answer
time: 0ms
memory: 24216kb

input:

96 100 1000
557703101 38662752 91559144 406758463 248251717 279124287 667387330 272252891 892434115 281731667 162886140 786660171 350559478 909940602 476034354 78826379 748607300 381191755 708777514 223906483 954905399 405424569 356033791 565979037 5787205 21241249 399771402 280058652 527147793 5875...

output:

82007488946
86777173467
35481695596
11498756054
87983213070
38502672413
33172639202
31451029430
111662176795
31626589074
105218154775
57116127519
14488184465
20368758481
41150521804
57639739744
45269689956
24620398400
51392609182
47597028824
72097558763
13572235163
78364419227
40255815091
1195379951...

result:

wrong answer 1st lines differ - expected: '80606469890', found: '82007488946'

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:


result:


Subtask #4:

score: 0
Time Limit Exceeded

Test #28:

score: 0
Time Limit Exceeded

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
Time Limit Exceeded

Test #43:

score: 0
Time Limit Exceeded

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:


result:


Subtask #7:

score: 0
Skipped

Dependency #1:

0%

Subtask #8:

score: 0
Skipped

Dependency #1:

0%