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;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
const int mod = 1e9 + 7;
const int MAX = 1e5 + 7;
mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
vector<pair<int, int>> g[MAX];
int A[MAX], B[MAX];
int tin[MAX];
int timer = 0;
vector<pair<int, int>> pj(MAX);
vector<int> pst(MAX);
vector<int> path;
vector<int> als[MAX];
vector<ll> f1(MAX, 0);
vector<int> f2(MAX, 0);
void add1(int p, ll x, int x2) {
for (; p < MAX; p = (p | (p + 1))) {
f1[p] += x;
f2[p] += x2;
}
}
ll sum1(int p) {
ll res = 0;
for (; p >= 0; p = (p & (p + 1)) - 1) {
res += f1[p];
}
return res;
}
int sum2(int p) {
int res = 0;
for (; p >= 0; p = (p & (p + 1)) - 1) {
res += f2[p];
}
return res;
}
void dfs(int v, int p) {
tin[v] = timer - 1;
for (auto [to, num] : g[v]) {
if (to != p) {
for (auto add : als[num]) {
timer++;
path.push_back(add);
}
dfs(to, v);
for (auto add : als[num]) {
timer++;
path.push_back(add);
}
}
}
}
pair<int, int> getpath(int v, int u) {
if (tin[v] > tin[u]) swap(v, u);
return {tin[v] + 1, tin[u]};
}
vector<int> cnts(MAX, 0);
int l = 0, r = 0;
void modify(int now) {
// cout << now << ' ' << cnts[now] << ' ' << l << ' ' << r << '\n';
if (cnts[now] == 1) {
add1(pst[now], pj[now].second, 1);
} else {
add1(pst[now], -pj[now].second, -1);
}
}
// int op = 0;
void upd(int nl, int nr) {
while (r > nr) {
// op++;
cnts[path[r]]--;
modify(path[r]);
r--;
}
while (r < nr) {
// op++;
r++;
cnts[path[r]]++;
modify(path[r]);
}
while (l < nl) {
// op++;
cnts[path[l]]--;
modify(path[l]);
l++;
}
while (l > nl) {
// op++;
l--;
cnts[path[l]]++;
modify(path[l]);
}
// if (op > 3e7) cout << 1 / 0;
}
void solve() {
int n, m, q;
cin >> n >> m >> q;
for (int i = 1; i < n; i++) {
int v, u;
cin >> v >> u;
A[i] = v;
B[i] = u;
g[v].push_back({u, i});
g[u].push_back({v, i});
}
for (int i = 1; i <= m; i++) {
cin >> pj[i].first >> pj[i].second;
als[pj[i].first].push_back(i);
}
vector<int> ps(m + 1);
for (int i = 1; i <= m; i++) ps[i] = i;
sort(ps.begin() + 1, ps.end(), [&](int p1, int p2) {
return pj[p1].second < pj[p2].second;
});
int ptr = 0;
for (int i = 1; i <= m; i++) {
pst[ps[i]] = ++ptr;
}
dfs(1, 1);
cnts[path[0]]++;
modify(path[0]);
vector<int> res(MAX, 0);
int buben = 888;
vector<array<ll, 5>> rq[MAX];
for (int i = 0; i < q; i++) {
ll v, u, a, b;
cin >> v >> u >> a >> b;
auto [L, R] = getpath(v, u);
rq[R / buben].push_back({L, R, a, b, i});
}
for (int i = 0; i < 1000; i++) {
if (rq[i].size()) {
sort(rq[i].begin(), rq[i].end(), [&](auto p1, auto p2) {
if (p1[0] == p2[0]) return p1[1] < p2[1];
return p1[0] < p2[0];
});
for (auto [L, R, a, b, i] : rq[i]) {
if (L > R) {
res[i] = a;
continue;
}
upd(L, R);
if (sum1(ptr) <= b) {
res[i] = a;
continue;
}
int can = 0;
for (int bt = 16; bt >= 0; bt--) {
int ncan = can + (1 << bt);
if (ncan <= ptr) {
if (sum1(ncan) <= b) {
can = ncan;
}
}
}
// cout << i << ' ' << can << '\n';
int lf = sum2(ptr) - sum2(can);
res[i] = a - lf;
}
}
}
for (int i = 0; i < q; i++) {
cout << max(-1, res[i]) << '\n';
}
}
int main() {
#ifdef __APPLE__
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0); cin.tie(0);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
Compilation message (stderr)
currencies.cpp: In function 'void dfs(int, int)':
currencies.cpp:57:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for (auto [to, num] : g[v]) {
| ^
currencies.cpp: In function 'void solve()':
currencies.cpp:150:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
150 | auto [L, R] = getpath(v, u);
| ^
currencies.cpp:159:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
159 | for (auto [L, R, a, b, i] : rq[i]) {
| ^
# | 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... |