Submission #888664

#TimeUsernameProblemLanguageResultExecution timeMemory
888664vjudge1Two Currencies (JOI23_currencies)C++17
0 / 100
68 ms51604 KiB
#include <bits/stdc++.h> using namespace std; #define int long long #define ff first #define ss second #define all(a) a.begin(), a.end() const int mod = 998244353; const int N = 1e5; int n, m, q; vector<int > g[N+10]; vector< pair<int, int> > check, edge; int depth[N+10], sub[N+10]; int up[N+10][18], sum[N+10][18]; int tin[N+10], tout[N+10]; vector<int> val[N+10]; int bigchild[N+10], pos[N+10], chain[N+10]; int timer = 1; void dfs(int v, int par){ tin[v] = timer++; up[v][0] = par; for(int to : g[v]){ if(to == par) continue; depth[to] = depth[v] + 1; dfs(to, v); sub[v]+= sub[to]; if(!bigchild[v] or sub[bigchild[v]] < sub[to]){ bigchild[v] = to; } } sub[v] += 1, tout[v] = timer; } int upper(int p, int a){ return (tin[p] <= tin[a] && tout[p] >= tout[a]); } int lca(int a, int b){ if(depth[b] < depth[a]) swap(a, b); int k = depth[b] - depth[a]; for(int i = 0;i < 18; i++){ if(k & (1<<i)) b = up[b][i]; } if(a == b) return a; for(int i = 17; i >= 0; i--){ if(up[b][i] != up[a][i]){ a = up[a][i]; } } return up[a][0]; } void dfs2(int v, int par, int head){ pos[v] = timer++; chain[v] = head; if(bigchild[v]){ dfs2(bigchild[v], v, head); } for(int to : g[v]){ if(to == par || to == bigchild[v]) continue; dfs2(to, v, to); } } int query(int a, int b){ if(chain[a] > chain[b]) swap(a, b); int sum = 0; while(chain[a] != chain[b]){ if(chain[a] > chain[b]) swap(a, b); int l =pos[chain[b]], r = pos[b]; //sum+= get(l, r, 1, 1, n); b = up[chain[b]][0]; } if(chain[a] > chain[b]) swap(a, b); int l = pos[a], r = pos[b]; //sum+= get(l, r, 1, 1, n); return sum; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m >> q; for(int i = 1;i < n; i++){ int a, b; cin >> a >> b; edge.push_back({a, b}); g[a].push_back(b); g[b].push_back(a); } dfs(1, 1); timer = 1; dfs2(1, 1, 1); int coins = 0; for(int i = 0;i < m; i++){ int p, c; cin >> p >> c; check.push_back({p, c}); int a = edge[p-1].ff, b = edge[p-1].ss; if(depth[a] < depth[b]) swap(a, b); sum[a][0] += 1; val[a].push_back(c); coins = c; } for(int j = 1;j < 18; j++){ for(int i = 1;i <= n; i++){ up[i][j] = up[up[i][j-1]][j-1]; sum[i][j] = sum[i][j-1] + sum[up[i][j-1]][j-1]; } } if(max({n, m, q}) <= 2000){ while(q--){ int s, t; cin >> s >> t; int x, y; cin >> x >> y; int lc = lca(s, t); vector<pair<int, int> > vec; while(s != lc){ for(int x : val[s]) vec.push_back({x, 1}); s = up[s][0]; } while(t != lc){ for(int x : val[t]) vec.push_back({x, 1}); t = up[t][0]; } sort(all(vec), [&](auto A, auto B){ return A.ff < B.ff; }); for(auto [silver, gold] : vec){ // cout << silver << ' ' << gold << ", "; if(y >= silver){ y-= silver; }else{ x-= gold; } } //cout << '\n'; cout << max(-1LL, x) << '\n'; } return 0; } return 0; }

Compilation message (stderr)

currencies.cpp: In function 'long long int query(long long int, long long int)':
currencies.cpp:78:8: warning: unused variable 'l' [-Wunused-variable]
   78 |    int l =pos[chain[b]], r = pos[b];
      |        ^
currencies.cpp:78:26: warning: unused variable 'r' [-Wunused-variable]
   78 |    int l =pos[chain[b]], r = pos[b];
      |                          ^
currencies.cpp:83:6: warning: unused variable 'l' [-Wunused-variable]
   83 |  int l = pos[a], r = pos[b];
      |      ^
currencies.cpp:83:18: warning: unused variable 'r' [-Wunused-variable]
   83 |  int l = pos[a], r = pos[b];
      |                  ^
currencies.cpp: In function 'int main()':
currencies.cpp:105:6: warning: variable 'coins' set but not used [-Wunused-but-set-variable]
  105 |  int coins = 0;
      |      ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...