Submission #729895

# Submission time Handle Problem Language Result Execution time Memory
729895 2023-04-24T18:58:29 Z shadow_sami Two Currencies (JOI23_currencies) C++17
28 / 100
287 ms 68540 KB
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll,ll> pi;
typedef map<ll,ll> mi;
typedef long double ld;
typedef vector<ld> vd;
typedef vector<vector<ld>> vvd;
typedef pair<ld,ld> pd;
#define ff first
#define ss second
#define srt(a) sort(a.begin(),a.end());
#define fip(k, n) for (ll i = k; i < n; i++)
#define fjp(k, n) for (ll j = k; j < n; j++)
#define fin(k, n) for (ll i = k; i >= n; i--)
#define fjn(k, n) for (ll j = k; j >= n; j--)
#define fp(k, n, m) for (ll k = n; k < m; k++)
#define fn(k, n, m) for (ll k = n; k >= m; k--)
#define ordered_set tree<pi, null_type,less< pi >, rb_tree_tag,tree_order_statistics_node_update>
#define totalOne(n) __builtin_popcount(n)
#define backZero(n) __builtin_ctzll(n)
#define frontZero(n) __builtin_clzll(n)
#define fx(k) for ( auto x : k )
#define test ll t;cin >> t;while (t--)
#define nli "\n"

// ==========================(debug)============================================================================================== //

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _printn(x); cerr << nli;
#else
#define debug(x)
#endif

