Submission #1123141

#TimeUsernameProblemLanguageResultExecution timeMemory
1123141Yang8onConstruction of Highway (JOI18_construction)C++20
100 / 100
228 ms20932 KiB
#include <bits/stdc++.h> #define Y8o "Construction_of_Highway" #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; int a[maxn], sz[maxn]; pii ed[maxn]; vector<int> o[maxn], cur; ll ans; int cnt; int h[maxn], par[maxn], nex[maxn]; int in[maxn], out[maxn], tt[maxn]; vector<pii> child[maxn]; struct BIT { int bit[maxn]; void update(int x, int val) { while(x > 0) bit[x] += val, x -= (x & -x); } int get(int x, int sum = 0) { while(x <= n) sum += bit[x], x += (x & -x); return sum; } } Bit; void pre_dfs(int u = 1, int p = 0) { sz[u] = 1; for(int &v : o[u]) { o[v].erase(find(all(o[v]), u)); pre_dfs(v, u); if(sz[v] > sz[o[u][0]]) swap(v, o[u][0]); sz[u] += sz[v]; } } void dfs(int u = 1) { in[u] = ++cnt, tt[cnt] = u; for(int v : o[u]) { h[v] = h[u] + 1, par[v] = u; nex[v] = v == o[u][0] ? nex[u] : v; dfs(v); } out[u] = cnt; } void prog(int p, int u, int val, vector<pii>& st) { int pre = h[p] - 1; vector<pii> tmp; while(child[p].size() && h[child[p].back().f] < h[u]) { tmp.push_back({ child[p].back().s, h[child[p].back().f] - pre }); pre = h[child[p].back().f]; child[p].pop_back(); } if(child[p].size()) tmp.push_back({ child[p].back().s, h[u] - pre }); child[p].push_back({ u, val }); reverse(all(tmp)); for(pii x : tmp) st.push_back(x); } void update(int u, int val) { vector<pii> st; /// luu tru nhung doan cap nhat cho dap an while(u != 0) { prog(nex[u], u, val, st); u = par[nex[u]]; } reverse(all(st)); for(auto [cl, sl] : st) ans += 1ll * sl * Bit.get(cl), Bit.update(cl - 1, +sl); for(auto [cl, sl] : st) Bit.update(cl - 1, -sl); } void solve() { cin >> n; fi(i, 1, n) cin >> a[i], cur.push_back(a[i]); sort(all(cur)); cur.resize(unique(all(cur)) - cur.begin()); fi(i, 1, n) a[i] = lower_bound(all(cur), a[i]) - cur.begin() + 1; 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 }; } nex[1] = 1; pre_dfs(), dfs(); update(1, a[1]); fi(i, 1, n - 1) { int u, v; tie(u, v) = ed[i]; ans = 0, update(v, a[v]); cout << ans << '\n'; } } int main() { iof(); int nTest = 1; // cin >> nTest; while(nTest --) { solve(); } ctime(); return 0; }

Compilation message (stderr)

construction.cpp: In function 'void iof()':
construction.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...