Submission #897037

#TimeUsernameProblemLanguageResultExecution timeMemory
897037ReLiceTwo Currencies (JOI23_currencies)C++14
100 / 100
755 ms77736 KiB
#include <bits/stdc++.h> #define ll long long #define str string #define ins insert #define ld long double #define pb push_back #define pf push_front #define pof pop_front() #define pob pop_back() #define lb lower_bound #define ub upper_bound #define endl "\n" #define fr first #define sc second #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define sz size() #define vll vector<ll> #define bc back() #define ar array using namespace std; template <class _T> bool chmin(_T &x, const _T &y){ if (x>y)x=y; return x>y; } template <class _T> bool chmax(_T &x, const _T &y){ if (x<y)x=y; return x<y; } //void fre(string s){freopen((s+".in").c_str(),"r",stdin);freopen((s+".out").c_str(),"w",stdout);} void start(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const ll inf=1e18+7; const ll mod=1e9+7; const ll N=3e5+5; const ld eps=1e-9; struct fen{ ll n; vll f; fen(ll m){ n=m; f.resize(n+1,0); } void upd(ll a,ll x){ for(ll i=a;i<=n;i+=i&-i){ f[i]+=x; } } ll pref(ll a){ ll res=0; for(ll i=a;i>0;i-=i&-i) res+=f[i]; return res; } }; vector<vll> g(N); ll p[N][20],sum[N][20]; ll tin[N],tout[N],d[N]; ll tiktak; void dfs(ll v){ tin[v]=++tiktak; for(auto i : g[v]){ if(i==p[v][0]) continue; p[i][0]=v; d[i]=d[v]+1; dfs(i); } tout[v]=tiktak; } ll lca(ll a,ll b){ if(d[a]<d[b]) swap(a,b); for(ll i=19;i>=0;i--){ if(d[a]-d[b]>=(1ll<<i)) a=p[a][i]; } if(a==b) return a; for(ll i=19;i>=0;i--){ if(p[a][i]!=p[b][i]){ a=p[a][i]; b=p[b][i]; } } return p[a][0]; } ll get(ll a,ll d){ ll res=0; for(ll i=0;i<20;i++){ if(d&(1ll<<i)){ res+=sum[a][i]; a=p[a][i]; } } return res; } void solve(){ ll i,j; ll a=1,b; ll n,m,q; cin>>n>>m>>q; vector<ar<ll,2>> edge; for(i=1;i<n;i++){ cin>>a>>b; g[a].pb(b); g[b].pb(a); edge.pb({a,b}); } dfs(1); vector<ar<ll,2>> add; for(i=0;i<m;i++){ ll p,c; cin>>p>>c; auto [a,b]=edge[p-1]; if(d[a]<d[b]) swap(a,b); add.pb({a,c}); sum[a][0]++; } sort(all(add),[&](auto a,auto b){ return a[1]<b[1]; }); for(j=1;j<20;j++){ for(i=1;i<=n;i++){ p[i][j]=p[p[i][j-1]][j-1]; sum[i][j]=sum[i][j-1]+sum[p[i][j-1]][j-1]; } } vector<ar<ll,4>> qs(q); vll path,lc; vector<ar<ll,2>> rng(q,{0,m+1}); for(auto &a : qs){ for(i=0;i<4;i++) cin>>a[i]; lc.pb(lca(a[0],a[1])); ll ch=get(a[0],d[a[0]]-d[lc.bc])+get(a[1],d[a[1]]-d[lc.bc]); path.pb(ch); } vll ans(q); for(i=0;i<q;i++){ ans[i]=qs[i][2]-path[i]; } while(true){ bool flag=true; fen f1(n+2),f2(n+2); vector<vll> mid(m+5); for(i=0;i<q;i++){ if(rng[i][1]-rng[i][0]>1){ ll m=(rng[i][0]+rng[i][1])/2; mid[m].pb(i); flag=false; } } if(flag) break; for(i=0;i<=m;i++){ for(auto it : mid[i]){ auto [s,t,g,sil]=qs[it]; ll Lc=lc[it],ch=path[it]; ll sum=f1.pref(tin[s])+f1.pref(tin[t])-2*f1.pref(tin[Lc]); ll cnt=f2.pref(tin[s])+f2.pref(tin[t])-2*f2.pref(tin[Lc]); ch-=cnt; if(sum<=sil){ chmax(ans[it],g-ch); rng[it][0]=i; }else rng[it][1]=i; } if(i<m){ auto [a,c]=add[i]; f1.upd(tin[a],c); f1.upd(tout[a]+1,-c); f2.upd(tin[a],1); f2.upd(tout[a]+1,-1); } } } for(i=0;i<q;i++)cout<<(ans[i]<0 ? -1 : ans[i])<<endl; } signed main(){ start(); ll t=1; //cin>>t; while(t--) solve(); return 0; }

Compilation message (stderr)

currencies.cpp: In function 'void solve()':
currencies.cpp:115:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  115 |         auto [a,b]=edge[p-1];
      |              ^
currencies.cpp:156:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  156 |                 auto [s,t,g,sil]=qs[it];
      |                      ^
currencies.cpp:167:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  167 |                 auto [a,c]=add[i];
      |                      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...