답안 #937803

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
937803 2024-03-04T13:28:35 Z WonderfulWhale Designated Cities (JOI19_designated_cities) C++17
100 / 100
1989 ms 139856 KB
#pragma GCC optimize "Ofast" // Not standard-compliant, alternative: O3
#pragma GCC optimize "inline" // Inlines all functions declared inline
#pragma GCC optimize "unroll-loops"
#include<bits/stdc++.h>
using namespace std;

#define int int64_t
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

int CNT = 0;

inline char gc() { // like getchar()
		static char buf[1 << 16];
			static size_t bc, be;
				if (bc >= be) {
							buf[0] = 0, bc = 0;
									be = fread(buf, 1, sizeof(buf), stdin);
										}
					return buf[bc++]; // returns 0 on EOF
}

int readInt() {
		int a, c;
			while ((a = gc()) < 40);
				if (a == '-') return -readInt();
				while ((c = gc()) >= 48) a = a * 10 + c - 480;
					return a - 48;
}

const int MAXN = 200'009;
const int INF = 1e9;

bool vis[MAXN];

struct SegTree {
	const static int T = 1<<19;
	pii t[2*T];
	int lz[2*T];
	void push(int v) {
		t[2*v].st += lz[v];
		lz[2*v] += lz[v];
		t[2*v+1].st += lz[v];
		lz[2*v+1] += lz[v];
		lz[v] = 0;
	}
	void update(int l, int r, int val, int tl=0, int tr=T-1, int v=1) {
		//if(v==1) cerr << l << " " << r << " " << val << "\n";
		if(l>r) return;
		if(l==tl&&r==tr) {
			//cerr << "adding: " << v << " " << val << "\n";
			t[v].st += val;
			lz[v] += val;
			//cerr << "new: " << t[v] << "\n";
			return;
		}
		int tm = (tl+tr)/2;
		push(v);
		update(l, min(r, tm), val, tl, tm, 2*v);
		update(max(l, tm+1), r, val, tm+1, tr, 2*v+1);
		t[v] = max(t[2*v], t[2*v+1]);
	}
	pii query(int l, int r, int tl=0, int tr=T-1, int v=1) {
		//cerr << "query: " << l <<  " " << r << " " << v << " "<<t[v] << "\n";
		if(l>r) return {-INF, -1};
		if(l==tl&&r==tr) return t[v];
		int tm = (tl+tr)/2;
		push(v);
		return max(query(l, min(r, tm), tl, tm, 2*v),
				query(max(l, tm+1), r, tm+1, tr, 2*v+1));
	}
} seg;
struct SegTree2 {
	const static int T = 1<<18;
	pii t[2*T];
	int lz[2*T];
	void push(int v) {
		t[2*v].st += lz[v];
		lz[2*v] += lz[v];
		t[2*v+1].st += lz[v];
		lz[2*v+1] += lz[v];
		lz[v] = 0;
	}
	void update(int l, int r, int val, int tl=0, int tr=T-1, int v=1) {
		//if(v==1) cerr << l << " " << r << " " << val << "\n";
		if(l>r) return;
		if(l==tl&&r==tr) {
			//cerr << "adding: " << v << " " << val << "\n";
			t[v].st += val;
			lz[v] += val;
			//cerr << "new: " << t[v] << "\n";
			return;
		}
		int tm = (tl+tr)/2;
		push(v);
		update(l, min(r, tm), val, tl, tm, 2*v);
		update(max(l, tm+1), r, val, tm+1, tr, 2*v+1);
		t[v] = max(t[2*v], t[2*v+1]);
	}
	pii query(int l, int r, int tl=0, int tr=T-1, int v=1) {
		//cerr << "query: " << l <<  " " << r << " " << v << " "<<t[v] << "\n";
		if(l>r) return {-INF, -1};
		if(l==tl&&r==tr) return t[v];
		int tm = (tl+tr)/2;
		push(v);
		return max(query(l, min(r, tm), tl, tm, 2*v),
				query(max(l, tm+1), r, tm+1, tr, 2*v+1));
	}
}seg1, seg2;

