Submission #829631

# Submission time Handle Problem Language Result Execution time Memory
829631 2023-08-18T13:35:49 Z CSQ31 Two Currencies (JOI23_currencies) C++17
100 / 100
1893 ms 146268 KB
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXNODE = 2e7+5;
int lf[MAXNODE],rg[MAXNODE],cnt[MAXNODE],ndcnt = 0;
ll sum[MAXNODE];
int new_node(){
	ndcnt++;
	return ndcnt;
}
int new_node(int v){
	ndcnt++;
	sum[ndcnt] = sum[v];
	cnt[ndcnt] = cnt[v];
	lf[ndcnt] = lf[v];
	rg[ndcnt] = rg[v];	
	return ndcnt;
}
//get the double euler tour array
//set cost(i) at tin(i) and -cost(i) at tout(i)
int upd(int v,int pos,int l,int r,ll val){
	int u = new_node(v);
	if(l==r){
		sum[u]+=val;
		if(val>0)cnt[u]++;
		if(val<0)cnt[u]--;
		return u;
	}
	int tm = (l+r)/2;
	if(pos<=tm){
		if(!lf[v])lf[v] = new_node();
		lf[u] = upd(lf[v],pos,l,tm,val);
	}else{
		if(!rg[v])rg[v] = new_node();
		rg[u] = upd(rg[v],pos,tm+1,r,val);
	}
	
	sum[u] = cnt[u] = 0;
	if(lf[u]){
		sum[u]+=sum[lf[u]];
		cnt[u]+=cnt[lf[u]];
	}
	if(rg[u]){
		sum[u]+=sum[rg[u]];
		cnt[u]+=cnt[rg[u]];
	}
	return u;
}
ll query(int v,int l,int r,int tl,int tr){
	if(!v)return 0;
	if(l==tl && r==tr)return sum[v];
	int tm = (tl+tr)/2;
	
	if(r<=tm)return query(lf[v],l,r,tl,tm);
	else if(l>tm)return query(rg[v],l,r,tm+1,tr);
	else{
		return query(lf[v],l,tm,tl,tm)+query(rg[v],tm+1,r,tm+1,tr);
	}
	
}
ll query2(int v,int l,int r,int tl,int tr){
	if(!v)return 0;
	if(l==tl && r==tr)return cnt[v];
	int tm = (tl+tr)/2;
	
	if(r<=tm)return query2(lf[v],l,r,tl,tm);
	else if(l>tm)return query2(rg[v],l,r,tm+1,tr);
	else{
		return query2(lf[v],l,tm,tl,tm)+query2(rg[v],tm+1,r,tm+1,tr);
	}
	
}
///////persistent seg
const int MAXN = 5e5+5;
vector<int>adj[MAXN];
int tin[MAXN],tout[MAXN],dep[MAXN],par[19][MAXN],timer = 0;
int cost[MAXN],ett[2*MAXN];
void dfs(int v,int u){
	tin[v] = ++timer;
	ett[tin[v]] = v;
	for(int x:adj[v]){
	    if(x == u)continue;	
	    par[0][x] = v;
	    dep[x] = dep[v]+1;
	    for(int i=1;i<=18;i++){
			par[i][x] = par[i-1][par[i-1][x]];
		}
		dfs(x,v);
	}
	tout[v] = ++timer;
	ett[tout[v]] = -v;
}
bool ancestor(int v,int u){return(tin[v] >= tin[u] && tout[u] >= tout[v]);}//is u an ancestor of v?
int lca(int v,int u){
	if(dep[v] > dep[u])swap(u,v);
	int dist = dep[u]-dep[v];
	for(int i=0;dist;i++,dist>>=1){
		if(dist&1)u = par[i][u];
	}
	if(u == v)return u;
	for(int i=18;i>=0;i--){
		if(par[i][v] != par[i][u]){
			u = par[i][u];
			v = par[i][v];
		}
	}
	return par[0][v];
}

