Submission #305619

#TimeUsernameProblemLanguageResultExecution timeMemory
305619youngyojunConstruction of Highway (JOI18_construction)C++17
100 / 100
696 ms26616 KiB
#include <bits/stdc++.h> #define pb push_back #define eb emplace_back #define sz(V) ((int)(V).size()) #define allv(V) ((V).begin()),((V).end()) #define befv(V) ((V)[(sz(V)-2)]) #define sorv(V) sort(allv(V)) #define revv(V) reverse(allv(V)) #define univ(V) (V).erase(unique(allv(V)),(V).end()) #define clv(V) (V).clear() #define upmin(a,b) (a)=min((a),(b)) #define upmax(a,b) (a)=max((a),(b)) #define rb(x) ((x)&(-(x))) #define INF (0x3f3f3f3f) #define INFLL (0x3f3f3f3f3f3f3f3fll) using namespace std; typedef long long ll; typedef pair<int, int> pii; const int MAXN = 100055; struct BIT { int d[MAXN]; void upd(int x, int r) { for(x += 2; x < MAXN; x += rb(x)) d[x] += r; } int get(int x) { int r = 0; for(x += 2; x; x -= rb(x)) r += d[x]; return r; } } bit; struct HLD { set<pii> V; int rprt, n; void init(int _n) { n = _n; } void get(vector<pii> &PQ, int i) { vector<pii> T; for(auto it = V.lower_bound(pii(i, -INF));; it--) { T.eb(min(i, it -> first), it -> second); if(V.begin() == it) break; } T.eb(-1, -1); for(int i = 1, n = sz(T), l, x; i < n; i++) { l = T[i-1].first - T[i].first; x = T[i-1].second; if(!l) continue; if(PQ.empty() || PQ.back().first != x) PQ.eb(x, l); else PQ.back().second += l; } } void upd(int i, int c) { for(; !V.empty();) { auto it = V.begin(); if(i < it -> first) break; V.erase(it); } V.insert(pii(i, c)); } } hld[MAXN]; vector<int> G[MAXN]; int HI[MAXN], HJ[MAXN], HSZ[MAXN], Hn; int prt[MAXN], dep[MAXN], cnt[MAXN], depo[MAXN]; int A[MAXN], B[MAXN], C[MAXN]; int N; void f(vector<pii> &V, int i) { for(; i; i = hld[HI[i]].rprt) hld[HI[i]].get(V, HJ[i]); } void g(int i, int c) { for(; i; i = hld[HI[i]].rprt) hld[HI[i]].upd(HJ[i], c); } ll getRev(vector<pii> &V) { { vector<int> VX; for(auto &v : V) VX.pb(v.first); sorv(VX); univ(VX); for(auto &v : V) v.first = (int)(lower_bound(allv(VX), v.first) - VX.begin()) + 1; } ll ret = 0; for(auto &v : V) { ret += ll(bit.get(v.first-1)) * v.second; bit.upd(v.first, v.second); } for(auto &v : V) bit.upd(v.first, -v.second); return ret; } void dfs1(int i) { cnt[i] = 1; for(int v : G[i]) { dep[v] = dep[i] + 1; prt[v] = i; dfs1(v); cnt[i] += cnt[v]; } } void dfs2(int i) { HJ[i] = HSZ[HI[i]]++; int hi = -1, hc = -1; for(int v : G[i]) { if(cnt[v] <= hc) continue; hi = v; hc = cnt[v]; } if(hi < 0) return; HI[hi] = HI[i]; dfs2(hi); } int main() { ios::sync_with_stdio(false); cin >> N; for(int i = 1; i <= N; i++) cin >> C[i]; for(int i = 1; i < N; i++) cin >> A[i] >> B[i]; for(int i = 1; i < N; i++) G[A[i]].pb(B[i]); dep[1] = 1; dfs1(1); iota(depo, depo+N+1, 0); sort(depo+1, depo+N+1, [&](int a, int b) { return dep[a] < dep[b]; }); for(int oi = 1, i; oi <= N; oi++) { i = depo[oi]; if(HI[i]) continue; Hn++; HI[i] = Hn; dfs2(i); } for(int i = 1; i <= Hn; i++) hld[i].init(HSZ[i]); for(int i = 1; i <= N; i++) { hld[HI[i]].V.insert(pii(HJ[i], C[i])); if(!HJ[i]) hld[HI[i]].rprt = prt[i]; } for(int qi = 1, a, b; qi < N; qi++) { a = A[qi]; b = B[qi]; vector<pii> V; f(V, a); printf("%lld\n", getRev(V)); g(b, C[b]); } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...