Submission #767922

#TimeUsernameProblemLanguageResultExecution timeMemory
767922Dan4LifeTwo Currencies (JOI23_currencies)C++17
100 / 100
646 ms80084 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; using ar = array<ll,2>; #define pb push_back #define all(a) begin(a),end(a) const int mxN = (int)1e5+10; const int mxN2 = mxN*135; const int mxLg = 17; ar seg[mxN2]; int n, m, q, IND=1; int jmp[mxLg][mxN]; vector<ar> adj[mxN]; vector<ll> v[mxN], w; int L[mxN2], R[mxN2]; int lev[mxN], ro[mxN], orig[mxN*2]; int newChild(int p, int v){ int p2 = ++IND; L[p2] = L[p], R[p2] = R[p]; seg[p2]={seg[p][0]+v,seg[p][1]+1}; return p2; } int newPar(int l, int r){ int p = ++IND; L[p] = l, R[p] = r; for(int i : {0,1}) seg[p][i] = seg[l][i]+seg[r][i]; return p; } int upd(int x, int v, int p, int l=1, int r=mxN*2){ if(!p) return p; if(l==r) return newChild(p,v); int mid = (l+r)/2; if(!L[p]) L[p]=++IND; if(!R[p]) R[p]=++IND; if(x<=mid) return newPar(upd(x,v,L[p],l,mid),R[p]); return newPar(L[p],upd(x,v,R[p],mid+1,r)); } ll query(ll x, int p1, int p2, int p3, int l=1, int r=mxN*2){ if(l==r) return min(seg[p1][1]+seg[p2][1]-2*seg[p3][1], orig[l]?(x/orig[l]):0ll); int mid = (l+r)/2; ll ltot = seg[L[p1]][0]+seg[L[p2]][0]-2*seg[L[p3]][0]; int lntot = seg[L[p1]][1]+seg[L[p2]][1]-2*seg[L[p3]][1]; if(ltot>=x) return query(x,L[p1],L[p2],L[p3],l,mid); return query(x-ltot,R[p1],R[p2],R[p3],mid+1,r)+lntot; } void dfs(int s, int p){ jmp[0][s]=p; lev[s] = lev[p]+1; for(auto [u,w] : adj[s]){ if(u==p) continue; ro[u] = ro[s]; for(auto x : v[w]) ro[u] = upd(x,orig[x],ro[u]); dfs(u,s); } } int getPath(int x, int k){ for(int i = mxLg-1; i>=0; i--) if((k>>i)&1) x=jmp[i][x]; return x; } int lca(int a, int b){ if(a==b) return a; if(jmp[0][a]==jmp[0][b]) return jmp[0][a]; if(lev[a]>lev[b]) swap(a,b); b = getPath(b,lev[b]-lev[a]); for(int i = mxLg-1; i>=0; i--) if(jmp[i][a]!=jmp[i][b] and jmp[i][a]) a=jmp[i][a], b=jmp[i][b]; return lca(a,b); } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> q; int p, c, a, b; ro[1]=1; for(int i = 1; i < n; i++) cin >> a >> b, adj[a].pb({b,i}), adj[b].pb({a,i}); for(int i = 1; i <= m; i++) cin >> p >> c, v[p].pb(c), w.pb(c); sort(all(w)); w.erase(unique(all(w)),end(w)); for(int i = 1; i < n; i++) for(auto &u : v[i]) u=lower_bound(all(w),u)-begin(w)+1; for(int cnt = 1; auto u : w) orig[cnt++]=u; dfs(1,0); for(int i = 1; i < mxLg; i++) for(int j = 1; j <= n; j++) jmp[i][j] = jmp[i-1][jmp[i-1][j]]; for(int i = 0; i < q; i++){ ll s,t,x,y,z; cin >> s >> t >> x >> y; z = ro[lca(s,t)]; s = ro[s], t = ro[t]; int num = seg[s][1]+seg[t][1]-2*seg[z][1]; cout << max(-1ll, x-(num-query(y,s,t,z))) << "\n"; } }

Compilation message (stderr)

currencies.cpp: In function 'void dfs(int, int)':
currencies.cpp:54:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   54 |         if(u==p) continue; ro[u] = ro[s];
      |         ^~
currencies.cpp:54:28: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   54 |         if(u==p) continue; ro[u] = ro[s];
      |                            ^~
currencies.cpp: In function 'int main()':
currencies.cpp:88:22: warning: range-based 'for' loops with initializer only available with '-std=c++2a' or '-std=gnu++2a'
   88 |     for(int cnt = 1; auto u : w) orig[cnt++]=u;
      |                      ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...