Submission #783344

#TimeUsernameProblemLanguageResultExecution timeMemory
783344fuad27Two Currencies (JOI23_currencies)C++17
100 / 100
978 ms36920 KiB
#pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt") #include<bits/stdc++.h> using namespace std; const int N=100'010,L=20; vector<pair<int,int>> g[N]; int up[N][L]; int tin[N], tout[N],depth[N]; int tinn[N]; int tim=1; void dfs(int at, int p) { up[at][0]=p; for(int i = 1;i<L;i++) { up[at][i]=up[up[at][i-1]][i-1]; } for(auto to:g[at]) { if(to.first==p)continue; tinn[to.first]=tim; tin[to.second]=tim++; depth[to.first]=depth[at]+1; dfs(to.first,at); tout[to.second]=tim++; } } int lca(int a, int b) { if(depth[a]<depth[b])swap(a,b); int k=depth[a]-depth[b]; for(int i = L-1;i>=0;i--) { if(k&(1<<i))a=up[a][i]; } if(a==b)return a; for(int j = L-1;j>=0;j--) { if(up[a][j]!=up[b][j]) { a=up[a][j]; b=up[b][j]; } } return up[a][0]; } template<typename T> struct BIT { int N_; vector<T> F_; BIT(int n) { N_=n; F_=vector<T>(n+1); } void update(int at, T val) { for(;at<=N_;at+=at&-at)F_[at]+=val; } T query(int l, int r) { if(l!=1) { return query(1,r)-query(1,l); } T res=0; for(;r>0;r-=r&-r)res+=F_[r]; return res; } }; struct query { int u, v, lc; long long x,y; }; signed main () { cin.tie(0)->sync_with_stdio(0); for(int i = 0;i<N;i++) for(int j = 0;j<L;j++) up[i][j]=1; int n, m, q; cin >> n >> m >> q; for(int i = 0;i<n-1;i++) { int a, b; cin >> a >> b; g[a].push_back({b,i}); g[b].push_back({a,i}); } vector<pair<long long, int>> v(m); for(int i = 0;i<m;i++) { cin >> v[i].second >> v[i].first; v[i].second--; } sort(v.begin(),v.end()); tinn[1]=tim++; dfs(1,1); query c[q]; for(int i=0;i<q;i++){ cin >> c[i].u >> c[i].v >> c[i].x >> c[i].y; c[i].lc=lca(c[i].u,c[i].v); } int l[q], r[q],res[q]; for(int i = 0;i<q;i++) { l[i]=0, r[i]=m; res[i]=-1; } BIT<long long> b3(2*n+10); for(int i = 0;i<m;i++) { b3.update(tin[v[i].second],1); b3.update(tout[v[i].second],-1); } while(1) { vector<pair<int,int>> queries; for(int i = 0;i<q;i++) { if(l[i]>r[i])continue; int tm=(l[i]+r[i])/2; queries.push_back({tm,i}); } if(queries.size()==0)break; sort(queries.begin(),queries.end()); BIT<long long> b1(2*n+10),b2(2*n+10); int pt=0; for(int i = 0;i<queries.size();i++) { while(pt<queries[i].first) { b1.update(tin[v[pt].second],v[pt].first); b1.update(tout[v[pt].second],-v[pt].first); b2.update(tin[v[pt].second],1); b2.update(tout[v[pt].second],-1); pt++; } int j=queries[i].second; int dst=b3.query(tinn[c[j].lc], tinn[c[j].u])+b3.query(tinn[c[j].lc],tinn[c[j].v]); if(b1.query(tinn[c[j].lc],tinn[c[j].u])+b1.query(tinn[c[j].lc],tinn[c[j].v])<=c[j].y) { l[j]=queries[i].first+1; if(dst-(b2.query(tinn[c[j].lc],tinn[c[j].u])+b2.query(tinn[c[j].lc],tinn[c[j].v]))<=c[j].x) { res[j]=c[j].x-(dst-(b2.query(tinn[c[j].lc],tinn[c[j].u])+b2.query(tinn[c[j].lc],tinn[c[j].v]))); } } else { r[j]=queries[i].first-1; } } } for(int i = 0;i<q;i++) { cout << res[i] << "\n"; } return 0; }

Compilation message (stderr)

currencies.cpp: In function 'int main()':
currencies.cpp:111:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |   for(int i = 0;i<queries.size();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...