This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
bool fl = false;
struct SegTree{
vector <vector<int>> T, pf;
int n;
SegTree(auto &a){
n = (int)a.size();
T.resize(n * 4 + 1), pf.resize(n * 4 + 1);
auto build = [&](auto build, int v, int l, int r) -> void{
if ( l == r ){
T[v] = a[l];
sort(all(T[v]));
} else{
int md = (l + r) >> 1;
build(build, v * 2, l, md);
build(build, v * 2 + 1, md + 1, r);
merge(all(T[v * 2]), all(T[v * 2 + 1]), back_inserter(T[v]));
}
pf[v].pb(0);
for ( auto &x: T[v] ){
pf[v].pb(pf[v].back() + x);
}
};
build(build, 1, 0, n - 1);
}
int get(int v, int l, int r, int tl, int tr, int k){
if ( l > tr or r < tl ){
return 0;
}
if ( tl <= l && tr >= r ){
int q = lower_bound(all(T[v]), k + 1) - T[v].begin();
return fl ? (int)T[v].size() - q : pf[v][q];
}
int md = (l + r) >> 1;
return get(v * 2, l, md, tl, tr, k) + get(v * 2 + 1, md + 1, r, tl, tr, k);
}
int get(int l, int r, int k){
if ( l > r ){
++r;
swap(l, r);
} else r--;
return get(1, 0, n - 1, l, r, k);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m, q; cin >> n >> m >> q;
for ( int i = 0; i + 1 < n; i++ ){
int u, v; cin >> u >> v;
--u, --v;
}
vector <vector<int>> pt(n);
int cost = -1;
for ( int i = 0; i < m; i++ ){
int p, c; cin >> p >> c;
pt[--p].pb(c);
}
SegTree seg(pt);
vector <ar<int,5>> qu(q);
int ind = 0;
for ( auto &[s, t, x, y, i]: qu ){
cin >> s >> t >> x >> y;
--s, --t; i = ind++;
}
vector <int> opt(q);
auto dfs = [&](auto dfs, int l, int r, auto &qu){
if ( qu.empty() ) return;
if ( l + 1 == r ){
for ( auto &[s, t, x, y, i]: qu ){
opt[i] = l;
} return;
}
int md = (l + r) >> 1;
vector <ar<int,5>> L, R;
for ( auto &[s, t, x, y, i]: qu ){
int cur = seg.get(s, t, md);
if ( cur > y ){
L.pb({s, t, x, y, i});
} else R.pb({s, t, x, y, i});
}
dfs(dfs, l, md, L);
dfs(dfs, md, r, R);
};
const int inf = 1e18 + 1;
dfs(dfs, 0, inf, qu);
for ( auto &[s, t, x, y, i]: qu ){
fl = true;
int r = seg.get(s, t, opt[i]);
fl = false;
y -= seg.get(s, t, opt[i]);
r -= y / (opt[i] + 1);
cout << max(-1LL, x - max(0LL, r)) << ln;
}
cout << '\n';
}
Compilation message (stderr)
currencies.cpp:36:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
36 | SegTree(auto &a){
| ^~~~
currencies.cpp: In function 'int main()':
currencies.cpp:87:9: warning: unused variable 'cost' [-Wunused-variable]
87 | int cost = -1;
| ^~~~
# | 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... |