Submission #107027

# Submission time Handle Problem Language Result Execution time Memory
107027 2019-04-21T13:33:18 Z eriksuenderhauf Designated Cities (JOI19_designated_cities) C++11
100 / 100
856 ms 53384 KB
#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*2];
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

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 time Memory Grader output
1 Correct 10 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 9 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6784 KB Output is correct
6 Correct 10 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 10 ms 6656 KB Output is correct
9 Correct 8 ms 6656 KB Output is correct
10 Correct 10 ms 6656 KB Output is correct
11 Correct 9 ms 6656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6656 KB Output is correct
2 Correct 717 ms 38120 KB Output is correct
3 Correct 733 ms 50576 KB Output is correct
4 Correct 609 ms 37800 KB Output is correct
5 Correct 670 ms 37952 KB Output is correct
6 Correct 694 ms 39656 KB Output is correct
7 Correct 595 ms 38124 KB Output is correct
8 Correct 770 ms 50700 KB Output is correct
9 Correct 572 ms 38596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6656 KB Output is correct
2 Correct 688 ms 38040 KB Output is correct
3 Correct 763 ms 52628 KB Output is correct
4 Correct 598 ms 38120 KB Output is correct
5 Correct 713 ms 38372 KB Output is correct
6 Correct 787 ms 40504 KB Output is correct
7 Correct 649 ms 39132 KB Output is correct
8 Correct 789 ms 46400 KB Output is correct
9 Correct 580 ms 39232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 9 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6784 KB Output is correct
6 Correct 10 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 10 ms 6656 KB Output is correct
9 Correct 8 ms 6656 KB Output is correct
10 Correct 10 ms 6656 KB Output is correct
11 Correct 9 ms 6656 KB Output is correct
12 Correct 9 ms 6656 KB Output is correct
13 Correct 12 ms 7012 KB Output is correct
14 Correct 11 ms 7120 KB Output is correct
15 Correct 12 ms 6908 KB Output is correct
16 Correct 14 ms 6928 KB Output is correct
17 Correct 11 ms 7032 KB Output is correct
18 Correct 13 ms 6924 KB Output is correct
19 Correct 13 ms 6784 KB Output is correct
20 Correct 14 ms 7028 KB Output is correct
21 Correct 13 ms 7036 KB Output is correct
22 Correct 13 ms 6916 KB Output is correct
23 Correct 13 ms 6908 KB Output is correct
24 Correct 14 ms 7012 KB Output is correct
25 Correct 13 ms 7040 KB Output is correct
26 Correct 12 ms 7040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 6656 KB Output is correct
2 Correct 717 ms 38120 KB Output is correct
3 Correct 733 ms 50576 KB Output is correct
4 Correct 609 ms 37800 KB Output is correct
5 Correct 670 ms 37952 KB Output is correct
6 Correct 694 ms 39656 KB Output is correct
7 Correct 595 ms 38124 KB Output is correct
8 Correct 770 ms 50700 KB Output is correct
9 Correct 572 ms 38596 KB Output is correct
10 Correct 10 ms 6656 KB Output is correct
11 Correct 688 ms 38040 KB Output is correct
12 Correct 763 ms 52628 KB Output is correct
13 Correct 598 ms 38120 KB Output is correct
14 Correct 713 ms 38372 KB Output is correct
15 Correct 787 ms 40504 KB Output is correct
16 Correct 649 ms 39132 KB Output is correct
17 Correct 789 ms 46400 KB Output is correct
18 Correct 580 ms 39232 KB Output is correct
19 Correct 9 ms 6656 KB Output is correct
20 Correct 752 ms 38532 KB Output is correct
21 Correct 759 ms 53384 KB Output is correct
22 Correct 631 ms 38456 KB Output is correct
23 Correct 741 ms 38636 KB Output is correct
24 Correct 738 ms 38600 KB Output is correct
25 Correct 744 ms 38648 KB Output is correct
26 Correct 707 ms 38500 KB Output is correct
27 Correct 709 ms 38700 KB Output is correct
28 Correct 754 ms 40088 KB Output is correct
29 Correct 666 ms 38760 KB Output is correct
30 Correct 707 ms 38780 KB Output is correct
31 Correct 616 ms 38940 KB Output is correct
32 Correct 772 ms 47104 KB Output is correct
33 Correct 596 ms 39340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 6656 KB Output is correct
2 Correct 9 ms 6656 KB Output is correct
3 Correct 9 ms 6656 KB Output is correct
4 Correct 9 ms 6656 KB Output is correct
5 Correct 10 ms 6784 KB Output is correct
6 Correct 10 ms 6656 KB Output is correct
7 Correct 7 ms 6656 KB Output is correct
8 Correct 10 ms 6656 KB Output is correct
9 Correct 8 ms 6656 KB Output is correct
10 Correct 10 ms 6656 KB Output is correct
11 Correct 9 ms 6656 KB Output is correct
12 Correct 9 ms 6656 KB Output is correct
13 Correct 717 ms 38120 KB Output is correct
14 Correct 733 ms 50576 KB Output is correct
15 Correct 609 ms 37800 KB Output is correct
16 Correct 670 ms 37952 KB Output is correct
17 Correct 694 ms 39656 KB Output is correct
18 Correct 595 ms 38124 KB Output is correct
19 Correct 770 ms 50700 KB Output is correct
20 Correct 572 ms 38596 KB Output is correct
21 Correct 10 ms 6656 KB Output is correct
22 Correct 688 ms 38040 KB Output is correct
23 Correct 763 ms 52628 KB Output is correct
24 Correct 598 ms 38120 KB Output is correct
25 Correct 713 ms 38372 KB Output is correct
26 Correct 787 ms 40504 KB Output is correct
27 Correct 649 ms 39132 KB Output is correct
28 Correct 789 ms 46400 KB Output is correct
29 Correct 580 ms 39232 KB Output is correct
30 Correct 9 ms 6656 KB Output is correct
31 Correct 12 ms 7012 KB Output is correct
32 Correct 11 ms 7120 KB Output is correct
33 Correct 12 ms 6908 KB Output is correct
34 Correct 14 ms 6928 KB Output is correct
35 Correct 11 ms 7032 KB Output is correct
36 Correct 13 ms 6924 KB Output is correct
37 Correct 13 ms 6784 KB Output is correct
38 Correct 14 ms 7028 KB Output is correct
39 Correct 13 ms 7036 KB Output is correct
40 Correct 13 ms 6916 KB Output is correct
41 Correct 13 ms 6908 KB Output is correct
42 Correct 14 ms 7012 KB Output is correct
43 Correct 13 ms 7040 KB Output is correct
44 Correct 12 ms 7040 KB Output is correct
45 Correct 9 ms 6656 KB Output is correct
46 Correct 752 ms 38532 KB Output is correct
47 Correct 759 ms 53384 KB Output is correct
48 Correct 631 ms 38456 KB Output is correct
49 Correct 741 ms 38636 KB Output is correct
50 Correct 738 ms 38600 KB Output is correct
51 Correct 744 ms 38648 KB Output is correct
52 Correct 707 ms 38500 KB Output is correct
53 Correct 709 ms 38700 KB Output is correct
54 Correct 754 ms 40088 KB Output is correct
55 Correct 666 ms 38760 KB Output is correct
56 Correct 707 ms 38780 KB Output is correct
57 Correct 616 ms 38940 KB Output is correct
58 Correct 772 ms 47104 KB Output is correct
59 Correct 596 ms 39340 KB Output is correct
60 Correct 9 ms 6744 KB Output is correct
61 Correct 729 ms 39904 KB Output is correct
62 Correct 814 ms 51652 KB Output is correct
63 Correct 702 ms 40068 KB Output is correct
64 Correct 742 ms 40012 KB Output is correct
65 Correct 746 ms 39880 KB Output is correct
66 Correct 763 ms 40008 KB Output is correct
67 Correct 748 ms 39704 KB Output is correct
68 Correct 737 ms 39996 KB Output is correct
69 Correct 763 ms 41460 KB Output is correct
70 Correct 845 ms 39884 KB Output is correct
71 Correct 725 ms 40104 KB Output is correct
72 Correct 665 ms 40808 KB Output is correct
73 Correct 856 ms 46764 KB Output is correct
74 Correct 646 ms 42008 KB Output is correct