QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#125073#5439. Meet in the MiddleTadijaSebezWA 38ms45296kbC++144.0kb2023-07-16 00:49:512023-07-16 00:49:52

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-16 00:49:52]
  • 评测
  • 测评结果:WA
  • 用时:38ms
  • 内存:45296kb
  • [2023-07-16 00:49:51]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll long long

const int N=200050;
const int L=19;

namespace Tree1{

vector<pair<int,int>> E[N];
int rmq[N][L];
int node[N],lid[N],rid[N],tid;
ll dep[N];
int lgs[N];

void AddEdge(int u,int v,int w){
	E[u].pb({v,w});
	E[v].pb({u,w});
}

void DFS(int u,int p){
	lid[u]=++tid;
	node[tid]=u;
	for(auto e:E[u]){
		int v=e.first;
		if(v!=p){
			dep[v]=dep[u]+e.second;
			DFS(v,u);
			node[++tid]=u;
		}
	}
	rid[u]=tid;
}

int combine(int u,int v){
	return dep[u]<dep[v]?u:v;
}

void Build(){
	DFS(1,0);
	for(int i=2;i<=tid;i++){
		lgs[i]=lgs[i>>1]+1;
	}
	for(int i=1;i<=tid;i++){
		rmq[i][0]=node[i];
	}
	for(int j=1;j<L;j++){
		for(int i=1;i+(1<<j)-1<=tid;i++){
			rmq[i][j]=combine(rmq[i][j-1],rmq[i+(1<<(j-1))][j-1]);
		}
	}
}

int LCA(int u,int v){
	u=lid[u];
	v=lid[v];
	if(u>v)swap(u,v);
	int k=lgs[v-u+1];
	return combine(rmq[u][k],rmq[v-(1<<k)+1][k]);
}

ll Dist(int u,int v){
	return dep[u]+dep[v]-2*dep[LCA(u,v)];
}

}

struct Node{
	pair<int,ll> nodes[2];

	Node(){}

	Node(int u,ll d){
		nodes[0]={u,d};
		nodes[1]={u,d};
	}

	Node(pair<int,ll> a, pair<int,ll> b){
		nodes[0]=a;
		nodes[1]=b;
	}

	void upd(ll x){
		nodes[0].second+=x;
		nodes[1].second+=x;
	}

	ll Get() const {
		if(nodes[0].first==nodes[1].first){
			return 0;
		}
		return Tree1::Dist(nodes[0].first,nodes[1].first)+nodes[0].second+nodes[1].second;
	}

	ll Query(int u){
		return ::max(Tree1::Dist(nodes[0].first,u)+nodes[0].second, Tree1::Dist(nodes[1].first,u)+nodes[1].second);
	}

	bool operator < (const Node& other) const {
		return Get()<other.Get();
	}

	static Node max(const Node& a, const Node& b){
		return a<b?b:a;
	}

	Node operator + (const Node& other) const {
		Node ans=max(other,*this);
		for(int i=0;i<2;i++){
			for(int j=0;j<2;j++){
				ans=max(ans,Node(nodes[i],other.nodes[j]));
			}
		}
		return ans;
	}
};

const int M=2*N;
int ls[M],rs[M],tsz,root;
Node node[M];
ll lzy[M];

void pull(int c){
	node[c]=node[ls[c]]+node[rs[c]];
}

void Build(int&c,int ss,int se,int*node,ll*dep){
	c=++tsz;
	if(ss==se){
		::node[c]=Node(node[ss],dep[node[ss]]);
		return;
	}
	int mid=ss+se>>1;
	Build(ls[c],ss,mid,node,dep);
	Build(rs[c],mid+1,se,node,dep);
	pull(c);
}

void upd(int c,ll x){
	node[c].upd(x);
	lzy[c]+=x;
}

void push(int c){
	if(lzy[c]!=0){
		upd(ls[c],lzy[c]);
		upd(rs[c],lzy[c]);
		lzy[c]=0;
	}
}

void Add(int c,int ss,int se,int qs,int qe,ll x){
	if(qs>qe||qs>se||ss>qe)return;
	if(qs<=ss&&qe>=se){
		upd(c,x);
		return;
	}
	int mid=ss+se>>1;
	push(c);
	Add(ls[c],ss,mid,qs,qe,x);
	Add(rs[c],mid+1,se,qs,qe,x);
	pull(c);
}

const int Q=500050;
vector<pair<int,int>> Qs[N];
ll ans[Q];

namespace Tree2{

vector<pair<int,int>> E[N];
int node[N],lid[N],rid[N],tid;
ll dep[N];

void AddEdge(int u,int v,int w){
	E[u].pb({v,w});
	E[v].pb({u,w});
}

void DFS(int u,int p){
	lid[u]=++tid;
	node[tid]=u;
	for(auto e:E[u]){
		int v=e.first;
		if(v!=p){
			dep[v]=dep[u]+e.second;
			DFS(v,u);
		}
	}
	rid[u]=tid;
}

void Solve(int u,int p){
	for(auto q:Qs[u]){
		ans[q.second]=::node[root].Query(q.first);
	}
	for(auto e:E[u]){
		int v=e.first;
		if(v!=p){
			Add(root,1,tid,1,tid,e.second);
			Add(root,1,tid,lid[u],rid[u],-2*e.second);
			Solve(v,u);
			Add(root,1,tid,1,tid,-e.second);
			Add(root,1,tid,lid[u],rid[u],2*e.second);
		}
	}
}

void Solve(){
	DFS(1,0);
	Build(root,1,tid,node,dep);
	Solve(1,0);
}

}

int main(){
	int n,q;
	scanf("%i %i",&n,&q);
	for(int i=1;i<n;i++){
		int u,v,w;
		scanf("%i %i %i",&u,&v,&w);
		Tree1::AddEdge(u,v,w);
	}
	for(int i=1;i<n;i++){
		int u,v,w;
		scanf("%i %i %i",&u,&v,&w);
		Tree2::AddEdge(u,v,w);
	}
	for(int i=1;i<=q;i++){
		int u,v;
		scanf("%i %i",&u,&v);
		Qs[v].push_back({u,i});
	}
	Tree1::Build();
	Tree2::Solve();
	for(int i=1;i<=q;i++){
		printf("%lld\n",ans[i]);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 42840kb

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

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

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: 38ms
memory: 44476kb

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:

645994505608
626497024080
514231172608
635222513902
472503610500
643999036724
639693980022
573561735860
644038806374
803832682370
672526670750
734721277820
744820046345
763735624420
551656005924
524899945523
610330950418
687706367806
547317423586
726768669064
653091475024
666719222866
701575393972
6...

result:

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