답안 #107024

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
107024 2019-04-21T13:30:25 Z eriksuenderhauf Designated Cities (JOI19_designated_cities) C++11
100 / 100
914 ms 59104 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);
          ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 8 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 8 ms 6656 KB Output is correct
6 Correct 8 ms 6656 KB Output is correct
7 Correct 10 ms 6656 KB Output is correct
8 Correct 9 ms 6656 KB Output is correct
9 Correct 9 ms 6656 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 10 ms 6656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 847 ms 40676 KB Output is correct
3 Correct 834 ms 56500 KB Output is correct
4 Correct 750 ms 42572 KB Output is correct
5 Correct 751 ms 43984 KB Output is correct
6 Correct 761 ms 45680 KB Output is correct
7 Correct 670 ms 44464 KB Output is correct
8 Correct 798 ms 56952 KB Output is correct
9 Correct 693 ms 44776 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 770 ms 40540 KB Output is correct
3 Correct 785 ms 58744 KB Output is correct
4 Correct 643 ms 42740 KB Output is correct
5 Correct 768 ms 44164 KB Output is correct
6 Correct 825 ms 46112 KB Output is correct
7 Correct 674 ms 44668 KB Output is correct
8 Correct 796 ms 51920 KB Output is correct
9 Correct 681 ms 44524 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 8 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 8 ms 6656 KB Output is correct
6 Correct 8 ms 6656 KB Output is correct
7 Correct 10 ms 6656 KB Output is correct
8 Correct 9 ms 6656 KB Output is correct
9 Correct 9 ms 6656 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 10 ms 6656 KB Output is correct
12 Correct 5 ms 6656 KB Output is correct
13 Correct 13 ms 6784 KB Output is correct
14 Correct 14 ms 7040 KB Output is correct
15 Correct 13 ms 7040 KB Output is correct
16 Correct 11 ms 6912 KB Output is correct
17 Correct 15 ms 6912 KB Output is correct
18 Correct 11 ms 7040 KB Output is correct
19 Correct 11 ms 6912 KB Output is correct
20 Correct 12 ms 7040 KB Output is correct
21 Correct 12 ms 6912 KB Output is correct
22 Correct 11 ms 6912 KB Output is correct
23 Correct 9 ms 6912 KB Output is correct
24 Correct 14 ms 7040 KB Output is correct
25 Correct 14 ms 7168 KB Output is correct
26 Correct 13 ms 7040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 847 ms 40676 KB Output is correct
3 Correct 834 ms 56500 KB Output is correct
4 Correct 750 ms 42572 KB Output is correct
5 Correct 751 ms 43984 KB Output is correct
6 Correct 761 ms 45680 KB Output is correct
7 Correct 670 ms 44464 KB Output is correct
8 Correct 798 ms 56952 KB Output is correct
9 Correct 693 ms 44776 KB Output is correct
10 Correct 8 ms 6656 KB Output is correct
11 Correct 770 ms 40540 KB Output is correct
12 Correct 785 ms 58744 KB Output is correct
13 Correct 643 ms 42740 KB Output is correct
14 Correct 768 ms 44164 KB Output is correct
15 Correct 825 ms 46112 KB Output is correct
16 Correct 674 ms 44668 KB Output is correct
17 Correct 796 ms 51920 KB Output is correct
18 Correct 681 ms 44524 KB Output is correct
19 Correct 8 ms 6656 KB Output is correct
20 Correct 737 ms 44124 KB Output is correct
21 Correct 823 ms 59104 KB Output is correct
22 Correct 613 ms 43000 KB Output is correct
23 Correct 719 ms 44128 KB Output is correct
24 Correct 748 ms 43048 KB Output is correct
25 Correct 760 ms 44096 KB Output is correct
26 Correct 727 ms 42916 KB Output is correct
27 Correct 681 ms 44236 KB Output is correct
28 Correct 785 ms 45816 KB Output is correct
29 Correct 731 ms 44192 KB Output is correct
30 Correct 644 ms 43392 KB Output is correct
31 Correct 591 ms 44552 KB Output is correct
32 Correct 710 ms 52728 KB Output is correct
33 Correct 549 ms 44868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 6656 KB Output is correct
2 Correct 8 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 8 ms 6656 KB Output is correct
6 Correct 8 ms 6656 KB Output is correct
7 Correct 10 ms 6656 KB Output is correct
8 Correct 9 ms 6656 KB Output is correct
9 Correct 9 ms 6656 KB Output is correct
10 Correct 9 ms 6656 KB Output is correct
11 Correct 10 ms 6656 KB Output is correct
12 Correct 8 ms 6656 KB Output is correct
13 Correct 847 ms 40676 KB Output is correct
14 Correct 834 ms 56500 KB Output is correct
15 Correct 750 ms 42572 KB Output is correct
16 Correct 751 ms 43984 KB Output is correct
17 Correct 761 ms 45680 KB Output is correct
18 Correct 670 ms 44464 KB Output is correct
19 Correct 798 ms 56952 KB Output is correct
20 Correct 693 ms 44776 KB Output is correct
21 Correct 8 ms 6656 KB Output is correct
22 Correct 770 ms 40540 KB Output is correct
23 Correct 785 ms 58744 KB Output is correct
24 Correct 643 ms 42740 KB Output is correct
25 Correct 768 ms 44164 KB Output is correct
26 Correct 825 ms 46112 KB Output is correct
27 Correct 674 ms 44668 KB Output is correct
28 Correct 796 ms 51920 KB Output is correct
29 Correct 681 ms 44524 KB Output is correct
30 Correct 5 ms 6656 KB Output is correct
31 Correct 13 ms 6784 KB Output is correct
32 Correct 14 ms 7040 KB Output is correct
33 Correct 13 ms 7040 KB Output is correct
34 Correct 11 ms 6912 KB Output is correct
35 Correct 15 ms 6912 KB Output is correct
36 Correct 11 ms 7040 KB Output is correct
37 Correct 11 ms 6912 KB Output is correct
38 Correct 12 ms 7040 KB Output is correct
39 Correct 12 ms 6912 KB Output is correct
40 Correct 11 ms 6912 KB Output is correct
41 Correct 9 ms 6912 KB Output is correct
42 Correct 14 ms 7040 KB Output is correct
43 Correct 14 ms 7168 KB Output is correct
44 Correct 13 ms 7040 KB Output is correct
45 Correct 8 ms 6656 KB Output is correct
46 Correct 737 ms 44124 KB Output is correct
47 Correct 823 ms 59104 KB Output is correct
48 Correct 613 ms 43000 KB Output is correct
49 Correct 719 ms 44128 KB Output is correct
50 Correct 748 ms 43048 KB Output is correct
51 Correct 760 ms 44096 KB Output is correct
52 Correct 727 ms 42916 KB Output is correct
53 Correct 681 ms 44236 KB Output is correct
54 Correct 785 ms 45816 KB Output is correct
55 Correct 731 ms 44192 KB Output is correct
56 Correct 644 ms 43392 KB Output is correct
57 Correct 591 ms 44552 KB Output is correct
58 Correct 710 ms 52728 KB Output is correct
59 Correct 549 ms 44868 KB Output is correct
60 Correct 10 ms 6700 KB Output is correct
61 Correct 914 ms 46644 KB Output is correct
62 Correct 845 ms 58488 KB Output is correct
63 Correct 663 ms 45456 KB Output is correct
64 Correct 806 ms 46828 KB Output is correct
65 Correct 782 ms 45196 KB Output is correct
66 Correct 843 ms 46684 KB Output is correct
67 Correct 815 ms 45320 KB Output is correct
68 Correct 696 ms 46972 KB Output is correct
69 Correct 725 ms 48424 KB Output is correct
70 Correct 715 ms 46812 KB Output is correct
71 Correct 779 ms 46196 KB Output is correct
72 Correct 664 ms 47852 KB Output is correct
73 Correct 803 ms 54452 KB Output is correct
74 Correct 661 ms 48952 KB Output is correct