Submission #105140

# Submission time Handle Problem Language Result Execution time Memory
105140 2019-04-10T16:58:58 Z Just_Solve_The_Problem Designated Cities (JOI19_designated_cities) C++11
100 / 100
897 ms 69140 KB
#include <bits/stdc++.h>

#define fr first
#define sc second
#define ll long long

using namespace std;

const int N = (int)2e5 + 7;

int n;

struct T {
	pair <ll, int> tree[N * 4];
	ll add[N * 4];
	T() {
		for (int i = 0; i < N * 4; i++) {
			tree[i] = {0, 0};
		}
		memset(add, 0, sizeof add);
	}
	void push(int v, int l, int r) {
		if (!add[v]) return ;
		if (l != r) {
			add[v + v] += add[v];
			add[v + v + 1] += add[v];
		}
		tree[v].fr += add[v];
		add[v] = 0;
	}
	void upd1(int pos, ll val, int v = 1, int l = 1, int r = n) {
		push(v, l, r);
		if (l == r) {
			tree[v] = {val, l};
			return ;
		}
		int mid = (l + r) >> 1;
		if (pos <= mid) {
			upd1(pos, val, v + v, l, mid);
		} else {
			upd1(pos, val, v + v + 1, mid + 1, r);
		}
		tree[v] = max(tree[v + v], tree[v + v + 1]);
	}
	void upd(int l, int r, ll val, int v = 1, int tl = 1, int tr = n) {
		push(v, tl, tr);
		if (tl > r || tr < l) return ;
		if (l <= tl && tr <= r) {
			add[v] += val;
			push(v, tl, tr);
			return ;
		}
		int mid = (tl + tr) >> 1;
		upd(l, r, val, v + v, tl, mid);
		upd(l, r, val, v + v + 1, mid + 1, tr);
		tree[v] = max(tree[v + v], tree[v + v + 1]);
	}
	pair<ll, int> get(int l, int r, int v = 1, int tl = 1, int tr = n) {
		push(v, tl, tr);
		if (tl > r || tr < l) return {0, 0};
		if (l <= tl && tr <= r) return tree[v];
		int mid = (tl + tr) >> 1;
		return max(get(l, r, v + v, tl, mid), get(l, r, v + v + 1, mid + 1, tr));
	}
};
T tr;

vector<pair<int, int>> gr[N];
ll ans[N];
ll h[N], he[N];
pair <ll, int> dp[N];
int tin[N], tout[N], used[N], par[N], pos[N];
int tiktak;

void precalc(int v, int pr) {
	tin[v] = ++tiktak;
	par[v] = pr;
	for (auto tto : gr[v]) {
		int to = tto.fr;
		int w = tto.sc;
		if (to == pr) {
			continue;
		}
		h[v] += w;
		precalc(to, v);
		h[v] += h[to];
	}
	tout[v] = tiktak;
}

pair < int, int > ans2;

void dfs1(int v, int pr, ll H = 0) {
	ll res = 0;
	for (auto tto : gr[v]) {
		int to = tto.fr;
		int w = tto.sc;
		if (to == pr) {
			H += w;
		}
	}
	ans[1] = min(ans[1], H + h[v]);
	dp[v] = {h[v], v};
	ll mx = 1e18;
	int ind = -1;
	for (auto tto : gr[v]) {
		int to = tto.fr;
		int w = tto.sc;
		if (to == pr) continue;
		dfs1(to, v, H + h[v] - h[to] - w);
		if (mx != 1e18) {
			if (mx + dp[to].fr + h[v] - w - h[to] + H < ans[2]) {
				// cout << mx + dp[to].fr + h[v] - w - h[to] + H << ' ' << dp[ind].sc << ' ' << dp[to].sc << '\n';
				ans[2] = mx + dp[to].fr + h[v] - w - h[to] + H;
				ans2 = {dp[ind].sc, dp[to].sc};
			}
		} 
		if (dp[to].fr - w - h[to] < mx) {
			mx = dp[to].fr - w - h[to];
			ind = to;
		}
		if (dp[to].fr + H + h[v] - h[to] - w < ans[2]) {
			ans[2] = dp[to].fr + H + h[v] - h[to] - w;
			ans2 = {v, dp[to].sc};
		}
		if (make_pair(dp[to].fr + h[v] - w - h[to], dp[to].sc) < dp[v]) {
			dp[v] = make_pair(dp[to].fr + h[v] - w - h[to], dp[to].sc);
		}
	}
}

