Submission #1123484

#TimeUsernameProblemLanguageResultExecution timeMemory
1123484ljtunasTwo Currencies (JOI23_currencies)C++17
0 / 100
598 ms123604 KiB
// 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 = 1e6 + 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 num[MAXN], tail[MAXN], depth[MAXN], d[MAXN], _lg[MAXN * 2], timedfs = 0, in[MAXN], en[MAXN], Timedfs = 0; vector<int> euler[MAXN * 2]; int check(int u, int v) { return num[u] <= num[v] && tail[v] <= tail[u]; } int min_by_time(int u, int v) { return (num[u] < num[v] ? u : v); } void dfs(int u, int par){ ++ timedfs; num[u] = timedfs; in[u] = ++Timedfs; euler[timedfs].push_back(u); Forv(v, g[u]){ if (v == par) continue; depth[v] = depth[u] + 1; dfs(v, u); euler[++ timedfs].push_back(u); } tail[u] = timedfs; en[u] = Timedfs; if (par == 0) { For (i, 0, 2 * n) _lg[i] = log2(i); For (k, 1, 19) { for (int i = 1; i + (1 << k) - 1 <= timedfs; ++i) euler[i].push_back(min_by_time(euler[i][k - 1], euler[i + (1 << k - 1)][k - 1])); } } } int getlca(int u, int v) { int L = min(num[u], num[v]); int R = max(tail[u], tail[v]); int lg = _lg[R - L + 1]; return min_by_time(euler[L][lg], euler[R - (1 << lg) + 1][lg]); } 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(num[u] > num[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; // cerr << f[i] << '\n'; } vector<vector<int>> vec(n + 10); 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; }

Compilation message (stderr)

currencies.cpp: In function 'int main()':
currencies.cpp:181:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  181 |         freopen("lat.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
currencies.cpp:182:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  182 |         freopen("lat.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...