QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#87282#5439. Meet in the Middlefzj2007WA 50ms35564kbC++145.0kb2023-03-12 11:26:282023-03-12 11:26:30

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-03-12 11:26:30]
  • 评测
  • 测评结果:WA
  • 用时:50ms
  • 内存:35564kb
  • [2023-03-12 11:26:28]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace IO{
	template<typename T>inline bool read(T &x){
		x=0;
		char ch=getchar();
		bool flag=0,ret=0;
		while(ch<'0'||ch>'9') flag=flag||(ch=='-'),ch=getchar();
		while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(),ret=1;
		x=flag?-x:x;
        return ret;
	}
	template<typename T,typename ...Args>inline bool read(T& a,Args& ...args){
	    return read(a)&&read(args...);
	}
	template<typename T>void prt(T x){
		if(x>9) prt(x/10);
		putchar(x%10+'0');
	}
	template<typename T>inline void put(T x){
		if(x<0) putchar('-'),x=-x;
		prt(x);
	}
	template<typename T>inline void put(char ch,T x){
		if(x<0) putchar('-'),x=-x;
		prt(x);
		putchar(ch);
	}
	template<typename T,typename ...Args>inline void put(T a,Args ...args){
	    put(a);
		put(args...);
	}
	template<typename T,typename ...Args>inline void put(const char ch,T a,Args ...args){
	    put(ch,a);
		put(ch,args...);
	}
	inline void put(string s){
		for(int i=0,sz=s.length();i<sz;i++) putchar(s[i]);
	}
	inline void put(const char* s){
		for(int i=0,sz=strlen(s);i<sz;i++) putchar(s[i]);
	}
}
using namespace IO;
#define N 100005
#define ll long long
int n,m;
ll ans[N*5],val[N];
vector<pair<int,int> > e1[N],e2[N];
vector<pair<int,int> > q[N];
namespace Tree{
	int st[N<<1][20],id[N],idx,lg[N<<1],dep[N];
	ll pre[N];
	inline void dfs(int x,int fa){
		st[++idx][0]=x,id[x]=idx,dep[x]=dep[fa]+1;
		for(auto tmp:e1[x]){
			int v=tmp.first;
			if(v!=fa) pre[v]=pre[x]+tmp.second,dfs(v,x),st[++idx][0]=x;
		}
	}
	inline int Min(int x,int y){
		return dep[x]<dep[y]?x:y;
	}
	inline void prework(){
		dfs(1,0);
		for(int i=2;i<=idx;i++) lg[i]=lg[i>>1]+1;
		for(int j=1;j<=lg[idx];j++)
			for(int i=1;i+(1<<j)-1<=idx;i++)
				st[i][j]=Min(st[i][j-1],st[i+(1<<j-1)][j-1]);
	}
	inline int lca(int x,int y){
		int l=id[x],r=id[y];
		if(l>r) swap(l,r);
		int k=lg[r-l+1];
		return Min(st[l][k],st[r-(1<<k)+1][k]);
	}
	inline ll dist(int x,int y){
		return pre[x]+pre[y]-2ll*pre[lca(x,y)];
	}
}
int dfn[N],siz[N],idx,id[N];
inline void dfs(int x,int fa){
	dfn[x]=++idx,id[idx]=x,siz[x]=1;
	for(auto tmp:e1[x]) 
		if(tmp.first!=fa) dfs(tmp.first,x),siz[x]+=siz[tmp.first];
}
struct Segment{
	struct node{
		pair<int,ll> u,v;
		ll tag;
		node(pair<int,ll> _u=make_pair(0,0),pair<int,ll> _v=make_pair(0,0)):u(_u),v(_v){}
		inline node operator+(const node &b)const{
			ll maxn=-1;
			node res;
			if(u.second+b.u.second+Tree::dist(u.first,b.u.first)>maxn) maxn=u.second+b.u.second+Tree::dist(u.first,b.u.first),res=node(u,b.u);
			if(v.second+b.u.second+Tree::dist(v.first,b.u.first)>maxn) maxn=v.second+b.u.second+Tree::dist(v.first,b.u.first),res=node(v,b.u);
			if(u.second+b.v.second+Tree::dist(u.first,b.v.first)>maxn) maxn=u.second+b.v.second+Tree::dist(u.first,b.v.first),res=node(u,b.v);
			if(v.second+b.v.second+Tree::dist(v.first,b.v.first)>maxn) maxn=v.second+b.v.second+Tree::dist(v.first,b.v.first),res=node(v,b.v);
			if(u.second+v.second+Tree::dist(u.first,v.first)>maxn) maxn=u.second+v.second+Tree::dist(u.first,v.first),res=node(u,v);
			if(b.u.second+b.v.second+Tree::dist(b.u.first,b.v.first)>maxn) maxn=b.u.second+b.v.second+Tree::dist(b.u.first,b.v.first),res=node(b.u,b.v);
			return res;
		}
	}t[N<<2];
	#define lc(x) (x<<1)
	#define rc(x) (x<<1|1)
	inline void push_up(int x){
		t[x]=t[lc(x)]+t[rc(x)];
	}
	inline void push_tag(int x,ll val){
		t[x].tag+=val;
		t[x].u.second+=val;
		t[x].v.second+=val;
	}
	inline void push_down(int x){
		if(t[x].tag){
			push_tag(lc(x),t[x].tag);
			push_tag(rc(x),t[x].tag);
			t[x].tag=0;
		}
	}
	inline void build(int x,int l,int r){
		if(l==r) return t[x].u=t[x].v=make_pair(id[l],val[id[l]]),void();
		int mid=l+r>>1;
		build(lc(x),l,mid),build(rc(x),mid+1,r);
		push_up(x);
	}
	inline void update(int x,int l,int r,int ql,int qr,ll val){
		if(ql<=l&&qr>=r) return push_tag(x,val),void();
		int mid=l+r>>1;
		push_down(x);
		if(ql<=mid) update(lc(x),l,mid,ql,qr,val);
		if(qr>mid) update(rc(x),mid+1,r,ql,qr,val);
		push_up(x);   
	}
	#undef lc
	#undef rc
}T;
inline void presolve(int x,int fa){
	for(auto tmp:e2[x])
		if(tmp.first!=fa) val[tmp.first]=val[x]+tmp.second,presolve(tmp.first,x);
}
inline void solve(int x,int fa){
	for(auto tmp:q[x]){
		int k=tmp.first,id=tmp.second;
		ans[id]=max(Tree::dist(k,T.t[1].u.first)+T.t[1].u.second,Tree::dist(k,T.t[1].v.first)+T.t[1].v.second);
	}
	for(auto tmp:e2[x]){
		int v=tmp.first,w=tmp.second;
		if(v==fa) continue;
		T.update(1,1,n,1,n,w);
		T.update(1,1,n,dfn[v],dfn[v]+siz[v]-1,-w-w);
		solve(v,x);
		T.update(1,1,n,1,n,-w);
		T.update(1,1,n,dfn[v],dfn[v]+siz[v]-1,w+w);
	}
}
int main(){
	read(n,m);
	for(int i=1,u,v,w;i<n;i++)
		read(u,v,w),e1[u].emplace_back(v,w),e1[v].emplace_back(u,w);
	for(int i=1,u,v,w;i<n;i++)
		read(u,v,w),e2[u].emplace_back(v,w),e2[v].emplace_back(u,w);
	for(int i=1,a,b;i<=m;i++)
		read(a,b),q[b].emplace_back(a,i);
	Tree::prework(),dfs(1,0),presolve(1,0),T.build(1,1,n),solve(1,0);;
	for(int i=1;i<=m;i++) put('\n',ans[i]);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 30244kb

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: 32656kb

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: 1ms
memory: 29280kb

input:

2 1
1 2 1
1 2 1
1 2

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: -100
Wrong Answer
time: 50ms
memory: 35564kb

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:

630535121020
633346637051
526422418851
629633456938
490822408927
640192550410
636940349746
586353253749
657200938977
812777232105
664684495472
741221791381
747110265685
768201251200
553522242296
528667626061
615498838288
683988699100
550536360382
737358714679
657284031960
675032157167
712196139134
6...

result:

wrong answer 1st numbers differ - expected: '647838384844', found: '630535121020'