void _printn(ll x){ cerr<<x<<" "; }
void _printn(int x){ cerr<<x<<" "; }
void _printn(ld x){ cerr<<x<<" "; }
void _printn(double x){ cerr<<x<<" "; }
void _printn(string x){ cerr<<x<<" "; }
void _printn(char x){ cerr<<x<<" "; }
void _printn(bool x){ cerr<<x<<" "; }
template<class T,class V>void _printn(pair<T,V> vv);
template<class T> void _printn(vector<T> vv);
template<class T> void _printn(set<T> vv);
template<class T,class V> void _printn(map<T,V> vv);
template<class T> void _printn(multiset<T> vv);
template<class T,class V>void _printn(pair<T,V> vv){ cerr<<"( ";_printn(vv.ff);cerr<<",";_printn(vv.ss);cerr<<")";}
template<class T> void _printn(vector<T> vv){ cerr<<"[ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"]"; };
template<class T> void _printn(set<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T> void _printn(multiset<T> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };
template<class T,class V> void _printn(map<T,V> vv){ cerr<<"{ "; for(auto xx:vv){ _printn(xx);cerr<<" "; } cerr<<"}"; };

// ==========================(debug)============================================================================================== //

ll n,m,tp,tp2,res,cnt,sum,tptp,ans;
const ll mx = 2e5+5;
const ll mod = 1e9+7;

// ==========================(MOD)=============================================================================================== //

ll mod_add(ll aa,ll bb){ return ((aa%mod)+(bb%mod))%mod; }
ll mod_minus(ll aa,ll bb){ return (((aa%mod)-(bb%mod))+10*mod)%mod; }
ll mod_mul(ll aa,ll bb){ return ((aa%mod)*(bb%mod))%mod; }
ll mod_power(ll aa,ll bb){ aa%=mod; ll empowered = 1; bb%=mod-1; while(bb > 0){ if(bb & 1) empowered = mod_mul(empowered,aa); bb = bb >> 1; aa = mod_mul(aa,aa); } return empowered; }
ll mod_divi(ll aa,ll bb){ aa=mod_mul(aa,mod_power(bb,mod-2)); return aa; }

// ==========================(MOD)=============================================================================================== //

bool f = false;
vector<pi> adj[mx];
ll parent[mx][30],q;
ll dp[mx],dep[mx];
ll path[mx];

void dfs(ll u,ll par,ll de){
	dep[u] = de;
	fx(adj[u]){
		if(x.ff!=par){
			parent[x.ff][0] = u;
			dp[x.ff] = dp[u] + path[x.ss];
			dfs(x.ff,u,de+1);			
		}
	}
	return;
}

ll lca(ll aa,ll bb){
	if(dep[aa] < dep[bb])
		swap(aa,bb);
	ll dis = dep[aa]-dep[bb];
	fin(29,0)
		if(dis & (1<<i)){
			aa = parent[aa][i];
		}
	if(aa==bb)
		return aa;
	fin(29,0)
		if(parent[aa][i] != parent[bb][i]){
			aa = parent[aa][i];
			bb = parent[bb][i];
		}
	return parent[aa][0];
}
ll sr,de,a,b,g,s;

void initializer(){
	fip(0,mx){
		adj[i].clear();
		path[i] = dep[i] = dp[i] = 0;
		fjp(0,30)
			parent[i][j] = 0;
	}
	return;
}

int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    // #ifndef ONLINE_JUDGE
    //     freopen("input1.txt", "r", stdin);
    //     freopen("output1.txt", "w", stdout);
    //     freopen("error1.txt", "w", stderr);
    // #endif // ONLINE_JUDGE

    	initializer();
        cin>>n>>m>>q;
        fip(0,n-1){
        	cin>>sr>>de;
        	adj[sr].push_back({de,i});
        	adj[de].push_back({sr,i});
        }
        fip(0,m){
        	cin>>sr>>tp;
        	sr--;
        	path[sr]++;
        }
        dp[1] = 0;
        parent[1][0] = 1;
        dfs(1,1,0);
        fjp(1,30)
        	fip(1,n+1){
        		parent[i][j] = parent[parent[i][j-1]][j-1];
        	}
        while(q--){
        	cin>>a>>b>>g>>s;
        	tptp = lca(a,b);
        	tp2 = dp[a] + dp[b] - 2 * dp[tptp];
        	// debug(dp[a]);
        	// debug(dp[b]);
        	// debug(dp[tptp]);
        	// debug(tp2);
        	// debug(s);
        	// debug(tp);
        	cnt = s / tp;
        	// debug(cnt);
        	tp2 = max(0ll,tp2-cnt);
        	// debug(tp2);
        	tp2 = g - tp2;
        	// debug(g);
        	// debug(tp2);
        	if(tp2<0)
        		tp2 = -1;
        	cout<<tp2<<nli;
        }
        
    // cerr << "Time elapsed: " << setprecision(6) << 1000.0 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 56660 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 25 ms 56652 KB Output is correct
2 Correct 29 ms 56740 KB Output is correct
3 Correct 28 ms 56764 KB Output is correct
4 Correct 35 ms 56764 KB Output is correct
5 Correct 207 ms 62044 KB Output is correct
6 Correct 158 ms 60480 KB Output is correct
7 Correct 206 ms 61316 KB Output is correct
8 Correct 177 ms 61380 KB Output is correct
9 Correct 153 ms 61384 KB Output is correct
10 Correct 210 ms 62036 KB Output is correct
11 Correct 231 ms 62072 KB Output is correct
12 Correct 216 ms 61972 KB Output is correct
13 Correct 207 ms 61952 KB Output is correct
14 Correct 198 ms 62000 KB Output is correct
15 Correct 253 ms 68048 KB Output is correct
16 Correct 250 ms 68540 KB Output is correct
17 Correct 262 ms 67860 KB Output is correct
18 Correct 254 ms 62412 KB Output is correct
19 Correct 287 ms 62404 KB Output is correct
20 Correct 238 ms 62388 KB Output is correct
21 Correct 196 ms 61584 KB Output is correct
22 Correct 158 ms 61568 KB Output is correct
23 Correct 171 ms 61508 KB Output is correct
24 Correct 167 ms 61680 KB Output is correct
25 Correct 183 ms 62088 KB Output is correct
26 Correct 202 ms 62312 KB Output is correct
27 Correct 186 ms 62240 KB Output is correct
28 Correct 159 ms 61980 KB Output is correct
29 Correct 174 ms 62040 KB Output is correct
30 Correct 175 ms 61972 KB Output is correct
31 Correct 172 ms 62084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 56588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 28 ms 56660 KB Output isn't correct
2 Halted 0 ms 0 KB -