제출 #921132

#제출 시각아이디문제언어결과실행 시간메모리
921132edogawa_somethingTwo Currencies (JOI23_currencies)C++17
0 / 100
25 ms30556 KiB
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vii; typedef pair<ll,ll> pii; #define F first #define S second #define pb push_back #define all(v) v.begin(),v.end() #define pow poww const ll M=1e5+10; const ll inf=2e18; const ll mod=998244353; const ll mmod=1e9+7; const ll sz=(1<<18); const ll MM=5e7; ll pow(ll x,ll y){ ll res=1; if(y<0) return 1; x%=mod; while(y>0){ if((y&1)) res*=x,res%=mod; x*=x,x%=mod; y=(y>>1); } return res; } ll n,m,q,tin[M],tout[M],sparse[M][20],val[M],root[M],vroot[M],curtime,t; vii v[M]; map<ll,vii>vv; pii edge[M]; struct pst{ ll ct=0,l[MM],r[MM],val[MM],cnt[MM]; ll np(ll x,ll y){ l[++ct]=x,r[ct]=y; val[ct]=val[l[ct]]+val[r[ct]]; cnt[ct]=cnt[l[ct]]+cnt[r[ct]]; return ct; } void build(ll lx=0,ll rx=sz){ if(rx-lx==1) return; ll mid=((lx+rx)>>1); build(lx,mid); ll lef=ct; build(mid,rx); ll righ=ct; np(lef,righ); } void update(ll i,ll v,ll x,ll lx=0,ll rx=sz){ if(rx-lx==1){ val[++ct]=val[x],cnt[ct]=cnt[x]; val[ct]+=v; if(v<0) cnt[ct]--; else cnt[ct]++; return; } ll mid=((lx+rx)>>1); if(i<mid){ update(i,v,l[x],lx,mid); np(ct,r[x]); } else{ update(i,v,r[x],mid,rx); np(l[x],ct); } } ll query(ll x,ll rr,ll lx=0,ll rx=sz){ if(rx<=rr) return val[x]; else if(lx>=rr) return 0; ll mid=((lx+rx)>>1); return query(l[x],rr,lx,mid)+query(r[x],rr,mid,rx); } ll qcnt(ll x,ll rr,ll lx=0,ll rx=sz){ if(rx<=rr) return cnt[x]; else if(lx>=rr) return 0; ll mid=((lx+rx)>>1); return qcnt(l[x],rr,lx,mid)+qcnt(r[x],rr,mid,rx); } }se; void dfs(ll x=1){ tin[x]=t++; for(auto it:v[x]){ if(it==sparse[x][0]) continue; sparse[it][0]=x; val[it]+=val[x]; dfs(it); } tout[x]=t++; } void build(){ sparse[1][0]=1; dfs(); for(int bit=1;bit<=18;bit++){ for(int i=1;i<=n;i++) sparse[i][bit]=sparse[sparse[i][bit-1]][bit-1]; } } ll least_common(ll x,ll y){ if(tin[y]<tin[x]&&tout[y]>tout[x]) return y; if(tin[x]<tin[y]&&tout[x]>tout[y]) return x; for(int bit=18;bit>=0;bit--){ if(tin[sparse[x][bit]]<tin[y]&&tout[sparse[x][bit]]>tout[y]) continue; x=sparse[x][bit]; } x=sparse[x][0]; return x; } int main(){ ios_base::sync_with_stdio(0),cin.tie(0); ll TC=1; //cin>>TC; while(TC--){ cin>>n>>m>>q; for(int i=0;i<n-1;i++){ ll x,y; cin>>x>>y; edge[i+1]={x,y}; v[x].pb(y),v[y].pb(x); } ll vi=0; build(); for(int i=0;i<m;i++){ ll x,y; cin>>x>>y; pii p=edge[x]; if(tin[p.F]<tin[p.S]) val[p.S]++,vv[y].pb(p.S); else val[p.F]++,vv[y].pb(p.F); vi=y; } t=0; build(); se.build(); root[0]=se.ct; for(auto it:vv){ for(auto itt:it.S){ se.update(tin[itt],it.F,se.ct); se.update(tout[itt],-it.F,se.ct); } root[++curtime]=se.ct; vroot[curtime]=it.F; } while(q--){ ll l=0,r=curtime,mid; ll s,t,g,y; cin>>s>>t>>g>>y; ll lc=least_common(s,t); while(l<=r){ mid=((l+r)>>1); if(se.query(root[mid],tin[s]+1)+se.query(root[mid],tin[t]+1)-se.query(root[mid],tin[lc]+1)*2<=y) l=mid+1; else r=mid-1; } if(l==0){ g-=val[s]+val[t]-val[lc]-val[sparse[lc][0]]-y/vroot[1]; if(g<0) g=-1; cout<<g<<'\n'; continue; } ll x=se.qcnt(root[l-1],tin[s]+1)+se.qcnt(root[l-1],tin[t]+1)-se.qcnt(root[l-1],tin[lc]+1)*2; y-=se.query(root[l-1],tin[s]+1)+se.query(root[l-1],tin[t]+1)-se.query(root[l-1],tin[lc]+1)*2; g-=val[s]+val[t]-val[lc]*2-x-y/vroot[l]; if(g<0) g=-1; cout<<g<<'\n'; } } return 0; } /* */

컴파일 시 표준 에러 (stderr) 메시지

currencies.cpp: In function 'int main()':
currencies.cpp:133:8: warning: variable 'vi' set but not used [-Wunused-but-set-variable]
  133 |     ll vi=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...