이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
typedef pair <ll, ll> ii;
const ll N = 1e5 + 99;
struct st
{
ll s, t, z, w;
};
st qs[N];
ll n, m, q, up[N][20], In[N], Out[N], h[N], cnt;
ll num[N], diff[N], dd[N], ans[N];
ii edg[N];
vector <ll> adj[N];
vector <ii> vt;
void dfs(ll u)
{
In[u] = ++cnt;
for (ll v : adj[u])
{
if (up[u][0] == v) continue;
h[v] = h[u] + 1;
up[v][0] = u;
for (int i = 1; i <= 16; i++)
{
up[v][i] = up[up[v][i - 1]][i - 1];
}
dfs(v);
}
Out[u] = cnt;
}
void pre(ll u)
{
for (ll v : adj[u])
{
if (up[u][0] == v) continue;
dd[v] += dd[u];
pre(v);
}
}
ll lca(ll u, ll v)
{
if (h[u] < h[v]) swap(u, v);
ll k = h[u] - h[v];
for (int i = 0; (1 << i) <= k; i++)
{
if (k >> i & 1)
{
u = up[u][i];
}
}
if (u == v) return u;
k = log2(h[u]);
for (int i = k; i >= 0; i--)
{
if (up[u][i] != up[v][i])
{
u = up[u][i];
v = up[v][i];
}
}
return up[u][0];
}
void update(ll x, ll val)
{
for (; x <= cnt; x += (x & -x))
{
diff[x] += val;
if (val >= 0) num[x]++;
else num[x]--;
}
}
ll getval(ll x)
{
ll res = 0;
for (; x > 0; x &= (x - 1))
{
res += diff[x];
}
return res;
}
ll getnum(ll x)
{
ll res = 0;
for (; x > 0; x &= (x - 1))
{
res += num[x];
}
return res;
}
void solve(ll l, ll r, vector <ll> id)
{
if (l > r || id.size() == 0) return;
vector <ll> left, right;
ll mid = (l + r) / 2;
for (int i = l; i <= mid; i++)
{
auto [c, u] = vt[i - 1];
update(In[u], c);
update(Out[u] + 1, -c);
}
for (ll i : id)
{
ll path = lca(qs[i].s, qs[i].t);
ll val = getval(In[qs[i].s]) + getval(In[qs[i].t]) - 2 * getval(In[path]);
if (val <= qs[i].w)
{
right.push_back(i);
ans[i] = max(ans[i], getnum(In[qs[i].s]) + getnum(In[qs[i].t]) - 2 * getnum(In[path]));
}
else left.push_back(i);
}
solve(mid + 1, r, right);
for (int i = l; i <= mid; i++)
{
auto [c, u] = vt[i - 1];
update(In[u], -c);
update(Out[u] + 1, c);
}
if (l != r) solve(l, mid, left);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if(fopen("test.inp", "r"))
{
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> m >> q;
for (int i = 1; i < n; i++)
{
cin >> edg[i].fi >> edg[i].se;
adj[edg[i].fi].push_back(edg[i].se);
adj[edg[i].se].push_back(edg[i].fi);
}
dfs(1);
for (int i = 1; i <= m; i++)
{
ll x, c;
cin >> x >> c;
ll res = edg[x].fi;
if (h[edg[x].fi] < h[edg[x].se]) res = edg[x].se;
dd[res]++;
vt.push_back({c, res});
}
pre(1);
sort(vt.begin(), vt.end());
vector <ll> id;
for (int i = 1; i <= q; i++)
{
cin >> qs[i].s >> qs[i].t >> qs[i].z >> qs[i].w;
id.push_back(i);
}
solve(1, m, id);
for (int i = 1; i <= q; i++)
{
ll path = lca(qs[i].s, qs[i].t);
ll res = dd[qs[i].s] + dd[qs[i].t] - 2 * dd[path];
if (res - ans[i] > qs[i].z)
{
cout << -1 << '\n';
}
else
{
cout << qs[i].z - (res - ans[i]) << '\n';
}
}
}
컴파일 시 표준 에러 (stderr) 메시지
currencies.cpp: In function 'void solve(long long int, long long int, std::vector<long long int>)':
currencies.cpp:114:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
114 | auto [c, u] = vt[i - 1];
| ^
currencies.cpp:135:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
135 | auto [c, u] = vt[i - 1];
| ^
currencies.cpp: In function 'int main()':
currencies.cpp:151:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:152:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | freopen("test.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... |