// author : anhtun_hi , nqg
#include <bits/stdc++.h>
#define ll long long
#define ii pair<ll, ll>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define reset(h, v) memset(h, v, sizeof h)
#define Forv(i, a) for(auto& i : a)
#define For(i, a, b) for(int i = a; i <= b; ++i)
#define Ford(i, a, b) for(int i = a; i >= b; --i)
#define c_bit(i) __builtin_popcountll(i)
#define Bit(mask, i) ((mask >> i) & 1LL)
#define onbit(mask, i) ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
#define Log2(x) (63 - __builtin_clzll(x))
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
using namespace std;
namespace std {
template <typename T, int D>
struct _vector : public vector <_vector <T, D - 1>> {
static_assert(D >= 1, "Dimension must be positive!");
template <typename... Args>
_vector(int n = 0, Args... args) : vector <_vector <T, D - 1>> (n, _vector <T, D - 1> (args...)) {}
};
template <typename T> struct _vector <T, 1> : public vector <T> {
_vector(int n = 0, const T& val = T()) : vector <T> (n, val) {}
};
template <class A, class B> bool minimize(A &a, B b){return a > b ? a = b, true : false;}
template <class A, class B> bool maximize(A &a, B b){return a < b ? a = b, true : false;}
}
const int dx[] = {0, 0, +1, -1}, dy[] = {-1, +1, 0, 0}, LOG = 20, base = 311, inf = 1e9 + 5;
const int MAXN = 1e5 + 5;
const ll mod = 1e9 + 7;
const ll oo = 1e18;
//#define int long long
int n, m, q; vector<int> g[MAXN];
ii E[MAXN], a[MAXN];
int sz[MAXN], dep[MAXN], p[MAXN][22], in[MAXN], en[MAXN], timedfs = 0;
void dfs(int u, int par) {
in[u] = ++timedfs;
p[u][0] = par, sz[u] = 1;
For(k, 1, 20) p[u][k] = p[p[u][k - 1]][k - 1];
for(int v : g[u]) {
if (v == par) continue;
dep[v] = dep[u] + 1;
dfs(v, u);
sz[u] += sz[v];
}
en[u] = timedfs;
}
int getlca(int u, int v) {
if (dep[u] < dep[v]) swap(u, v);
int len = dep[u] - dep[v];
For(k, 0, 20) if (Bit(len, k)) u = p[u][k];
if (u == v) return u;
Ford(k, 20, 0) if (p[u][k] != p[v][k])
u = p[u][k], v = p[v][k];
return p[u][0];
}
struct Queries{
int u, v; ll x, y;
} qr[MAXN];
int lo[MAXN], hi[MAXN], res[MAXN], f[MAXN];
ii bit[MAXN];
void update(int u, ii val){
while (u <= n) bit[u].fi += val.fi, bit[u].se += val.se, u += u &- u;
}
void update(int l, int r, ii val){
update(l, val);
update(r + 1, {-val.fi, -val.se});
}
ii get(int u){
ii ans = {0, 0};
while (u) ans.fi += bit[u].fi, ans.se += bit[u].se, u -= u &- u;
return ans;
}
void Solve() {
cin >> n >> m >> q;
For(i, 1, n - 1){
auto &[u, v] = E[i];
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 0);
For(i, 1, m) cin >> a[i].se >> a[i].fi;
sort(a + 1, a + m + 1);
For(i, 1, n - 1) {
auto &[u, v] = E[i];
if(in[u] > in[v]) swap(u, v);
}
For(i, 1, m){
auto [c, p] = a[i];
auto [u, v] = E[p];
update(in[v], en[v], {c, 1});
}
For(i, 1, q){
auto &[u, v, x, y] = qr[i];
cin >> u >> v >> x >> y;
lo[i] = -1, hi[i] = m + 1;
ii _u = get(in[u]);
ii _v = get(in[v]);
ii _lca = get(in[getlca(u, v)]);
ii tmp = {_u.fi + _v.fi - _lca.fi * 2, _u.se + _v.se - _lca.se * 2};
f[i] = tmp.se;
}
vector<vector<int>> vec(1e6);
while (true){
bool ok = false;
For(i, 0, m) vec[i].clear();
For(i, 1, q) {
if (hi[i] - lo[i] > 1){
int mid = lo[i] + hi[i] >> 1;
vec[mid].push_back(i);
ok = true;
}
}
if(!ok) break;
For(i, 1, n) bit[i] = {0, 0};
For(i, 0, m){
if(i){
auto [c, p] = a[i];
auto [u, v] = E[p];
update(in[v], en[v], {c, 1});
}
Forv(j, vec[i]){
auto [u, v, x, y] = qr[j];
ii _u = get(in[u]);
ii _v = get(in[v]);
ii _lca = get(in[getlca(u, v)]);
ii tmp = {_u.fi + _v.fi - _lca.fi * 2, _u.se + _v.se - _lca.se * 2};
if (tmp.fi <= y) lo[j] = i, res[j] = tmp.se;
else hi[j] = i;
}
}
}
For(i, 1, q) {
if (lo[i] == -1) cout << -1 << '\n';
else {
auto [u, v, x, y] = qr[i];
if (f[i] - res[i] <= x) {
cout << x - (f[i] - res[i]) << '\n';
}
else cout << -1 << '\n';
}
}
}
signed main() {
cin.tie(0) -> sync_with_stdio(0);
if(fopen("lat.inp", "r")) {
freopen("lat.inp", "r", stdin);
freopen("lat.out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --) Solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In function 'int main()':
currencies.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen("lat.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:169:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
169 | freopen("lat.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |