제출 #961685

#제출 시각아이디문제언어결과실행 시간메모리
961685LudisseyTwo Currencies (JOI23_currencies)C++14
0 / 100
259 ms17096 KiB
#include <bits/stdc++.h> #define int long long #define sz(a) (int)a.size() #define all(a) a.begin(), a.end() const int LOG=20; using namespace std; struct FenwickTree { vector<int> bit; vector<int> bitc; int n; FenwickTree(int _n) { this->n = _n; bit.assign(_n,0); bitc.assign(_n,0); } int sum(int idx) { int ret = 0; for (++idx; idx > 0; idx -= idx & -idx) ret += bit[idx]; return ret; } void range_add(int l, int r, int val){ add(l, val); add(r + 1, -val); } void add(int idx, int val){ for (++idx; idx < n; idx += idx & -idx) bit[idx] += val; } }; signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int n,m,q; cin >> n >> m >> q; vector<vector<pair<int,int>>> sum(n); vector<pair<int,int>> v; vector<int> ed; for (int i = 0; i < n-1; i++) { int a,b; cin >> a >> b; ed.push_back(min(a-1,b-1)); } for (int i = 0; i < m; i++){ int p,c; cin >> p >> c; v.push_back({c,ed[p-1]}); } sort(v.begin(),v.end()); for (int i = 0; i < m; i++){ sum[v[i].second].push_back({v[i].first,i}); } int square=sqrt(m*2); int squareN=sqrt(n*2); vector<int> qrs(m); vector<vector<pair<pair<pair<int,int>,pair<int,int>>,int>>> queries((n/squareN)+1); vector<vector<int>> coins((n/square)+1); vector<vector<int>> qrt((m/square)+1); vector<int> qrtSUM((m/square)+1); vector<int> qrtCOUNT((m/square)+1); vector<int> ans(q); for (int Q = 0; Q < q; Q++) { int s,t,x,y; cin >> s >> t >> x >> y; if(s>t) swap(s,t); queries[(s/squareN)].push_back({{{t-1, s-1},{x,y}},Q}); coins[(s/squareN)].push_back(y); } for (int i = 0; i < sz(queries); i++) { sort(all(queries[i])); sort(all(coins[i])); } for (int i = 0; i < sz(qrt); i++) qrt[i].resize(square); int l=-1,r=-1; int totalCOUNT=0; for (int i = 0; i < sz(queries); i++){ for (int j = 0; j < sz(queries[i]); j++){ int s=queries[i][j].first.first.second,t=queries[i][j].first.first.first,x=queries[i][j].first.second.first,y=queries[i][j].first.second.second,qr=queries[i][j].second; if(r==-1) { r=s; l=s; } for (int k = r; k < t; k++) { for (auto u : sum[k]) { qrtSUM[(u.second/square)]+=u.first; qrtCOUNT[(u.second/square)]++; totalCOUNT++; qrt[(u.second/square)][(u.second%square)]=u.first; } } for (int k = t; k < r; k++) { for (auto u : sum[k]) { qrtSUM[(u.second/square)]-=u.first; qrtCOUNT[(u.second/square)]--; totalCOUNT--; qrt[(u.second/square)][(u.second%square)]=0; } } for (int k = l; k < s; k++) { for (auto u : sum[k]) { qrtSUM[(u.second/square)]-=u.first; qrtCOUNT[(u.second/square)]--; totalCOUNT--; qrt[(u.second/square)][(u.second%square)]=0; } } for (int k = s; k < l; k++) { for (auto u : sum[k]) { qrtSUM[(u.second/square)]+=u.first; qrtCOUNT[(u.second/square)]++; totalCOUNT++; qrt[(u.second/square)][(u.second%square)]=u.first; } } int cqrt=0; int csum=0; int cnt=0; while(cqrt<sz(qrtSUM)){ csum+=qrtSUM[cqrt]; if(csum>y) break; cnt+=qrtCOUNT[cqrt]; cqrt++; } if(cqrt==sz(qrtSUM)) { ans[qr]=x; } else { csum-=qrtSUM[cqrt]; int _i=0; while(_i<sz(qrt[cqrt])) { if(qrt[cqrt][_i]>0) cnt++; csum+=qrt[cqrt][_i]; if(csum>y) { cnt--; break; } _i++; } ans[qr]=max(-1LL, x-(totalCOUNT-cnt)); } l=s; r=t; } } for (int i = 0; i < sz(ans); i++) cout << ans[i] << "\n"; return 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...