int n;
vector<pair<int, pii>> G[MAXN];
int tin[MAXN], tout[MAXN], T;
int sum[MAXN];
int heavy[MAXN];
int head[MAXN];
int pos[MAXN], P;
int par[MAXN];
pii cost[MAXN];

pii dp[MAXN];

void dfs(int x, int p=-1) {
	par[x] = p;
	tin[x] = ++T;
	seg.t[tin[x]+seg.T].nd = x;
	seg.update(tin[x], tin[x], INF);
	for(auto [y, c]:G[x]) {
		if(y==p) 
			continue;
		cost[y] = c;
		sum[x] += c.nd;
		dfs(y, x);
		sum[x] += sum[y];
	}
	tout[x] = ++T;
	seg.t[tout[x]+seg.T].nd = x;
	seg.update(tout[x], tout[x], INF);
	for(auto [y, c]:G[x]) {
		if(y==p) continue;
		//cerr << "updates: " << x << " " << y << "\n";
		seg.update(tin[y], tout[y], c.st);
		seg.update(1, tin[y]-1, c.nd);
		seg.update(tout[y]+1, 2*n, c.nd);
	}
}

pair<int, pii> start;

void dfs2(int x, int p=-1, int s=0) {
	pii best[2];
	best[0] = {0, x};
	best[1].st = -INF;
	for(auto [y, c]:G[x]) {
		if(y==p) continue;
		dfs2(y, x, s+c.st+sum[x]-sum[y]-c.nd);
		int val = dp[y].st+c.st+c.nd-sum[y]-c.nd;
		if(val>=best[0].st) {
			best[1] = best[0];
			best[0] = {val, dp[y].nd};
		} else if(val>=best[1].st) {
			best[1] = {val, dp[y].nd};
		}
	}
	start = max(start, {best[0].st+best[1].st+s+sum[x], 
			{best[0].nd, best[1].nd}});
	dp[x] = {sum[x], x};
	for(auto [y, c]:G[x]) {
		if(y==p) continue;
		dp[x] = max(dp[x], 
				{dp[y].st+c.st+c.nd+sum[x]-sum[y]-c.nd, dp[y].nd});
	}
}

int dfs3(int x, int p=-1) {
	int s = 1;
	int max_s = 0;
	for(auto [y, c]:G[x]) {
		if(y==p) continue;
		int c_s = dfs3(y, x);
		s += c_s;
		if(c_s>max_s) {
			max_s = c_s;
			heavy[x] = y;
		}
	}
	return s;
}

void decompose(int x, int h, int p=-1) {
	head[x] = h;
	pos[x] = P++;
	if(heavy[x]!=0)
		decompose(heavy[x], h, x);
	for(auto [y, c]:G[x]) {
		if(y!=p&&y!=heavy[x])
			decompose(y, y, x);
	}
	//cerr << "hld: " << x << "->" << pos[x] << "\n";
}

int ans[MAXN];

void erase_e(int x, int type) {
	CNT++;
	//cerr << "erasing edge: " << x << " " << type << "\n";
	if(type==0) {
		seg.update(1, tin[x]-1, -cost[x].nd);
		seg.update(tout[x]+1, 2*n, -cost[x].nd);
		seg1.update(pos[x], pos[x], -INF);
	}
	if(type==1) {
		seg.update(tin[x], tout[x], -cost[x].st);
		seg2.update(pos[x], pos[x], -INF);
	}
}