bool upper(int a, int b) {
	return tin[a] <= tin[b] && tout[a] >= tout[b];
}

void dfs2(int v, int pr, ll H = 0) {
	tin[v] = ++tiktak;
	pos[tin[v]] = v;
	par[v] = pr;
	he[v] = H;
	for (auto tto : gr[v]) {
		int to = tto.fr;
		int w = tto.sc;
		if (to == pr) continue;
		dfs2(to, v, H + (!used[to]) * w);
	}
	tout[v] = tiktak;
}

main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		ans[i] = 1e18;
		dp[i] = {1e18, 0};
	}
	for (int i = 1; i < n; i++) {
		int a, b, c, d;
		scanf("%d %d %d %d", &a, &b, &c, &d);
		gr[a].push_back({b, c});
		gr[b].push_back({a, d});
	}
	precalc(1, 1);
	dfs1(1, 1);
	used[ans2.fr] = 1;
	while (!upper(ans2.fr, ans2.sc)) {
		ans2.fr = par[ans2.fr];
		used[ans2.fr] = 1;
	}
	used[ans2.sc] = 1;
	while (!upper(ans2.sc, ans2.fr)) {
		ans2.sc = par[ans2.sc];
		used[ans2.sc] = 1;
	}
	tiktak = 0;
	dfs2(ans2.fr, ans2.sc);
	for (int i = 1; i <= n; i++) {
		tr.upd1(tin[i], he[i]);
	}
	for (int i = 3; i <= n; i++) {
		ans[i] = ans[i - 1];
		pair<ll, int> asd = tr.get(1, n);
		asd.fr = max(asd.fr, 0LL);
		ans[i] -= asd.fr;
		//cout << i << ' ' << asd.fr << endl;
		int v = pos[asd.sc];
		while (!used[v]) {
			used[v] = 1;
			for (auto tto : gr[v]) {
				int to = tto.fr;
				int w = tto.sc;
				if (used[to] || to == par[v]) continue;
				tr.upd(tin[to], tout[to], -tr.get(tin[v], tin[v]).fr);
			}
			tr.upd1(tin[v], 0);
			v = par[v];
		}
		// cout << ans[i] << ' ';
	}
	int q;
	scanf("%d", &q);
	while (q--) {
		int x;
		scanf("%d", &x);
		printf("%lld\n", ans[x]);
	}
}

Compilation message

designated_cities.cpp: In function 'void dfs1(int, int, long long int)':
designated_cities.cpp:94:5: warning: unused variable 'res' [-Wunused-variable]
  ll res = 0;
     ^~~
designated_cities.cpp: At global scope:
designated_cities.cpp:150:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
designated_cities.cpp: In function 'int main()':
designated_cities.cpp:190:9: warning: unused variable 'w' [-Wunused-variable]
     int w = tto.sc;
         ^
designated_cities.cpp:151:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:158: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:200:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &q);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:203:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &x);
   ~~~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 25 ms 23808 KB Output is correct
