Submission #953913

#TimeUsernameProblemLanguageResultExecution timeMemory
953913amirhoseinfar1385Two Currencies (JOI23_currencies)C++17
100 / 100
892 ms71804 KiB
#include<bits/stdc++.h> using namespace std; const long long maxn=100000+10,lg=18; struct qu{ long long u,v,x,y,uv; }allq[maxn]; long long n,m,q,lca[lg][maxn],timea,kaf=(1<<18),high[maxn],low[maxn],mid[maxn],check[maxn],len[maxn],res[maxn]; vector<long long>adj[maxn]; vector<pair<long long,long long>>allc; pair<long long,long long>stf[maxn],heye[maxn]; struct segment{ long long seg[(1<<19)]; void clear(){ for(long long i=0;i<(1<<19);i++){ seg[i]=0; } } void upd(long long i,long long l,long long r,long long tl,long long tr,long long w){ if(l>r||l>tr||r<tl||tl>tr){ return ; } if(l>=tl&&r<=tr){ seg[i]+=w; return ; } long long m=(l+r)>>1; upd((i<<1),l,m,tl,tr,w); upd((i<<1)^1,m+1,r,tl,tr,w); return ; } long long pors(long long i){ i+=kaf; long long ret=0; while(i>0){ ret+=seg[i]; i>>=1; } return ret; } }seg; bool zird(long long u,long long v){ return stf[v].first<=stf[u].first&&stf[v].second>=stf[u].second; } void vorod(){ cin>>n>>m>>q; for(long long i=1;i<n;i++){ long long u,v; cin>>u>>v; adj[u].push_back(v); adj[v].push_back(u); heye[i]=make_pair(u,v); } allc.resize(m); for(long long i=0;i<m;i++){ cin>>allc[i].second>>allc[i].first; } for(long long i=0;i<q;i++){ cin>>allq[i].u>>allq[i].v>>allq[i].y>>allq[i].x; } } void dfs(long long u,long long par=-1){ timea++; stf[u].first=timea; if(par!=-1){ sort(adj[u].begin(),adj[u].end()); adj[u].erase(lower_bound(adj[u].begin(),adj[u].end(),par)); } for(auto x:adj[u]){ lca[0][x]=u; len[x]=len[u]+1; dfs(x,u); } stf[u].second=timea; } void callca(){ for(long long i=1;i<lg;i++){ for(long long j=1;j<=n;j++){ lca[i][j]=lca[i-1][lca[i-1][j]]; } } } long long getlca(long long u,long long v){ if(zird(u,v)){ return v; } if(zird(v,u)){ return u; } for(long long i=lg-1;i>=0;i--){ if(lca[i][u]!=0&&zird(v,lca[i][u])==0){ u=lca[i][u]; } } return lca[0][u]; } void pre(){ sort(allc.begin(),allc.end()); dfs(1); callca(); for(long long i=0;i<m;i++){ long long te=allc[i].second; if(len[heye[te].first]<len[heye[te].second]){ allc[i].second=heye[te].second; }else{ allc[i].second=heye[te].first; } } for(long long i=0;i<q;i++){ allq[i].uv=getlca(allq[i].u,allq[i].v); } for(long long i=0;i<q;i++){ low[i]=0; high[i]=maxn-2; } } void checkasl(long long ind){ long long w=seg.pors(stf[allq[ind].u].first)+seg.pors(stf[allq[ind].v].first)-2*seg.pors(stf[allq[ind].uv].first); check[ind]=(w<=allq[ind].x); } vector<long long>allm[maxn]; void calcheck(){ for(long long i=0;i<q;i++){ allm[mid[i]].push_back(i); } seg.clear(); for(long long i=0;i<=m;i++){ for(auto x:allm[i]){ checkasl(x); } if(i<m){ auto x=allc[i]; seg.upd(1,0,kaf-1,stf[x.second].first,stf[x.second].second,x.first); } } for(long long i=0;i<q;i++){ allm[mid[i]].clear(); } } void solve(){ for(long long asd=0;asd<20;asd++){ for(long long i=0;i<q;i++){ //cout<<i<<endl; mid[i]=(high[i]+low[i])/2; check[i]=0; } calcheck(); for(long long i=0;i<q;i++){ if(check[i]){ low[i]=mid[i]; }else{ high[i]=mid[i]; } } } } void khor(){ seg.clear(); for(long long i=0;i<q;i++){ allm[low[i]].push_back(i); } for(long long i=m;i>=0;i--){ if(i<m) seg.upd(1,0,kaf-1,stf[allc[i].second].first,stf[allc[i].second].second,1); for(auto x:allm[i]){ res[x]=seg.pors(stf[allq[x].u].first)+seg.pors(stf[allq[x].v].first)-2*seg.pors(stf[allq[x].uv].first); } } for(long long i=0;i<q;i++){ long long ret=max(-1ll,allq[i].y-res[i]); cout<<ret<<"\n"; } } int main(){ ios::sync_with_stdio(0); cin.tie(0); //cout.tie(0); // freopen("inp.txt","r",stdin); vorod(); pre(); solve(); khor(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...