Submission #888818

#TimeUsernameProblemLanguageResultExecution timeMemory
888818vjudge1Two Currencies (JOI23_currencies)C++17
10 / 100
5058 ms60932 KiB
#include <bits/stdc++.h> #define int long long #define ff first #define ss second #define pb push_back #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() using namespace std; void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);} int pow(int a,int b,int m){int ans=1;while(b){if(b&1){ans=(ans*a)%m;}b>>=1;a=(a*a)%m;}return ans;} int binpow(int a,int b){int ans=1;while(b){if(b&1){ans=(ans*a);}b>>=1;a=(a*a);}return ans;} const int N = 2e5 + 10; vector <int> g[N], cost[N]; int up[N][30]; int tin[N], tout[N], ch[N], par[N], notch[N]; int timer; void dfs(int v, int p){ par[v] = p; tin[v] = timer++; up[v][0] = p; for (int i=1; i<=25; ++i) up[v][i] = up[up[v][i-1]][i-1]; for(auto to : g[v]){ if(to == p)continue; dfs(to, v); } tout[v] = timer++; } bool upper (int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; } bool upper2 (int a, int b) { return tin[a] < tin[b] && tout[a] > tout[b]; } int lca (int a, int b) { if (upper (a, b)) return a; if (upper (b, a)) return b; for (int i=25; i>=0; --i) if (! upper (up[a][i], b)) a = up[a][i]; return up[a][0]; } main(){ ios :: sync_with_stdio(0); cin.tie(0); int n, m, q; cin >> n >> m >> q; vector <pair <int, int> > road = {{-1, -1}}; for(int i = 0; i < n - 1; i++){ int a, b; cin >> a >> b; road.pb({a, b}); g[a].pb(b); g[b].pb(a); } dfs(1, 1); for(int i = 1; i <= n; i++){ int x = road[i].ff, y = road[i].ss; if(par[x] == y){ ch[i] = x; notch[i] = y; }else{ ch[i] = y; notch[i] = x; } } set <int> st; vector <int> pref = {0}; int mr = 0; for(int i = 0; i < m; i++){ int ind, silv; cin >> ind >> silv; pref.pb(silv); st.insert(ind); mr = ch[ind]; cost[ch[ind]].pb(silv); } sort(all(pref)); for(int i = 1; i <= m; i++){ pref[i] += pref[i - 1]; } pref.pb(2e18); if((int)st.size() == 1){ for(int i = 0; i < q; i++){ int f, s, gold, silv; cin >> f >> s >> gold >> silv; int lc = lca(f, s); //cout << mr <<"====" << endl; if(upper2(lc, mr) && upper(mr, f) || upper(mr, s) && upper2(lc, mr)){ int l = -1, r = gold; while(l + 1 < r){ int mid = (l + r) >> 1; if(pref[m - mid] <= silv) r = mid; else l = mid; } cout << l << endl; }else{ cout << gold << endl; } } return 0; } //cout << "====" << endl; for(int i = 0; i < q; i++){ int f, s, gold, silv; cin >> f >> s >> gold >> silv; vector <int> c; int lc = lca(f, s); while(s != lc){ for(auto x : cost[s]){ c.pb(x); } s= par[s]; } while(f != lc){ for(auto x : cost[f]){ c.pb(x); } f = par[f]; } sort(all(c)); for(auto x : c){ if(silv >= x){ silv -= x; }else{ gold--; } } if(gold < 0){ cout << "-1\n"; }else{ cout << gold << endl; } } }

Compilation message (stderr)

currencies.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   53 | main(){
      | ^~~~
currencies.cpp: In function 'int main()':
currencies.cpp:99:22: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   99 |    if(upper2(lc, mr) && upper(mr, f) || upper(mr, s) && upper2(lc, mr)){
      |       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
currencies.cpp: In function 'void fp(std::string)':
currencies.cpp:12:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:12:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | void fp(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...