이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 r){
int ret=0;
for ( ; r>=0; r=(r&(r+1))-1) ret+=bit[r];
return ret;
}
int count0(int r){
int ret=0;
for ( ; r>=0; r=(r&(r+1))-1) ret+=bitc[r];
return ret;
}
int count0(int l, int r){
return count0(r) - count0(l - 1);
}
void change(int idx, int val){
for (; idx < n; idx = (idx | (idx + 1))) {
bit[idx] += val;
if(val>0) bitc[idx]++;
else bitc[idx]--;
}
}
};
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(n);
vector<vector<pair<pair<pair<int,int>,pair<int,int>>,int>>> queries((n/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/square)].push_back({{{t-1, s-1},{x,y}},Q});
}
for (int i = 0; i < sz(queries); i++) sort(all(queries[i]));
for (int i = 0; i < sz(queries); i++){
int l=-1,r=-1;
FenwickTree tree(sz(v));
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(j==0) r=s;
for (int k = r; k < t; k++) {
for (auto u : sum[k]) {
tree.change(u.second, u.first);
}
}
for (int k = l; k < s && j>0; k++) {
for (auto u : sum[k]) {
tree.change(u.second, -u.first);
}
}
for (int k = s; k < l && j>0; k++) {
for (auto u : sum[k]) {
tree.change(u.second, u.first);
}
}
l=s; r=t;
int bl=0,br=sz(v)-1;
int as=-1;
while(bl<=br){
int mid=(bl+br)/2;
int sm=tree.sum(mid);
if(sm>y) {
br=mid-1;
}else{
as=mid;
bl=mid+1;
}
}
int c=tree.count0(as+1, sz(v)-1);
ans[qr]=max(-1LL, x-c);
}
}
for (int i = 0; i < sz(ans); i++) cout << ans[i] << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |