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>
#define ll long long
#define ull unsigned long long
#define all(x) x.begin(), x.end()
#define Neco "hai loai tien te"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 1e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n, m, q;
int ans[maxn];
int In[maxn], Out[maxn], h[maxn];
int st[maxn], _log[2 * maxn], Sl;
vector<int> edges[maxn];
pair<int, int> sth[maxn];
pair<int, int> E[maxn], L[2 * maxn][18], P[maxn];
struct QR {ll u, v, x, y, id; } qr[maxn];
struct dl {int l, r, id; };
struct BIT {
ll bit[maxn];
void init() {
fi(i, 1, n + 5) bit[i] = 0;
}
void update(int x, ll val) {
int l = In[x], r = Out[x] + 1;
while(l < maxn) bit[l] += val, l += (l & -l);
while(r < maxn) bit[r] -= val, r += (r & -r);
}
ll get(int x, ll ans = 0) {
while(x > 0) ans += bit[x], x -= (x & -x);
return ans;
}
} Bit;
/// PRE DFS
void pre_dfs(int u, int par, int k) {
static int Time = 0;
In[u] = ++Time, L[++Sl][0] = {k, u};
st[u] = Sl, h[u] = k;
for(int v : edges[u]) if(v != par) {
pre_dfs(v, u, k + 1);
L[++Sl][0] = {k, u};
}
Out[u] = Time;
}
void prepare() {
fi(i, 1, Sl) _log[i] = __lg(i);
fi(j, 1, _log[Sl]) fi(i, 1, Sl - (1 << j) + 1) L[i][j] = min(L[i][j - 1], L[i + (1 << (j - 1))][j - 1]);
}
int lca(int u, int v) {
int l = st[u], r = st[v], j;
if(l > r) swap(l, r);
j = _log[r - l + 1];
return min(L[l][j], L[r - (1 << j) + 1][j]).second;
}
ll distW(int u, int v) {
return Bit.get(In[u]) + Bit.get(In[v]) - 2 * Bit.get(In[lca(u, v)]);
}
/*----------------*/
void input()
{
cin >> n >> m >> q;
fi(i, 1, n - 1) {
int x, y;
cin >> x >> y;
edges[x].push_back(y), edges[y].push_back(x);
E[i] = {x, y};
}
fi(i, 1, m) {
int id, val; cin >> id >> val;
P[i] = {val, id};
}
fi(i, 1, q) cin >> qr[i].u >> qr[i].v >> qr[i].x >> qr[i].y, qr[i].id = i;
}
void solve()
{
input();
pre_dfs(1, 0, 0), prepare();
fi(i, 1, m) {
int u = In[E[P[i].second].first] > In[E[P[i].second].second] ? E[P[i].second].first : E[P[i].second].second;
P[i].second = u;
}
sort(P + 1, P + 1 + m);
vector<dl> store[maxn], tmp[maxn];
fi(i, 1, q) store[(1 + m) >> 1].push_back({1, m, i});
int Cnt = 0;
while(Cnt < q)
{
Bit.init();
fi(i, 1, m) {
Bit.update(P[i].second, P[i].first);
for(auto [l, r, id] : store[i]) {
ll u = qr[id].u, v = qr[id].v, Silver = qr[id].y;
int mid = (l + r) >> 1;
if(distW(u, v) <= Silver) l = mid + 1;
else r = mid - 1;
if(l > r) ans[qr[id].id] = r, ++Cnt;
else tmp[(l + r) >> 1].push_back({l, r, id});
}
}
fi(i, 1, m) swap(tmp[i], store[i]), tmp[i].clear();
}
// Log(P, 1, m) ;
Bit.init();
fi(i, 1, q) sth[i] = {ans[i], i};
sort(sth + 1, sth + 1 + q);
int j = 1;
fi(i, 1, q) {
while(j <= sth[i].first) Bit.update(P[j++].second, 1);
int id = sth[i].second, u = qr[id].u, v = qr[id].v;
ans[id] = - distW(u, v);
}
while(j <= m) Bit.update(P[j++].second, 1);
fi(i, 1, q) {
ans[i] = qr[i].x - (distW(qr[i].u, qr[i].v) + ans[i]);
cout << max(ans[i], -1) << '\n';
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message (stderr)
currencies.cpp: In function 'int main()':
currencies.cpp:184:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
184 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:185:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
185 | freopen(Neco".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... |