Submission #107021

#TimeUsernameProblemLanguageResultExecution timeMemory
107021eriksuenderhaufDesignated Cities (JOI19_designated_cities)C++11
23 / 100
139 ms13728 KiB
#pragma GCC optimize("O3") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define mem(a,v) memset((a), (v), sizeof (a)) #define enl printf("\n") #define case(t) printf("Case #%d: ", (t)) #define ni(n) scanf("%d", &(n)) #define nl(n) scanf("%lld", &(n)) #define nai(a, n) for (int i = 0; i < (n); i++) ni(a[i]) #define nal(a, n) for (int i = 0; i < (n); i++) nl(a[i]) #define pri(n) printf("%d\n", (n)) #define prl(n) printf("%lld\n", (n)) #define pii pair<int, int> #define pil pair<int, long long> #define pll pair<long long, long long> #define vii vector<pii> #define vil vector<pil> #define vll vector<pll> #define vi vector<int> #define vl vector<long long> #define pb push_back #define mp make_pair #define fi first #define se second using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef cc_hash_table<int,int,hash<int>> ht; typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset; const double pi = acos(-1); const int MOD = 1e9 + 7; const ll INF = 1e16 + 7; const int MAXN = 2e5 + 5; const double eps = 1e-9; pil edg[MAXN]; vi adj[MAXN]; ll sm[MAXN], mx[MAXN], ans[MAXN], depth[MAXN]; ll seg[MAXN*4], lazy[MAXN*4], parW[MAXN]; int cnt = 0, disc[MAXN], st[MAXN], en[MAXN]; int vis[MAXN], par[MAXN]; void dfs1(int u, int p) { for (int i: adj[u]) { int v; ll w; tie(v, w) = edg[i]; if (v == p) continue; dfs1(v, u); sm[u] += sm[v] + w; mx[u] = max(mx[u], mx[v] + w); } } void dfs2(int u, int p, ll m) { int i1 = -1, i2 = -1; ll w1 = 0, w2 = 0; mx[u] = max(mx[u], m); for (int i: adj[u]) { int v; ll w; tie(v, w) = edg[i]; if (v == p) continue; sm[v] = sm[u] - w + edg[i ^ 1].se; if (mx[v] + w > w1) { tie(i2, w2) = mp(i1, w1); tie(i1, w1) = mp(v, mx[v] + w); } else if (mx[v] + w > w2) { tie(i2, w2) = mp(v, mx[v] + w); } } for (int i: adj[u]) { int v; ll w; tie(v, w) = edg[i]; if (v == p) continue; if (v == i1) dfs2(v, u, max(m, w2) + edg[i ^ 1].se); else dfs2(v, u, max(m, w1) + edg[i ^ 1].se); } } void dfs3(int u, int p, ll d) { disc[cnt] = u; st[u] = cnt++; depth[u] = d; for (int i: adj[u]) { int v; ll w; tie(v, w) = edg[i]; if (v == p) continue; par[v] = u; parW[v] = w; dfs3(v, u, d + w); } en[u] = cnt; } void prop(int l, int r, int k) { if (l != r) { lazy[k*2] += lazy[k]; seg[k*2] += lazy[k]; lazy[k*2+1] += lazy[k]; seg[k*2+1] += lazy[k]; } lazy[k] = 0; } void build(int l, int r, int k) { if (l == r) { seg[k] = depth[disc[l]]; return; } int m = (l + r) / 2; build(l, m, k*2); build(m+1, r, k*2+1); seg[k] = max(seg[k*2], seg[k*2+1]); } int qry(int l, int r, int k) { if (seg[k] == 0) return -1; if (l == r) return disc[l]; prop(l,r,k); int m = (l+r) / 2; if (seg[k*2] == seg[k]) return qry(l,m,k*2); return qry(m+1,r,k*2+1); } void upd(int l, int r, int k, int a, int b, ll val) { if (a <= l && r <= b) { seg[k] += val; lazy[k] += val; return; } prop(l,r,k); int m = (l+r) / 2; if (a <= m) upd(l,m,k*2,a,b,val); if (b > m) upd(m+1,r,k*2+1,a,b,val); seg[k] = max(seg[k*2], seg[k*2+1]); } int main() { mem(ans, 0x3f); int n; ni(n); for (int i = 1; i < n; i++) { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); a--, b--; edg[cnt] = mp(b, c); adj[a].pb(cnt++); edg[cnt] = mp(a, d); adj[b].pb(cnt++); } dfs1(0, -1); dfs2(0, -1, 0); int root = 0; for (int i = 0; i < n; i++) { ans[1] = min(ans[1], sm[i]); if (ans[2] > sm[i] - mx[i]) root = i, ans[2] = sm[i] - mx[i]; } cnt = 1; dfs3(root, -1, 0); par[root] = root; build(1, n, 1); ll cur = sm[root]; vis[root] = 1; for (int i = 2; i <= n; i++) { ll val = seg[1]; cur -= val; ans[i] = min(ans[i], cur); int ind = qry(1,n,1); if (ind < 0) continue; while (!vis[ind]) { vis[ind] = 1; upd(1,n,1,st[ind],en[ind]-1,-parW[ind]); ind = par[ind]; } } int q; ni(q); while (q--) { int e; ni(e); prl(ans[e]); } return 0; }

Compilation message (stderr)

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:143:2: note: in expansion of macro 'ni'
  ni(n);
  ^~
designated_cities.cpp:146:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d %d %d", &a, &b, &c, &d);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:179:9: note: in expansion of macro 'ni'
  int q; ni(q);
         ^~
designated_cities.cpp:8:20: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
 #define ni(n) scanf("%d", &(n))
               ~~~~~^~~~~~~~~~~~
designated_cities.cpp:181:10: note: in expansion of macro 'ni'
   int e; ni(e);
          ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...