#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;
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]});
}
vector<int> prefix(sz(v));
for (int i = 0; i < sz(v); i++)
{
if(i>0) prefix[i]+=prefix[i-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/squareN)+1);
vector<vector<int>> qrt((m/square)+1);
vector<int> qrtSUM((m/square)+1);
vector<int> qrtCOUNT((m/square)+1);
vector<int> ans(q);
vector<pair<pair<int,int>,pair<int,int>>> copyQUERY(q);
for (int Q = 0; Q < q; Q++)
{
int s,t,x,y; cin >> s >> t >> x >> y;
copyQUERY[Q]={{s,t},{x,y}};
prefix[s-1]++;
}
for (int i = 1; i < n; i++) prefix[i]+=prefix[i-1];
for (int Q = 0; Q < q; Q++)
{
int s=copyQUERY[Q].first.first,t=copyQUERY[Q].first.second,x=copyQUERY[Q].second.first,y=copyQUERY[Q].second.second;
if(s>t) swap(s,t);
queries[(prefix[s]/squareN)].push_back({{{t-1, s-1},{x,y}},Q});
coins[(prefix[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+1);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |