This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// todo
#include <bits/stdc++.h>
using namespace std;
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, pair<int, int>>> edges;
int tin[N], tout[N], timer;
int up[N][LOG];
node st[25 * N];
int ll[25 * N], rr[25 * N];
int id;
int dep[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;
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) 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;
}
int 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);
edges.push_back({0, {u, v}});
}
for(int i = 0; i < m; i++) {
int ind, val;
cin >> ind >> val;
edges[ind - 1].first = val;
}
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++;
update(id, id - 1, 1, n, tin[u], 1, w);
if(tout[u] < n) update(id, id - 1, 1, n, tout[u] + 1, -1, -w);
}
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(mid + 1, 1, n, 1, tin[u]).sum + get(mid + 1, 1, n, 1, tin[v]).sum - 2 * get(mid + 1, 1, n, 1, tin[lca]).sum <= y) {
ans = mid;
l = mid + 1;
} else r = mid - 1;
}
cout << dep[u] + dep[v] - 2 * dep[lca] - (get(ans + 1, 1, n, 1, tin[u]).sum + get(ans + 1, 1, n, 1, tin[v]).sum - 2 * get(ans + 1, 1, n, 1, tin[lca]).sum) << "\n";
}
return 0;
}
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:100:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
100 | 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... |