Submission #956351

#TimeUsernameProblemLanguageResultExecution timeMemory
956351LCJLYTwo Currencies (JOI23_currencies)C++14
100 / 100
2932 ms286872 KiB
#include <bits/stdc++.h>
using namespace std;
 
//#define int long long 
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << "  " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << "  " << j << " " << #i << "  " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
typedef pair<pii,pii>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

inline pii combine(pii a, pii b){
	return {a.first+b.first,a.second+b.second};
}

struct node{
	int s,e,m;
	pii v;
	node *l,*r;
	node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v({0,0}){
		if(s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	node(node *u){
		s=u->s,e=u->e,m=u->m,v=u->v,l=u->l,r=u->r;
	}
	
	void upd(int x, pii y){
		if(s==e){
			v.first+=y.first;
			v.second+=y.second;
			return;
		}
		if(x<=m){
			l=new node(l);
			l->upd(x,y);
		}
		else{
			r=new node(r);
			r->upd(x,y);
		}
		v=combine(l->v,r->v);
	}
	
	pii query(int x, int y){
		if(x<=s&&y>=e){
			return v;
		}
		if(y<=m) return l->query(x,y);
		if(x>m) return r->query(x,y);
		return combine(l->query(x,m),r->query(m+1,y));
	}
}*root[100005];

vector<int>adj[100005];
int in[100005];
int out[100005];
int d[100005];
int two[25][100005];
int ptr=0;

void dfs(int index, int par){
	in[index]=++ptr;
	for(int x=0;x<20;x++){
		two[x+1][index]=two[x][two[x][index]];
	}
	for(auto it:adj[index]){
		if(it==par) continue;
		two[0][it]=index;
		d[it]=d[index]+1;
		dfs(it,index);
	}
	out[index]=++ptr;
}

int lca(int a, int b){
	if(d[a]>d[b]) swap(a,b);
	int diff=d[b]-d[a];
	for(int x=0;x<19;x++){
		if(diff&(1<<x)){
			b=two[x][b];
		}
	}
	if(a==b) return a;
	for(int x=19;x>=0;x--){
		if(two[x][a]!=two[x][b]){
			a=two[x][a];
			b=two[x][b];
		}
	}
	return two[0][a];
} 

void solve(){
	int n,m,q;
	cin >> n >> m >> q;
	
	int temp,temp2;
	pii edge[n-1];
	for(int x=0;x<n-1;x++){
		cin >> temp >> temp2;
		adj[temp].push_back(temp2);
		adj[temp2].push_back(temp);
		edge[x]={temp,temp2};
	}
	
	dfs(1,-1);
	
	pii check[m]; //cost edge
	for(int x=0;x<m;x++){
		cin >> check[x].second >> check[x].first;
	}
	
	sort(check,check+m);
	
	root[0]=new node(0,200005);
	for(int x=0;x<m;x++){
		root[x+1]=new node(root[x]);
		int road=check[x].second-1;
		int index;
		if(d[edge[road].first] > d[edge[road].second]){
			index=edge[road].first;
		}
		else index=edge[road].second;
		//show(index,index);
		root[x+1]->upd(in[index],{check[x].first,1});
		root[x+1]->upd(out[index],{-check[x].first,-1});
		
		//for(int y=0;y<2*n;y++){
			//cout << root[x+1]->query(y,y).second << " ";
		//}
		//cout << endl;
	}
	
	int from,to;
	long long a,b; //gold silver
	for(int x=0;x<q;x++){
		cin >> from >> to >> a >> b;
		
		int hold=lca(from,to);
		
		int l=0;
		int r=m;
		pii best={0,0};
		int mid;
		
		while(l<=r){
			mid=(l+r)/2;
			pii que=root[mid]->query(0,in[from]);
			pii que2=root[mid]->query(0,in[to]);
			pii que3=root[mid]->query(0,in[hold]);
			//show(mid,mid);
			//show3(que.first,que.first,que2.first,que2.first,que3.first,que3.first);
			//show(total,que.first+que2.first-que3.first*2,b,b)
			if(que.first+que2.first-que3.first*2<=b){
				best={mid,que.second+que2.second-que3.second*2};
				l=mid+1;
			}
			else r=mid-1;
		}
		
		//show3(a,root[m]->query(0,in[from]),b,root[m]->query(0,in[to]),c,root[m]->query(0,in[hold]));
		long long len=root[m]->query(0,in[from]).second+root[m]->query(0,in[to]).second-2*root[m]->query(0,in[hold]).second - best.second;
		//show(best.second,best.second);
		//show(len,len);
		if(a-len<0){
			cout << -1 << "\n";
		}
		else cout << a-len << "\n";
	}
	
}
 
int32_t main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//freopen("in.txt","w",stdout);
	//cin >> t;
	while(t--){
		solve();
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...