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 int long long
struct node {
int cnt;
long long sum;
node() : cnt(0), sum(0) { }
};
const int N = 1e5 + 10, LOG = 18;
int n, m, q;
vector<int> g[N];
vector<pair<int, int>> edg;
vector<pair<int, pair<int, int>>> edges;
int tin[N], tout[N], timer;
int up[N][LOG];
node st[50 * N];
int ll[50 * N], rr[50 * N];
int id;
int dep[N];
int which[N];
bool is_ancestor(int u, int v) { return tin[u] <= tin[v] && tout[u] >= tout[v]; }
int LCA(int u, int v) {
if(is_ancestor(u, v)) return u;
if(is_ancestor(v, u)) return v;
for(int j = LOG - 1; j >= 0; j--) {
if(up[u][j] == 0) continue;
if(is_ancestor(up[u][j], v)) continue;
u = up[u][j];
}
return up[u][0];
}
void dfs(int u, int par) {
tin[u] = ++timer;
for(int v : g[u]) {
if(v == par) continue;
up[v][0] = u;
dep[v] = dep[u] + 1;
dfs(v, u);
}
tout[u] = timer;
}
void update(int root, int oldroot, int l, int r, int x, int val1, long long val2) {
if(l > r || r < x || x < l) return;
st[root] = st[oldroot];
if(l == r) {
st[root].cnt += val1;
st[root].sum += val2;
return;
}
int mid = (l + r) / 2;
if(mid >= x) {
ll[root] = ++id;
rr[root] = rr[oldroot];
update(ll[root], ll[oldroot], l, mid, x, val1, val2);
} else {
ll[root] = ll[oldroot];
rr[root] = ++id;
update(rr[root], rr[oldroot], mid + 1, r, x, val1, val2);
}
st[root].cnt = st[ll[root]].cnt + st[rr[root]].cnt;
st[root].sum = st[ll[root]].sum + st[rr[root]].sum;
}
node get(int root, int l, int r, int L, int R) {
if(l > r || r < L || R < l || root == 0) return node();
if(L <= l && r <= R) return st[root];
int mid = (l + r) / 2;
node nd1 = get(ll[root], l, mid, L, R);
node nd2 = get(rr[root], mid + 1, r, L, R);
node nd;
nd.cnt = nd1.cnt + nd2.cnt;
nd.sum = nd1.sum + nd2.sum;
return nd;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 1; i < n; i++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
edg.emplace_back(u, v);
}
edges.push_back({-1, edg[0]});
for(int i = 0; i < m; i++) {
int ind, val;
cin >> ind >> val;
edges.push_back({val, edg[ind - 1]});
}
sort(edges.begin(), edges.end());
dfs(1, 0);
for(int j = 1; j < LOG; j++) for(int i = 1; i <= n; i++) up[i][j] = up[up[i][j - 1]][j - 1];
for(int i = 0; i < edges.size(); i++) {
pair<int, pair<int, int>> e = edges[i];
int u = e.second.first, v = e.second.second;
if(up[v][0] == u) swap(u, v);
int w = e.first;
id++;
which[i] = id;
int lst = id;
if(i == 0) update(id, (i > 0 ? which[i - 1] : 0), 1, n, tin[u], 0, 0);
else update(id, (i > 0 ? which[i - 1] : 0), 1, n, tin[u], 1, w);
if(tout[u] < n) {
id++;
which[i] = id;
if(i == 0) update(id, lst, 1, n, tout[u] + 1, 0, 0);
else update(id, lst, 1, n, tout[u] + 1, -1, -w);
}
}
// for(int i = 0; i < edges.size(); i++) {
// for(int j = 1; j <= n; j++) cout << get(which[i], 1, n, 1, tin[j]).sum << " ";
// cout << "\n";
// }
while(q--) {
int u, v, x;
long long y;
cin >> u >> v >> x >> y;
int lca = LCA(u, v);
int l = 0, r = edges.size() - 1;
int ans = l;
while(l <= r) {
int mid = (l + r) / 2;
if(get(which[mid], 1, n, 1, tin[u]).sum + get(which[mid], 1, n, 1, tin[v]).sum - 2 * get(which[mid], 1, n, 1, tin[lca]).sum <= y) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
// for(int i = 0; i < edges.size(); i++) {
// cout << i << ":\n";
// cout << get(which[i], 1, n, 1, tin[u]).sum + get(which[i], 1, n, 1, tin[v]).sum - 2 * get(which[i], 1, n, 1, tin[lca]).sum << "\n";
// cout << get(which[i], 1, n, 1, tin[u]).cnt + get(which[i], 1, n, 1, tin[v]).cnt - 2 * get(which[i], 1, n, 1, tin[lca]).cnt << "\n";
// cout << "\n";
// }
// cout << ans << "\n";
int res = get(which[edges.size() - 1], 1, n, 1, tin[u]).cnt + get(which[edges.size() - 1], 1, n, 1, tin[v]).cnt - 2 * get(which[edges.size() - 1], 1, n, 1, tin[lca]).cnt - (get(which[ans], 1, n, 1, tin[u]).cnt + get(which[ans], 1, n, 1, tin[v]).cnt - 2 * get(which[ans], 1, n, 1, tin[lca]).cnt);
res = x - res;
cout << (res < 0 ? -1 : res) << "\n";
}
return 0;
}
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:104:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
104 | for(int i = 0; i < edges.size(); 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... |