Submission #1120919

#TimeUsernameProblemLanguageResultExecution timeMemory
1120919Yang8onTwo Currencies (JOI23_currencies)C++17
100 / 100
1364 ms44556 KiB
#include <bits/stdc++.h> #define Y8o "TwoCurrencies" #define maxn (int) 1e5 + 5 #define ll long long #define pii pair<int, int> #define gb(i, j) ((i >> j) & 1) #define all(x) x.begin(), x.end() #define _left id * 2, l, mid #define _right id * 2 + 1, mid + 1, r #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 f first #define s second using namespace std; 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); } void iof() { ios_base::sync_with_stdio(0); cin.tie(NULL), cout.tie(NULL); if(fopen(Y8o".inp", "r")) { freopen(Y8o".inp", "r", stdin); // freopen(Y8o".out", "w", stdout); } } void ctime() { cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n"; } int n, m, Q, N; pii ed[maxn]; vector<int> o[maxn]; int in[maxn], out[maxn], cnt; int h[maxn], cha[maxn][17]; vector<pii> cur; struct dl { int u, v; ll g, si; } qr[maxn]; int L[maxn], R[maxn], ans[maxn]; vector<int> st[maxn]; struct BIT { ll bit[maxn]; void build() { fi(i, 1, n) bit[i] = 0; } void update(int x, ll val) { while(x <= n) bit[x] += val, x += (x & -x); } ll get(int x, ll sum = 0) { while(x > 0) sum += bit[x], x -= (x & -x); return sum; } void upR(int l, int r, ll val) { update(l, +val), update(r + 1, -val); } } Bit; void dfs(int u = 1, int p = 0) { in[u] = ++cnt; for(int v : o[u]) if(v != p) { h[v] = h[u] + 1, cha[v][0] = u; fi(i, 1, 16) cha[v][i] = cha[cha[v][i - 1]][i - 1]; dfs(v, u); } out[u] = cnt; } int lca(int u, int v) { if(h[u] < h[v]) swap(u, v); int kc = h[u] - h[v]; fid(i, 16, 0) if(gb(kc, i)) u = cha[u][i]; fid(i, 16, 0) if(cha[u][i] != cha[v][i]) u = cha[u][i], v = cha[v][i]; return u == v ? u : cha[u][0]; } ll weight(int u, int v) { return Bit.get(in[u]) + Bit.get(in[v]) - 2ll*Bit.get(in[lca(u, v)]); } void solve() { cin >> n >> m >> Q; fi(i, 1, n - 1) { int u, v; cin >> u >> v; o[u].push_back(v), o[v].push_back(u); ed[i] = { u, v }; } dfs(); fi(i, 1, n - 1) { int &u = ed[i].f, &v = ed[i].s; if(cha[u][0] == v) swap(u, v); } fi(i, 1, m) { int id, val; cin >> id >> val; cur.push_back({ val, id }); } sort( all(cur) ); N = cur.size() - 1; fi(i, 1, Q) { int u, v; ll g, si; cin >> u >> v >> g >> si; qr[i] = { u, v, g, si }; L[i] = 0, R[i] = N; ans[i] = g; } while(1) { int ok = 1; fi(i, 1, Q) if(L[i] <= R[i]) { ok = 0; st[ (L[i] + R[i]) >> 1 ].push_back(i); } if(ok) break; Bit.build(); fi(_, 0, N) { int val, i; tie(val, i) = cur[_]; int u, v; tie(u, v) = ed[i]; Bit.upR(in[v], out[v], +val); while(st[_].size()) { int id = st[_].back(); st[_].pop_back(); int u = qr[id].u, v = qr[id].v; ll g = qr[id].g, si = qr[id].si; if(si >= weight(u, v)) L[id] = _ + 1; else R[id] = _ - 1; } } } fi(i, 1, Q) st[R[i] + 1].push_back(i); Bit.build(); fid(_, N, 0) { int val, i; tie(val, i) = cur[_]; int u, v; tie(u, v) = ed[i]; Bit.upR(in[v], out[v], +1); while(st[_].size()) { int id = st[_].back(); st[_].pop_back(); int u = qr[id].u, v = qr[id].v; ll g = qr[id].g, si = qr[id].si; ans[id] -= weight(u, v); } } fi(i, 1, Q) cout << max(ans[i], -1) << '\n'; } int main() { iof(); int nTest = 1; // cin >> nTest; while(nTest --) { solve(); } ctime(); return 0; }

Compilation message (stderr)

currencies.cpp: In function 'void solve()':
currencies.cpp:128:20: warning: unused variable 'g' [-Wunused-variable]
  128 |                 ll g = qr[id].g, si = qr[id].si;
      |                    ^
currencies.cpp:146:16: warning: unused variable 'g' [-Wunused-variable]
  146 |             ll g = qr[id].g, si = qr[id].si;
      |                ^
currencies.cpp:146:30: warning: unused variable 'si' [-Wunused-variable]
  146 |             ll g = qr[id].g, si = qr[id].si;
      |                              ^~
currencies.cpp: In function 'void iof()':
currencies.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   27 |         freopen(Y8o".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...