3 Correct 26 ms 23936 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23836 KB Output is correct
6 Correct 25 ms 23936 KB Output is correct
7 Correct 24 ms 23840 KB Output is correct
8 Correct 25 ms 23856 KB Output is correct
9 Correct 27 ms 23928 KB Output is correct
10 Correct 26 ms 23808 KB Output is correct
11 Correct 27 ms 23808 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23936 KB Output is correct
2 Correct 873 ms 42932 KB Output is correct
3 Correct 664 ms 59800 KB Output is correct
4 Correct 773 ms 42976 KB Output is correct
5 Correct 785 ms 43020 KB Output is correct
6 Correct 735 ms 45304 KB Output is correct
7 Correct 699 ms 42988 KB Output is correct
8 Correct 626 ms 60280 KB Output is correct
9 Correct 586 ms 41836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 23824 KB Output is correct
2 Correct 788 ms 43068 KB Output is correct
3 Correct 568 ms 62728 KB Output is correct
4 Correct 665 ms 43000 KB Output is correct
5 Correct 745 ms 42980 KB Output is correct
6 Correct 755 ms 45832 KB Output is correct
7 Correct 537 ms 43380 KB Output is correct
8 Correct 725 ms 54068 KB Output is correct
9 Correct 592 ms 41956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 25 ms 23808 KB Output is correct
3 Correct 26 ms 23936 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23836 KB Output is correct
6 Correct 25 ms 23936 KB Output is correct
7 Correct 24 ms 23840 KB Output is correct
8 Correct 25 ms 23856 KB Output is correct
9 Correct 27 ms 23928 KB Output is correct
10 Correct 26 ms 23808 KB Output is correct
11 Correct 27 ms 23808 KB Output is correct
12 Correct 26 ms 23936 KB Output is correct
13 Correct 29 ms 24056 KB Output is correct
14 Correct 29 ms 24184 KB Output is correct
15 Correct 29 ms 24184 KB Output is correct
16 Correct 29 ms 24044 KB Output is correct
17 Correct 30 ms 24184 KB Output is correct
18 Correct 25 ms 24056 KB Output is correct
19 Correct 29 ms 24064 KB Output is correct
20 Correct 28 ms 24212 KB Output is correct
21 Correct 30 ms 24128 KB Output is correct
22 Correct 30 ms 24156 KB Output is correct
23 Correct 29 ms 24176 KB Output is correct
24 Correct 27 ms 24064 KB Output is correct
25 Correct 25 ms 24312 KB Output is correct
26 Correct 26 ms 24184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 23936 KB Output is correct
2 Correct 873 ms 42932 KB Output is correct
3 Correct 664 ms 59800 KB Output is correct
4 Correct 773 ms 42976 KB Output is correct
5 Correct 785 ms 43020 KB Output is correct
6 Correct 735 ms 45304 KB Output is correct
7 Correct 699 ms 42988 KB Output is correct
8 Correct 626 ms 60280 KB Output is correct
9 Correct 586 ms 41836 KB Output is correct
10 Correct 24 ms 23824 KB Output is correct
11 Correct 788 ms 43068 KB Output is correct
12 Correct 568 ms 62728 KB Output is correct
13 Correct 665 ms 43000 KB Output is correct
14 Correct 745 ms 42980 KB Output is correct
15 Correct 755 ms 45832 KB Output is correct
16 Correct 537 ms 43380 KB Output is correct
17 Correct 725 ms 54068 KB Output is correct
18 Correct 592 ms 41956 KB Output is correct
19 Correct 23 ms 23936 KB Output is correct
20 Correct 767 ms 43004 KB Output is correct
21 Correct 674 ms 69140 KB Output is correct
22 Correct 777 ms 47972 KB Output is correct
23 Correct 822 ms 49240 KB Output is correct
24 Correct 744 ms 48000 KB Output is correct
25 Correct 814 ms 49088 KB Output is correct
26 Correct 714 ms 48008 KB Output is correct
27 Correct 765 ms 49132 KB Output is correct
28 Correct 823 ms 51364 KB Output is correct
29 Correct 809 ms 49328 KB Output is correct
30 Correct 730 ms 48408 KB Output is correct
31 Correct 711 ms 49388 KB Output is correct
32 Correct 650 ms 60816 KB Output is correct
33 Correct 649 ms 48224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 23928 KB Output is correct
2 Correct 25 ms 23808 KB Output is correct
3 Correct 26 ms 23936 KB Output is correct
4 Correct 24 ms 23800 KB Output is correct
5 Correct 24 ms 23836 KB Output is correct
6 Correct 25 ms 23936 KB Output is correct
7 Correct 24 ms 23840 KB Output is correct
8 Correct 25 ms 23856 KB Output is correct
9 Correct 27 ms 23928 KB Output is correct
10 Correct 26 ms 23808 KB Output is correct
11 Correct 27 ms 23808 KB Output is correct
12 Correct 26 ms 23936 KB Output is correct
13 Correct 873 ms 42932 KB Output is correct
14 Correct 664 ms 59800 KB Output is correct
15 Correct 773 ms 42976 KB Output is correct
16 Correct 785 ms 43020 KB Output is correct
17 Correct 735 ms 45304 KB Output is correct
18 Correct 699 ms 42988 KB Output is correct
19 Correct 626 ms 60280 KB Output is correct
20 Correct 586 ms 41836 KB Output is correct
21 Correct 24 ms 23824 KB Output is correct
22 Correct 788 ms 43068 KB Output is correct
23 Correct 568 ms 62728 KB Output is correct
24 Correct 665 ms 43000 KB Output is correct
25 Correct 745 ms 42980 KB Output is correct
26 Correct 755 ms 45832 KB Output is correct
27 Correct 537 ms 43380 KB Output is correct
28 Correct 725 ms 54068 KB Output is correct
29 Correct 592 ms 41956 KB Output is correct
30 Correct 26 ms 23936 KB Output is correct
31 Correct 29 ms 24056 KB Output is correct
32 Correct 29 ms 24184 KB Output is correct
33 Correct 29 ms 24184 KB Output is correct
34 Correct 29 ms 24044 KB Output is correct
35 Correct 30 ms 24184 KB Output is correct
36 Correct 25 ms 24056 KB Output is correct
37 Correct 29 ms 24064 KB Output is correct
38 Correct 28 ms 24212 KB Output is correct
39 Correct 30 ms 24128 KB Output is correct
40 Correct 30 ms 24156 KB Output is correct
41 Correct 29 ms 24176 KB Output is correct
42 Correct 27 ms 24064 KB Output is correct
43 Correct 25 ms 24312 KB Output is correct
44 Correct 26 ms 24184 KB Output is correct
45 Correct 23 ms 23936 KB Output is correct
46 Correct 767 ms 43004 KB Output is correct
47 Correct 674 ms 69140 KB Output is correct
48 Correct 777 ms 47972 KB Output is correct
49 Correct 822 ms 49240 KB Output is correct
50 Correct 744 ms 48000 KB Output is correct
51 Correct 814 ms 49088 KB Output is correct
52 Correct 714 ms 48008 KB Output is correct
53 Correct 765 ms 49132 KB Output is correct
54 Correct 823 ms 51364 KB Output is correct
55 Correct 809 ms 49328 KB Output is correct
56 Correct 730 ms 48408 KB Output is correct
57 Correct 711 ms 49388 KB Output is correct
58 Correct 650 ms 60816 KB Output is correct
59 Correct 649 ms 48224 KB Output is correct
60 Correct 23 ms 23896 KB Output is correct
61 Correct 897 ms 51688 KB Output is correct
62 Correct 681 ms 67612 KB Output is correct
63 Correct 738 ms 50664 KB Output is correct
64 Correct 852 ms 52024 KB Output is correct
65 Correct 781 ms 50404 KB Output is correct
66 Correct 892 ms 51944 KB Output is correct
67 Correct 804 ms 50332 KB Output is correct
68 Correct 811 ms 51808 KB Output is correct
69 Correct 820 ms 53784 KB Output is correct
70 Correct 857 ms 51800 KB Output is correct
71 Correct 755 ms 50932 KB Output is correct
72 Correct 707 ms 52456 KB Output is correct
73 Correct 694 ms 61488 KB Output is correct
74 Correct 663 ms 52228 KB Output is correct