Submission #852689

# Submission time Handle Problem Language Result Execution time Memory
852689 2023-09-22T14:05:17 Z willychan Two Currencies (JOI23_currencies) C++14
100 / 100
901 ms 47324 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
//#include<bits/extc++.h>
//__gnu_pbds

int n,m,q;
struct Query{
	int a,b;
	int x,y;
	int ans;
};
struct request{
	int node;
	ll v;
};
const int N = 1e5+5;

vector<int> side[N];
vector<request> Req;
vector<Query> Que;
// tree stuff
pair<int,int> dep[N];
int father[N][20];
int t;
void dfs(int cur){
	dep[cur].first=++t;
	for(auto i : side[cur]){
		if(!dep[i].first){
			father[i][0] = cur;
			dfs(i);
		}
	}
	dep[cur].second=++t;
}

bool is_father_of(int a,int b){
	return (dep[b].first>=dep[a].first && dep[b].second<=dep[a].second);
}

int lca(int a,int b){
	if(is_father_of(a,b)) return a;
	if(is_father_of(b,a)) return b;
	for(int j=19;j>=0;j--){
		if(!is_father_of(father[b][j],a)) b = father[b][j];
	}
	return father[b][0];
}

//------------------------

//bit stuff
vector<ll> vsum;
vector<ll> cnt;


void add(ll v,int ind,vector<ll> &bit){
	while(ind<=2*n){
		bit[ind]+=v;
		ind+=(ind&-ind);
	}
}
ll query(int ind,vector<ll> &bit){
	ll result = 0;
	while(ind){
		result+=bit[ind];
		ind-=(ind&-ind);
	}
	return result;
}
//--------------------


// total binary search stuff
void do_thing(int l,int r,int mi){
	for(int i=l;i<r;i++){
		if(i<0) continue;
		add(mi*Req[i].v,dep[Req[i].node].first,vsum);
		add(-1*mi*Req[i].v,dep[Req[i].node].second,vsum);
		add(mi,dep[Req[i].node].first,cnt);
		add(-1*mi,dep[Req[i].node].second,cnt);
	}
}

void split(vector<int> &qry,vector<int> &lft,vector<int> &rgt,int k){
	for(auto i : qry){
		long long sum = 0;
		sum+=query(dep[Que[i].a].first,vsum);
		sum+=query(dep[Que[i].b].first,vsum);
		sum-=2*query(dep[lca(Que[i].a,Que[i].b)].first,vsum);
	//	cout<<i<<" "<<sum<<" "<<Que[i].y<<" "<<k<<" -split"<<(sum>Que[i].y)<<"\n";
		if(sum>Que[i].y) lft.push_back(i);
		else rgt.push_back(i);
	}
	vector<int>().swap(qry);
}

void total_BS(int l,int r,vector<int> &qry){
	if(r-l<=1){
		for(auto i : qry) Que[i].ans=l;
		return;
	}
	int mid = (l+r)/2;
	do_thing(l,mid,1);
	vector<int> lft,rgt;	
	split(qry,lft,rgt,mid);
	total_BS(mid,r,rgt);
	do_thing(l,mid,-1);
	total_BS(l,mid,lft);
}

//-----------------------------------