void erase_v(int x) {
	//cerr << "erasing v: " << x << "\n";
	seg.update(tin[x], tin[x], -INF);
	seg.update(tout[x], tout[x], -INF);
	vector<pii> segments;
	int cur = x;
	while(cur!=-1) {
		segments.pb({pos[head[cur]], pos[cur]});
		cur = par[head[cur]];
	}
	reverse(all(segments));
	for(pii x:segments) {
		//cerr << x.st << " " << x.nd << "\n";
	}
	int pre = 0;
	for(pii x:segments) {
		pii y = seg1.query(pre, x.st-1);
		//cerr << "querying: "<< pre << " " << x.st-1 << "\n";
		while(y.st>0) {
			erase_e(y.nd, 0);
			y = seg1.query(pre, x.st-1);
		}
		pre = x.nd+1;
	}
	pii Y = seg1.query(pre, n);
	while(Y.st>0) {
		erase_e(Y.nd, 0);
		Y = seg1.query(pre, n);
	}
	cur = x;
	while(cur) {
		if(!vis[cur]) {
			vis[cur] = true;
			erase_e(cur, 1);
		} else {
			break;
		}
		cur = par[cur];
	}
	//for(pii x:segments) {
		//pii y = seg2.query(x.st, x.nd);
		//while(y.st>0) {
			//erase_e(y.nd, 1);
			//y = seg2.query(x.st, x.nd);
		//}
	//}
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

	for(int i=0;i<2*seg.T;i++) {
		seg.t[i].st = -INF;
	}

	//cin >> n;
	n = readInt();
	int S = 0;
	for(int i=0;i<n-1;i++) {
		int a, b, c, d;
		//cin >> a >> b >> c >> d;
		a = readInt();
		b = readInt();
		c = readInt();
		d = readInt();
		S += c+d;
		G[a].pb({b, {c, d}});
		G[b].pb({a, {d, c}});
	}
	dfs(1);
	dfs2(1);
	dfs3(1);
	decompose(1, 1);
	for(int i=2;i<=n;i++) {
		seg1.t[pos[i]+seg1.T].nd = i;
		seg1.update(pos[i], pos[i], cost[i].nd);
		seg2.t[pos[i]+seg2.T].nd = i;
		seg2.update(pos[i], pos[i], cost[i].nd);
	}
	//cerr << "cos: " << tin[2] << "\n";
	ans[1] = seg.query(1, 2*n).st;
	ans[2] = start.st;
	//cerr << "here\n";
	erase_v(start.nd.st);
	erase_v(start.nd.nd);
	//cerr << "here\n";
	for(int i=3;i<=n;i++) {
		//cerr << seg.query(tin[2], tin[2]).st << " xd\n";
		//cerr << seg.query(tin[0], tin[0]).st << " xd\n";
		pii x = seg.query(1, 2*n);
		//cerr << "New year, new me " << x.nd << "\n";
		//cerr << "with some weird value: " << x.st << "\n";
		ans[i] = ans[i-1] + x.st;
		erase_v(x.nd);
	}
	int q;
	q = readInt();
	//cin >> q;
	while(q--) {
		int x;
		x = readInt();
		//cin >> x;
		cout << S-ans[x] << "\n";
	}
}

Compilation message

designated_cities.cpp: In function 'int64_t readInt()':
designated_cities.cpp:29:4: warning: this 'while' clause does not guard... [-Wmisleading-indentation]
   29 |    while ((a = gc()) < 40);
      |    ^~~~~
designated_cities.cpp:30:5: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'while'
   30 |     if (a == '-') return -readInt();
      |     ^~