int main()
{
	owo
	int n,m,q;
	cin>>n>>m>>q;
	vector<pii>ed(n),C(m);
	for(int i=1;i<n;i++)cin>>ed[i].fi>>ed[i].se;
	for(int i=0;i<m;i++)cin>>C[i].se>>C[i].fi;
	
	vii p(n);
	sort(all(C));
	int N = n;
	for(int i=0;i<m;i++){
		N++; //label this guy as N
		p[C[i].se].pb(N);
		cost[N] = C[i].fi;
	}
	for(int i=1;i<n;i++){
		int a = ed[i].fi;
		int b = ed[i].se;
		for(int x:p[i]){
			adj[a].pb(x);
			adj[x].pb(a);
			a = x;
		}
		adj[a].pb(b);
		adj[b].pb(a);
	}
	dfs(1,-1);
	vector<int>root(2*N+1);
	for(int i=1;i<=2*N;i++){
		root[i] = new_node(root[i-1]);
		
		int v = abs(ett[i]);
		if(v > n){
			if(ett[i]>0)root[i] = upd(root[i],v,1,N,cost[v]);
			else        root[i] = upd(root[i],v,1,N,-cost[v]);
		}
	}
	while(q--){
		int s,t,x;
		ll y;
		cin>>s>>t>>x>>y;
		int v = lca(s,t);
		int l = 1,r = N;
		//cout<<s<<" "<<t<<" "<<v<<'\n';
		while(r>=l){
			int tm = (l+r)/2;
			ll a = query(root[tin[v]],1,tm,1,N);
			ll b = query(root[tin[s]],1,tm,1,N);
			ll c = query(root[tin[t]],1,tm,1,N);
			if(b+c-2*a > y)r = tm-1;
			else l = tm+1;
		}
		int gol = cnt[root[tin[s]]] + cnt[root[tin[t]]] - 2*cnt[root[tin[v]]];
		gol -= query2(root[tin[s]],1,r,1,N) + query2(root[tin[t]],1,r,1,N);
		gol += 2*query2(root[tin[v]],1,r,1,N);
		if(x>=gol)cout<<x-gol<<'\n';
		else cout<<-1<<'\n';
	}
	
	
	
	
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 7 ms 12244 KB Output is correct
4 Correct 6 ms 12172 KB Output is correct
5 Correct 13 ms 14048 KB Output is correct
6 Correct 16 ms 14000 KB Output is correct
7 Correct 12 ms 13568 KB Output is correct
8 Correct 17 ms 14164 KB Output is correct
9 Correct 16 ms 14192 KB Output is correct
10 Correct 15 ms 14164 KB Output is correct
11 Correct 16 ms 14136 KB Output is correct
12 Correct 19 ms 14188 KB Output is correct
13 Correct 13 ms 14468 KB Output is correct
14 Correct 13 ms 14404 KB Output is correct
15 Correct 15 ms 14304 KB Output is correct
16 Correct 15 ms 14276 KB Output is correct
17 Correct 20 ms 14244 KB Output is correct
18 Correct 18 ms 14280 KB Output is correct
19 Correct 13 ms 14208 KB Output is correct
20 Correct 12 ms 14164 KB Output is correct
21 Correct 15 ms 14200 KB Output is correct
22 Correct 17 ms 14204 KB Output is correct
23 Correct 13 ms 14160 KB Output is correct
24 Correct 16 ms 14164 KB Output is correct
25 Correct 16 ms 14264 KB Output is correct
26 Correct 15 ms 14164 KB Output is correct
27 Correct 11 ms 14196 KB Output is correct
28 Correct 12 ms 14164 KB Output is correct
29 Correct 12 ms 14140 KB Output is correct
30 Correct 17 ms 14156 KB Output is correct
31 Correct 14 ms 14192 KB Output is correct
32 Correct 16 ms 14196 KB Output is correct
33 Correct 14 ms 14420 KB Output is correct
34 Correct 15 ms 14500 KB Output is correct
35 Correct 18 ms 14500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12212 KB Output is correct
2 Correct 13 ms 14164 KB Output is correct
3 Correct 13 ms 14136 KB Output is correct
4 Correct 12 ms 14164 KB Output is correct
5 Correct 1113 ms 120204 KB Output is correct
6 Correct 1487 ms 104736 KB Output is correct
7 Correct 1342 ms 92340 KB Output is correct
8 Correct 1026 ms 90224 KB Output is correct
9 Correct 940 ms 95308 KB Output is correct
10 Correct 1757 ms 130324 KB Output is correct
11 Correct 1652 ms 130424 KB Output is correct
12 Correct 1598 ms 130324 KB Output is correct
13 Correct 1506 ms 130536 KB Output is correct
14 Correct 1556 ms 130412 KB Output is correct
15 Correct 1620 ms 144832 KB Output is correct
16 Correct 1560 ms 145928 KB Output is correct
17 Correct 1519 ms 144056 KB Output is correct
18 Correct 1757 ms 130872 KB Output is correct
19 Correct 1839 ms 130656 KB Output is correct
20 Correct 1780 ms 130764 KB Output is correct
21 Correct 630 ms 130940 KB Output is correct
22 Correct 623 ms 130904 KB Output is correct
23 Correct 696 ms 130752 KB Output is correct
24 Correct 721 ms 130772 KB Output is correct
25 Correct 967 ms 134136 KB Output is correct
26 Correct 983 ms 134116 KB Output is correct
27 Correct 1025 ms 133872 KB Output is correct
28 Correct 583 ms 130492 KB Output is correct
29 Correct 598 ms 130356 KB Output is correct
30 Correct 760 ms 130324 KB Output is correct
31 Correct 753 ms 130424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 12244 KB Output is correct
2 Correct 13 ms 14428 KB Output is correct
3 Correct 13 ms 14500 KB Output is correct
4 Correct 19 ms 14420 KB Output is correct
5 Correct 839 ms 107220 KB Output is correct
6 Correct 827 ms 91972 KB Output is correct
7 Correct 1122 ms 123416 KB Output is correct
8 Correct 1487 ms 146268 KB Output is correct
9 Correct 1528 ms 146088 KB Output is correct
10 Correct 1524 ms 146072 KB Output is correct
11 Correct 1201 ms 145616 KB Output is correct
12 Correct 1165 ms 145688 KB Output is correct
13 Correct 1186 ms 145672 KB Output is correct
14 Correct 803 ms 146188 KB Output is correct
15 Correct 840 ms 146036 KB Output is correct
16 Correct 958 ms 146164 KB Output is correct
17 Correct 915 ms 146148 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 12244 KB Output is correct
2 Correct 6 ms 12244 KB Output is correct
3 Correct 7 ms 12244 KB Output is correct
4 Correct 6 ms 12172 KB Output is correct
5 Correct 13 ms 14048 KB Output is correct
6 Correct 16 ms 14000 KB Output is correct
7 Correct 12 ms 13568 KB Output is correct
8 Correct 17 ms 14164 KB Output is correct
9 Correct 16 ms 14192 KB Output is correct
10 Correct 15 ms 14164 KB Output is correct
11 Correct 16 ms 14136 KB Output is correct
12 Correct 19 ms 14188 KB Output is correct
13 Correct 13 ms 14468 KB Output is correct
14 Correct 13 ms 14404 KB Output is correct
15 Correct 15 ms 14304 KB Output is correct
16 Correct 15 ms 14276 KB Output is correct
17 Correct 20 ms 14244 KB Output is correct
18 Correct 18 ms 14280 KB Output is correct
19 Correct 13 ms 14208 KB Output is correct
20 Correct 12 ms 14164 KB Output is correct
21 Correct 15 ms 14200 KB Output is correct
22 Correct 17 ms 14204 KB Output is correct
23 Correct 13 ms 14160 KB Output is correct
24 Correct 16 ms 14164 KB Output is correct
25 Correct 16 ms 14264 KB Output is correct
26 Correct 15 ms 14164 KB Output is correct
27 Correct 11 ms 14196 KB Output is correct
28 Correct 12 ms 14164 KB Output is correct
29 Correct 12 ms 14140 KB Output is correct
30 Correct 17 ms 14156 KB Output is correct
31 Correct 14 ms 14192 KB Output is correct
32 Correct 16 ms 14196 KB Output is correct
33 Correct 14 ms 14420 KB Output is correct
34 Correct 15 ms 14500 KB Output is correct
35 Correct 18 ms 14500 KB Output is correct
36 Correct 7 ms 12212 KB Output is correct
37 Correct 13 ms 14164 KB Output is correct
38 Correct 13 ms 14136 KB Output is correct
39 Correct 12 ms 14164 KB Output is correct
40 Correct 1113 ms 120204 KB Output is correct
41 Correct 1487 ms 104736 KB Output is correct
42 Correct 1342 ms 92340 KB Output is correct
43 Correct 1026 ms 90224 KB Output is correct
44 Correct 940 ms 95308 KB Output is correct
45 Correct 1757 ms 130324 KB Output is correct
46 Correct 1652 ms 130424 KB Output is correct
47 Correct 1598 ms 130324 KB Output is correct
48 Correct 1506 ms 130536 KB Output is correct
49 Correct 1556 ms 130412 KB Output is correct
50 Correct 1620 ms 144832 KB Output is correct
51 Correct 1560 ms 145928 KB Output is correct
52 Correct 1519 ms 144056 KB Output is correct
53 Correct 1757 ms 130872 KB Output is correct
54 Correct 1839 ms 130656 KB Output is correct
55 Correct 1780 ms 130764 KB Output is correct
56 Correct 630 ms 130940 KB Output is correct
57 Correct 623 ms 130904 KB Output is correct
58 Correct 696 ms 130752 KB Output is correct
59 Correct 721 ms 130772 KB Output is correct
60 Correct 967 ms 134136 KB Output is correct
61 Correct 983 ms 134116 KB Output is correct
62 Correct 1025 ms 133872 KB Output is correct
63 Correct 583 ms 130492 KB Output is correct
64 Correct 598 ms 130356 KB Output is correct
65 Correct 760 ms 130324 KB Output is correct
66 Correct 753 ms 130424 KB Output is correct
67 Correct 5 ms 12244 KB Output is correct
68 Correct 13 ms 14428 KB Output is correct
69 Correct 13 ms 14500 KB Output is correct
70 Correct 19 ms 14420 KB Output is correct
71 Correct 839 ms 107220 KB Output is correct
72 Correct 827 ms 91972 KB Output is correct
73 Correct 1122 ms 123416 KB Output is correct
74 Correct 1487 ms 146268 KB Output is correct
75 Correct 1528 ms 146088 KB Output is correct
76 Correct 1524 ms 146072 KB Output is correct
77 Correct 1201 ms 145616 KB Output is correct
78 Correct 1165 ms 145688 KB Output is correct
79 Correct 1186 ms 145672 KB Output is correct
80 Correct 803 ms 146188 KB Output is correct
81 Correct 840 ms 146036 KB Output is correct
82 Correct 958 ms 146164 KB Output is correct
83 Correct 915 ms 146148 KB Output is correct
84 Correct 899 ms 119780 KB Output is correct
85 Correct 1202 ms 103932 KB Output is correct
86 Correct 983 ms 78484 KB Output is correct
87 Correct 1622 ms 130624 KB Output is correct
88 Correct 1646 ms 130488 KB Output is correct
89 Correct 1573 ms 130328 KB Output is correct
90 Correct 1644 ms 130540 KB Output is correct
91 Correct 1645 ms 130420 KB Output is correct
92 Correct 1660 ms 140288 KB Output is correct
93 Correct 1682 ms 143552 KB Output is correct
94 Correct 1893 ms 130756 KB Output is correct
95 Correct 1883 ms 130756 KB Output is correct
96 Correct 1893 ms 130776 KB Output is correct
97 Correct 1879 ms 130648 KB Output is correct
98 Correct 1270 ms 130556 KB Output is correct
99 Correct 1307 ms 130628 KB Output is correct
100 Correct 1339 ms 130636 KB Output is correct
101 Correct 1297 ms 130636 KB Output is correct
102 Correct 1350 ms 133828 KB Output is correct
103 Correct 1285 ms 133908 KB Output is correct
104 Correct 1331 ms 133960 KB Output is correct
105 Correct 784 ms 130396 KB Output is correct
106 Correct 683 ms 130380 KB Output is correct
107 Correct 743 ms 130380 KB Output is correct
108 Correct 781 ms 130380 KB Output is correct