QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#308158#5439. Meet in the Middlexx019WA 43ms58020kbC++204.0kb2024-01-19 17:05:002024-01-19 17:05:00

Judging History

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

  • [2024-01-19 17:05:00]
  • 评测
  • 测评结果:WA
  • 用时:43ms
  • 内存:58020kb
  • [2024-01-19 17:05:00]
  • 提交

answer

#include<bits/stdc++.h>
#define int long long
#ifdef DEBUG
#define msg(args...) fprintf(stderr,args)
#else
#define msg(...) void()
#endif
using namespace std;
const int inf=1e18;
inline int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
struct edge{
	int v,w,nxt;
};
struct Graph{
	int tot,head[100005];edge e[200005];
	void add(int u,int v,int w){
		e[++tot]=(edge){v,w,head[u]},head[u]=tot;
	}
	int cur,dfn[100005],rnk[100005],d[100005],f[22][100005],siz[100005];
	int getmin(int x,int y){
		return ((dfn[x]<dfn[y])?x:y);
	}
	void dfs(int u,int fa){
		dfn[u]=++cur,rnk[cur]=u,f[0][cur]=fa;siz[u]=1;
		for(int i=head[u];i;i=e[i].nxt){
			int v=e[i].v,w=e[i].w;if(v==fa)continue;
			d[v]=d[u]+w;dfs(v,u);siz[u]+=siz[v];
		}
	}
	int getlca(int u,int v){
		if(u==v)return u;
		if((u=dfn[u])>(v=dfn[v]))swap(u,v);
		int o=__lg(v-u++);
		return getmin(f[o][u],f[o][v-(1ll<<o)+1]);
	}
	void init(){
		dfs(1,0);
		for(int j=1;(1ll<<j)<=cur;j++){
			for(int i=1;i+(1ll<<j)-1<=cur;i++){
				f[j][i]=getmin(f[j-1][i],f[j-1][i+(1ll<<(j-1))]);
			}
		}
	}
	int dist(int u,int v){
		return d[u]+d[v]-d[getlca(u,v)]*2;
	}
}t1,t2;
struct Chain{
	int u,v,du,dv,d;
	Chain(const int &_u=0,const int &_v=0,const int &_du=0,const int &_dv=0,const int &_d=0):u(_u),v(_v),du(_du),dv(_dv),d(_du+_dv+_d){}
	bool operator <(const Chain &b)const{
		return d<b.d;
	}
	Chain operator +(const Chain &b)const{
		Chain c=max(*this,b);
		if(u&&b.u)c=max(c,Chain(u,b.u,du,b.du,t1.dist(u,b.u)));
		if(u&&b.v)c=max(c,Chain(u,b.v,du,b.dv,t1.dist(u,b.v)));
		if(v&&b.u)c=max(c,Chain(v,b.u,dv,b.du,t1.dist(v,b.u)));
		if(v&&b.v)c=max(c,Chain(v,b.v,dv,b.dv,t1.dist(v,b.v)));		
		return c;
	}
};
struct segtree{
	#define ls (p<<1)
	#define rs (p<<1|1)
	#define lson l,mid,ls
	#define rson mid+1,r,rs
	Chain c[400005];int tag[400005];
	void pushup(int p){
		c[p]=c[ls]+c[rs];
	}
	void pushdown(int p){
		tag[ls]+=tag[p],tag[rs]+=tag[p];
		if(c[ls].u)c[ls].du+=tag[p];
		if(c[ls].v)c[ls].dv+=tag[p];
		if(c[ls].u&&c[ls].v)c[ls].d+=2*tag[p];
		if(c[rs].u)c[rs].du+=tag[p];
		if(c[rs].v)c[rs].dv+=tag[p];
		if(c[rs].u&&c[rs].v)c[rs].d+=2*tag[p];	
		tag[p]=0;
	}
	void build(int l,int r,int p){
		tag[p]=0;
		if(l==r){
			c[p]=Chain(t2.rnk[l],0,t2.d[t2.rnk[l]],0,0);
			return;
		}
		int mid=(l+r)>>1;
		build(lson);build(rson);
		pushup(p);
	}
	void add(int l,int r,int p,int L,int R,int v){
		if(L>R)return;
		if(L<=l&&r<=R){
			tag[p]+=v;
			if(c[p].u)c[p].du+=v;
			if(c[p].v)c[p].dv+=v;
			if(c[p].u&&c[p].v)c[p].d+=2*v;
			return;
		}
		int mid=(l+r)>>1;pushdown(p);
		if(L<=mid)add(lson,L,R,v);
		if(R>mid)add(rson,L,R,v);
		pushup(p);
	}
	Chain ask(int l,int r,int p,int L,int R){
		if(L<=l&&r<=R)return c[p];
		int mid=(l+r)>>1;pushdown(p);
		if(R<=mid)return ask(lson,L,R);
		if(L>mid)return ask(rson,L,R);
		return ask(lson,L,R)+ask(rson,L,R);
	}
	#undef lson
	#undef rson
	#undef ld
	#undef rs
}Tr;
int T,n,q,qu[500005],qv[500005],ans[500005];vector<int>t[100005];
void solve(int u,int fa){
	Chain l=Tr.ask(1,n,1,1,n);
	for(auto x:t[u]){
		if(l.u)ans[x]=max(ans[x],t1.dist(qu[x],l.u)+t2.dist(l.u,qv[x]));
		if(l.v)ans[x]=max(ans[x],t1.dist(qu[x],l.v)+t2.dist(l.v,qv[x]));		
	}
	for(int i=t2.head[u];i;i=t2.e[i].nxt){
		int v=t2.e[i].v,w=t2.e[i].w;if(v==fa)continue;
		Tr.add(1,n,1,t2.dfn[v],t2.dfn[v]+t2.siz[v]-1,-w);
		Tr.add(1,n,1,1,t2.dfn[v]-1,w);
		Tr.add(1,n,1,t2.dfn[v]+t2.siz[v],n,w);
		solve(v,u);
		Tr.add(1,n,1,t2.dfn[v],t2.dfn[v]+t2.siz[v]-1,w);
		Tr.add(1,n,1,1,t2.dfn[v]-1,-w);
		Tr.add(1,n,1,t2.dfn[v]+t2.siz[v],n,-w);
	}
}
signed main(){
	n=read(),q=read();
	for(int i=1,u,v,w;i<n;i++)u=read(),v=read(),w=read(),t1.add(u,v,w),t1.add(v,u,w);
	for(int i=1,u,v,w;i<n;i++)u=read(),v=read(),w=read(),t2.add(u,v,w),t2.add(v,u,w);
	t1.init();t2.init();Tr.build(1,n,1);
	for(int i=1;i<=q;i++)qu[i]=read(),qv[i]=read(),t[qv[i]].push_back(i);
	solve(1,0);for(int i=1;i<=q;i++)printf("%lld\n",ans[i]); 
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 36716kb

input:

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

output:

6
4
5
3

result:

ok 4 number(s): "6 4 5 3"

Test #2:

score: 0
Accepted
time: 0ms
memory: 34584kb

input:

2 1
1 2 1
1 2 1
1 1

output:

2

result:

ok 1 number(s): "2"

Test #3:

score: 0
Accepted
time: 3ms
memory: 32472kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 43ms
memory: 57496kb

input:

10000 50000
8101 5996 108427744
5996 7605 870838849
5996 5599 603303696
8101 3006 339377417
8101 6463 442516687
6463 5560 109174079
5560 4063 127596224
3006 1682 947915262
5996 1986 130416311
6463 5455 279771516
6463 2634 516688796
4063 3034 217886863
7605 5092 742375061
5599 2266 193804402
5092 140...

output:

647838384844
626539793176
514273941704
637066393138
472546379596
645842915960
641537859258
573604504956
644081575470
803875451466
674370549986
734764046916
744862815441
763778393516
553499885160
526743824759
610373719514
689550247042
549161302822
726811438160
653134244120
666761991962
701575393972
6...

result:

ok 50000 numbers

Test #5:

score: 0
Accepted
time: 30ms
memory: 57596kb

input:

10000 50000
5314 8843 137901358
5314 4153 459134340
5314 8667 933926892
4153 6504 330487798
4153 8880 750362377
4153 5990 874275912
4153 546 563436331
5990 6216 902348875
8843 3101 669215553
6216 8138 732343176
8667 8675 581114294
6504 7416 127778711
546 4239 282695908
6504 9455 549237168
5314 8340 ...

output:

464564968641
331633000004
565299667784
484694871646
570451097836
417492802442
372302349684
638725688107
386235986078
355738655551
462027769535
558485994764
524714144289
450157947013
432701214095
494566741391
529031758638
637683369382
415646847933
344894296260
390294136162
527685175763
575151290175
3...

result:

ok 50000 numbers

Test #6:

score: -100
Wrong Answer
time: 35ms
memory: 58020kb

input:

10000 50000
2808 2490 757309564
2808 9601 312271047
2808 4046 119325897
2808 4894 466822371
4894 1507 498399554
2490 5982 84088145
9601 1251 149019541
2808 6681 416590999
2808 6583 357757899
1251 3192 889947539
6583 9762 350572496
6681 22 597479070
5982 8744 263208242
8744 5281 49894126
1507 8806 30...

output:

1496166931438
2058806276380
2007322626439
2044250452467
1543567245539
1695101693278
1765462307870
2576423082091
2293041259117
2085376969344
2375783476943
1954788661090
2056530503168
2453153202726
1973122281824
2096456496839
2205257612773
2015714406862
1550970509166
2122832986951
2102262624814
168109...

result:

wrong answer 1st numbers differ - expected: '1501072697023', found: '1496166931438'