signed main(){
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m>>q;
	Que.resize(q);
	vsum.resize(2*n+1,0);
	cnt.resize(2*n+1,0);
	vector<pair<int,int> > es;
	for(int i=0;i<n-1;i++){
			int a,b;cin>>a>>b;	
			es.push_back({a,b});
			side[a].push_back(b);
			side[b].push_back(a);
	}
	for(int i=0;i<20;i++){
		father[1][i]=1;
	}
	dfs(1);
	for(int j=1;j<20;j++){
		for(int i=2;i<=n;i++){
			father[i][j] = father[father[i][j-1]][j-1];
		}
	}
	for(int i=0;i<m;i++){
		 int p;cin>>p;
		 p--;
		 int c;cin>>c;
		 if(dep[es[p].first]<dep[es[p].second]) swap(es[p].first,es[p].second);
		 Req.push_back({es[p].first,c});
	}
	sort(Req.begin(),Req.end(),[](const request &a,const request &b){return a.v<b.v;});
	/*
	cout<<endl;
	for(auto i : Req){
		cout<<i.node<<" "<<i.v<<endl;
	}
	cout<<endl;
	*/
	vector<int> tmp;		
	for(int i=0;i<q;i++){
		cin>>Que[i].a>>Que[i].b>>Que[i].x>>Que[i].y;
		tmp.push_back(i);
	}
	total_BS(0,m+1,tmp);
	int ttt = 0;	
	vector<int> anssort;
	for(int i=0;i<q;i++){
		anssort.push_back(i);
	}

	/*cout<<"\n";
	for(int i=0;i<q;i++){
		cout<<Que[i].ans<<"\n";
	}
	cout<<"\n\n\n-----\n";
	*/

	sort(anssort.begin(),anssort.end(),[&](const int a,const int b){return Que[a].ans<Que[b].ans;});
	vector<ll> realans(q,0);
	for(int i=0;i<q;i++){
		do_thing(ttt,Que[anssort[i]].ans,1);
		ttt = Que[anssort[i]].ans;
		realans[anssort[i]]+=Que[anssort[i]].x;
		realans[anssort[i]]+=query(dep[Que[anssort[i]].a].first,cnt);
		realans[anssort[i]]+=query(dep[Que[anssort[i]].b].first,cnt);
		realans[anssort[i]]-=2*query(dep[lca(Que[anssort[i]].a,Que[anssort[i]].b)].first,cnt);
	}
//	cout<<ttt<<endl;
	do_thing(ttt,m,1);
	for(int i=0;i<q;i++){
		realans[i]-=query(dep[Que[i].a].first,cnt);
		realans[i]-=query(dep[Que[i].b].first,cnt);
		realans[i]+=2*query(dep[lca(Que[i].a,Que[i].b)].first,cnt);
	}
	for(int i=0;i<q;i++){
		cout<<max(-1LL,realans[i])<<"\n";
	}
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5464 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 6 ms 7760 KB Output is correct
6 Correct 8 ms 8136 KB Output is correct
7 Correct 7 ms 8064 KB Output is correct
8 Correct 9 ms 8028 KB Output is correct
9 Correct 9 ms 8028 KB Output is correct
10 Correct 9 ms 8028 KB Output is correct
11 Correct 9 ms 8132 KB Output is correct
12 Correct 9 ms 8284 KB Output is correct
13 Correct 6 ms 8132 KB Output is correct
14 Correct 9 ms 8028 KB Output is correct
15 Correct 8 ms 8028 KB Output is correct
16 Correct 10 ms 8028 KB Output is correct
17 Correct 9 ms 8028 KB Output is correct
18 Correct 8 ms 8028 KB Output is correct
19 Correct 8 ms 8028 KB Output is correct
20 Correct 8 ms 8028 KB Output is correct
21 Correct 8 ms 8028 KB Output is correct
22 Correct 8 ms 8028 KB Output is correct
23 Correct 8 ms 8084 KB Output is correct
24 Correct 9 ms 8028 KB Output is correct
25 Correct 8 ms 8028 KB Output is correct
26 Correct 7 ms 8028 KB Output is correct
27 Correct 7 ms 8060 KB Output is correct
28 Correct 7 ms 8028 KB Output is correct
29 Correct 7 ms 8028 KB Output is correct
30 Correct 9 ms 8144 KB Output is correct
31 Correct 9 ms 8028 KB Output is correct
32 Correct 9 ms 8028 KB Output is correct
33 Correct 6 ms 8028 KB Output is correct
34 Correct 6 ms 8028 KB Output is correct
35 Correct 6 ms 8236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5720 KB Output is correct
2 Correct 9 ms 8064 KB Output is correct
3 Correct 9 ms 8132 KB Output is correct
4 Correct 9 ms 8028 KB Output is correct
5 Correct 508 ms 38456 KB Output is correct
6 Correct 685 ms 34432 KB Output is correct
7 Correct 587 ms 38608 KB Output is correct
8 Correct 438 ms 37056 KB Output is correct
9 Correct 447 ms 36668 KB Output is correct
10 Correct 745 ms 41840 KB Output is correct
11 Correct 720 ms 42404 KB Output is correct
12 Correct 703 ms 41956 KB Output is correct
13 Correct 710 ms 42028 KB Output is correct
14 Correct 744 ms 42396 KB Output is correct
15 Correct 545 ms 46948 KB Output is correct
16 Correct 443 ms 46924 KB Output is correct
17 Correct 543 ms 45612 KB Output is correct
18 Correct 882 ms 41700 KB Output is correct
19 Correct 867 ms 42404 KB Output is correct
20 Correct 901 ms 41632 KB Output is correct
21 Correct 604 ms 41452 KB Output is correct
22 Correct 653 ms 41904 KB Output is correct
23 Correct 636 ms 41652 KB Output is correct
24 Correct 624 ms 42052 KB Output is correct
25 Correct 617 ms 42052 KB Output is correct
26 Correct 626 ms 42804 KB Output is correct
27 Correct 626 ms 42848 KB Output is correct
28 Correct 477 ms 41276 KB Output is correct
29 Correct 455 ms 41792 KB Output is correct
30 Correct 516 ms 42296 KB Output is correct
31 Correct 516 ms 42248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 5468 KB Output is correct
2 Correct 6 ms 8028 KB Output is correct
3 Correct 6 ms 8064 KB Output is correct
4 Correct 6 ms 8028 KB Output is correct
5 Correct 267 ms 41996 KB Output is correct
6 Correct 268 ms 41644 KB Output is correct
7 Correct 330 ms 34884 KB Output is correct
8 Correct 402 ms 47172 KB Output is correct
9 Correct 405 ms 47324 KB Output is correct
10 Correct 450 ms 46560 KB Output is correct
11 Correct 361 ms 47028 KB Output is correct
12 Correct 385 ms 46412 KB Output is correct
13 Correct 357 ms 45968 KB Output is correct
14 Correct 333 ms 47188 KB Output is correct
15 Correct 310 ms 46356 KB Output is correct
16 Correct 359 ms 46768 KB Output is correct
17 Correct 334 ms 46884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 5464 KB Output is correct
2 Correct 1 ms 5468 KB Output is correct
3 Correct 1 ms 5468 KB Output is correct
4 Correct 1 ms 5468 KB Output is correct
5 Correct 6 ms 7760 KB Output is correct
6 Correct 8 ms 8136 KB Output is correct
7 Correct 7 ms 8064 KB Output is correct
8 Correct 9 ms 8028 KB Output is correct
9 Correct 9 ms 8028 KB Output is correct
10 Correct 9 ms 8028 KB Output is correct
11 Correct 9 ms 8132 KB Output is correct
12 Correct 9 ms 8284 KB Output is correct
13 Correct 6 ms 8132 KB Output is correct
14 Correct 9 ms 8028 KB Output is correct
15 Correct 8 ms 8028 KB Output is correct
16 Correct 10 ms 8028 KB Output is correct
17 Correct 9 ms 8028 KB Output is correct
18 Correct 8 ms 8028 KB Output is correct
19 Correct 8 ms 8028 KB Output is correct
20 Correct 8 ms 8028 KB Output is correct
21 Correct 8 ms 8028 KB Output is correct
22 Correct 8 ms 8028 KB Output is correct
23 Correct 8 ms 8084 KB Output is correct
24 Correct 9 ms 8028 KB Output is correct
25 Correct 8 ms 8028 KB Output is correct
26 Correct 7 ms 8028 KB Output is correct
27 Correct 7 ms 8060 KB Output is correct
28 Correct 7 ms 8028 KB Output is correct
29 Correct 7 ms 8028 KB Output is correct
30 Correct 9 ms 8144 KB Output is correct
31 Correct 9 ms 8028 KB Output is correct
32 Correct 9 ms 8028 KB Output is correct
33 Correct 6 ms 8028 KB Output is correct
34 Correct 6 ms 8028 KB Output is correct
35 Correct 6 ms 8236 KB Output is correct
36 Correct 1 ms 5720 KB Output is correct
37 Correct 9 ms 8064 KB Output is correct
38 Correct 9 ms 8132 KB Output is correct
39 Correct 9 ms 8028 KB Output is correct
40 Correct 508 ms 38456 KB Output is correct
41 Correct 685 ms 34432 KB Output is correct
42 Correct 587 ms 38608 KB Output is correct
43 Correct 438 ms 37056 KB Output is correct
44 Correct 447 ms 36668 KB Output is correct
45 Correct 745 ms 41840 KB Output is correct
46 Correct 720 ms 42404 KB Output is correct
47 Correct 703 ms 41956 KB Output is correct
48 Correct 710 ms 42028 KB Output is correct
49 Correct 744 ms 42396 KB Output is correct
50 Correct 545 ms 46948 KB Output is correct
51 Correct 443 ms 46924 KB Output is correct
52 Correct 543 ms 45612 KB Output is correct
53 Correct 882 ms 41700 KB Output is correct
54 Correct 867 ms 42404 KB Output is correct
55 Correct 901 ms 41632 KB Output is correct
56 Correct 604 ms 41452 KB Output is correct
57 Correct 653 ms 41904 KB Output is correct
58 Correct 636 ms 41652 KB Output is correct
59 Correct 624 ms 42052 KB Output is correct
60 Correct 617 ms 42052 KB Output is correct
61 Correct 626 ms 42804 KB Output is correct
62 Correct 626 ms 42848 KB Output is correct
63 Correct 477 ms 41276 KB Output is correct
64 Correct 455 ms 41792 KB Output is correct
65 Correct 516 ms 42296 KB Output is correct
66 Correct 516 ms 42248 KB Output is correct
67 Correct 1 ms 5468 KB Output is correct
68 Correct 6 ms 8028 KB Output is correct
69 Correct 6 ms 8064 KB Output is correct
70 Correct 6 ms 8028 KB Output is correct
71 Correct 267 ms 41996 KB Output is correct
72 Correct 268 ms 41644 KB Output is correct
73 Correct 330 ms 34884 KB Output is correct
74 Correct 402 ms 47172 KB Output is correct
75 Correct 405 ms 47324 KB Output is correct
76 Correct 450 ms 46560 KB Output is correct
77 Correct 361 ms 47028 KB Output is correct
78 Correct 385 ms 46412 KB Output is correct
79 Correct 357 ms 45968 KB Output is correct
80 Correct 333 ms 47188 KB Output is correct
81 Correct 310 ms 46356 KB Output is correct
82 Correct 359 ms 46768 KB Output is correct
83 Correct 334 ms 46884 KB Output is correct
84 Correct 561 ms 37492 KB Output is correct
85 Correct 534 ms 28960 KB Output is correct
86 Correct 452 ms 28152 KB Output is correct
87 Correct 693 ms 41804 KB Output is correct
88 Correct 718 ms 42324 KB Output is correct
89 Correct 714 ms 42336 KB Output is correct
90 Correct 706 ms 42316 KB Output is correct
91 Correct 670 ms 42300 KB Output is correct
92 Correct 652 ms 45496 KB Output is correct
93 Correct 598 ms 45692 KB Output is correct
94 Correct 888 ms 42064 KB Output is correct
95 Correct 858 ms 42456 KB Output is correct
96 Correct 865 ms 41612 KB Output is correct
97 Correct 844 ms 41644 KB Output is correct
98 Correct 675 ms 41644 KB Output is correct
99 Correct 670 ms 41284 KB Output is correct
100 Correct 680 ms 42168 KB Output is correct
101 Correct 643 ms 41908 KB Output is correct
102 Correct 604 ms 42820 KB Output is correct
103 Correct 633 ms 42596 KB Output is correct
104 Correct 603 ms 42328 KB Output is correct
105 Correct 524 ms 42300 KB Output is correct
106 Correct 488 ms 41364 KB Output is correct
107 Correct 513 ms 42140 KB Output is correct
108 Correct 498 ms 42124 KB Output is correct