designated_cities.cpp: In function 'void erase_v(int64_t)':
designated_cities.cpp:233:10: warning: variable 'x' set but not used [-Wunused-but-set-variable]
  233 |  for(pii x:segments) {
      |          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 62044 KB Output is correct
2 Correct 11 ms 61924 KB Output is correct
3 Correct 10 ms 62044 KB Output is correct
4 Correct 10 ms 61924 KB Output is correct
5 Correct 11 ms 61908 KB Output is correct
6 Correct 10 ms 61960 KB Output is correct
7 Correct 11 ms 62044 KB Output is correct
8 Correct 10 ms 61816 KB Output is correct
9 Correct 9 ms 62044 KB Output is correct
10 Correct 10 ms 62044 KB Output is correct
11 Correct 10 ms 62040 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 62044 KB Output is correct
2 Correct 1818 ms 89248 KB Output is correct
3 Correct 1698 ms 129656 KB Output is correct
4 Correct 1713 ms 88532 KB Output is correct
5 Correct 1633 ms 88652 KB Output is correct
6 Correct 1689 ms 94396 KB Output is correct
7 Correct 1626 ms 88216 KB Output is correct
8 Correct 1668 ms 130612 KB Output is correct
9 Correct 1635 ms 85268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 62032 KB Output is correct
2 Correct 1789 ms 88688 KB Output is correct
3 Correct 1600 ms 136148 KB Output is correct
4 Correct 1669 ms 87956 KB Output is correct
5 Correct 1758 ms 88448 KB Output is correct
6 Correct 1680 ms 95636 KB Output is correct
7 Correct 1609 ms 86732 KB Output is correct
8 Correct 1731 ms 114752 KB Output is correct
9 Correct 1518 ms 85092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 62044 KB Output is correct
2 Correct 11 ms 61924 KB Output is correct
3 Correct 10 ms 62044 KB Output is correct
4 Correct 10 ms 61924 KB Output is correct
5 Correct 11 ms 61908 KB Output is correct
6 Correct 10 ms 61960 KB Output is correct
7 Correct 11 ms 62044 KB Output is correct
8 Correct 10 ms 61816 KB Output is correct
9 Correct 9 ms 62044 KB Output is correct
10 Correct 10 ms 62044 KB Output is correct
11 Correct 10 ms 62040 KB Output is correct
12 Correct 9 ms 62044 KB Output is correct
13 Correct 20 ms 62300 KB Output is correct
14 Correct 20 ms 62556 KB Output is correct
15 Correct 21 ms 62300 KB Output is correct
16 Correct 20 ms 62416 KB Output is correct
17 Correct 20 ms 62296 KB Output is correct
18 Correct 20 ms 62300 KB Output is correct
19 Correct 20 ms 62300 KB Output is correct
20 Correct 19 ms 62308 KB Output is correct
21 Correct 21 ms 62300 KB Output is correct
22 Correct 20 ms 62300 KB Output is correct
23 Correct 20 ms 62300 KB Output is correct
24 Correct 21 ms 62300 KB Output is correct
25 Correct 19 ms 62812 KB Output is correct
26 Correct 20 ms 62300 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 62044 KB Output is correct
2 Correct 1818 ms 89248 KB Output is correct
3 Correct 1698 ms 129656 KB Output is correct
4 Correct 1713 ms 88532 KB Output is correct
5 Correct 1633 ms 88652 KB Output is correct
6 Correct 1689 ms 94396 KB Output is correct
7 Correct 1626 ms 88216 KB Output is correct
8 Correct 1668 ms 130612 KB Output is correct
9 Correct 1635 ms 85268 KB Output is correct
10 Correct 9 ms 62032 KB Output is correct
11 Correct 1789 ms 88688 KB Output is correct
12 Correct 1600 ms 136148 KB Output is correct
13 Correct 1669 ms 87956 KB Output is correct
14 Correct 1758 ms 88448 KB Output is correct
15 Correct 1680 ms 95636 KB Output is correct
16 Correct 1609 ms 86732 KB Output is correct
17 Correct 1731 ms 114752 KB Output is correct
18 Correct 1518 ms 85092 KB Output is correct
19 Correct 9 ms 62044 KB Output is correct
20 Correct 1809 ms 88528 KB Output is correct
21 Correct 1552 ms 139856 KB Output is correct
22 Correct 1708 ms 90144 KB Output is correct
23 Correct 1835 ms 92256 KB Output is correct
24 Correct 1779 ms 91076 KB Output is correct
25 Correct 1989 ms 91792 KB Output is correct
26 Correct 1837 ms 90648 KB Output is correct
27 Correct 1638 ms 91288 KB Output is correct
28 Correct 1700 ms 97024 KB Output is correct
29 Correct 1835 ms 92528 KB Output is correct
30 Correct 1709 ms 90780 KB Output is correct
31 Correct 1546 ms 90436 KB Output is correct
32 Correct 1667 ms 119604 KB Output is correct
33 Correct 1505 ms 89460 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 62044 KB Output is correct
2 Correct 11 ms 61924 KB Output is correct
3 Correct 10 ms 62044 KB Output is correct
4 Correct 10 ms 61924 KB Output is correct
5 Correct 11 ms 61908 KB Output is correct
6 Correct 10 ms 61960 KB Output is correct
7 Correct 11 ms 62044 KB Output is correct
8 Correct 10 ms 61816 KB Output is correct
9 Correct 9 ms 62044 KB Output is correct
10 Correct 10 ms 62044 KB Output is correct
11 Correct 10 ms 62040 KB Output is correct
12 Correct 10 ms 62044 KB Output is correct
13 Correct 1818 ms 89248 KB Output is correct
14 Correct 1698 ms 129656 KB Output is correct
15 Correct 1713 ms 88532 KB Output is correct
16 Correct 1633 ms 88652 KB Output is correct
17 Correct 1689 ms 94396 KB Output is correct
18 Correct 1626 ms 88216 KB Output is correct
19 Correct 1668 ms 130612 KB Output is correct
20 Correct 1635 ms 85268 KB Output is correct
21 Correct 9 ms 62032 KB Output is correct
22 Correct 1789 ms 88688 KB Output is correct
23 Correct 1600 ms 136148 KB Output is correct
24 Correct 1669 ms 87956 KB Output is correct
25 Correct 1758 ms 88448 KB Output is correct
26 Correct 1680 ms 95636 KB Output is correct
27 Correct 1609 ms 86732 KB Output is correct
28 Correct 1731 ms 114752 KB Output is correct
29 Correct 1518 ms 85092 KB Output is correct
30 Correct 9 ms 62044 KB Output is correct
31 Correct 20 ms 62300 KB Output is correct
32 Correct 20 ms 62556 KB Output is correct
33 Correct 21 ms 62300 KB Output is correct
34 Correct 20 ms 62416 KB Output is correct
35 Correct 20 ms 62296 KB Output is correct
36 Correct 20 ms 62300 KB Output is correct
37 Correct 20 ms 62300 KB Output is correct
38 Correct 19 ms 62308 KB Output is correct
39 Correct 21 ms 62300 KB Output is correct
40 Correct 20 ms 62300 KB Output is correct
41 Correct 20 ms 62300 KB Output is correct
42 Correct 21 ms 62300 KB Output is correct
43 Correct 19 ms 62812 KB Output is correct
44 Correct 20 ms 62300 KB Output is correct
45 Correct 9 ms 62044 KB Output is correct
46 Correct 1809 ms 88528 KB Output is correct
47 Correct 1552 ms 139856 KB Output is correct
48 Correct 1708 ms 90144 KB Output is correct
49 Correct 1835 ms 92256 KB Output is correct
50 Correct 1779 ms 91076 KB Output is correct
51 Correct 1989 ms 91792 KB Output is correct
52 Correct 1837 ms 90648 KB Output is correct
53 Correct 1638 ms 91288 KB Output is correct
54 Correct 1700 ms 97024 KB Output is correct
55 Correct 1835 ms 92528 KB Output is correct
56 Correct 1709 ms 90780 KB Output is correct
57 Correct 1546 ms 90436 KB Output is correct
58 Correct 1667 ms 119604 KB Output is correct
59 Correct 1505 ms 89460 KB Output is correct
60 Correct 10 ms 62044 KB Output is correct
61 Correct 1819 ms 94932 KB Output is correct
62 Correct 1614 ms 134664 KB Output is correct
63 Correct 1621 ms 93332 KB Output is correct
64 Correct 1825 ms 94848 KB Output is correct
65 Correct 1811 ms 93552 KB Output is correct
66 Correct 1912 ms 95220 KB Output is correct
67 Correct 1850 ms 93268 KB Output is correct
68 Correct 1685 ms 94576 KB Output is correct
69 Correct 1711 ms 100092 KB Output is correct
70 Correct 1796 ms 95568 KB Output is correct
71 Correct 1742 ms 93580 KB Output is correct
72 Correct 1582 ms 94208 KB Output is correct
73 Correct 1625 ms 118312 KB Output is correct
74 Correct 1524 ms 92796 KB Output is correct