답안 #999751

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
999751 2024-06-16T06:17:03 Z vjudge1 Construction of Highway (JOI18_construction) C++17
100 / 100
656 ms 47224 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define inf 0x3F3F3F3F3F3F3F3F

struct BIT
{
	int n;
	vector<int> ft;
	void init(int N)
	{
		n = N + 10;
		ft.assign(n + 10, 0);
	}
	void add(int pos, int val)
	{
		for (pos = pos + 5; pos <= n; pos += pos & -pos) ft[pos] += val;
	}
	int get(int pos, int res = 0)
	{
		for (pos = pos + 5; pos > 0; pos -= pos & -pos) res += ft[pos];
		return res;
	}
};

struct SegTree
{
	int sz;
	vector<array<int, 2>> st;
	vector<int> lz;
	void init(int n)
	{
		st.assign((n + 1) << 2, {-inf, inf});
		lz.assign((n + 1) << 2, -1);
	}
	void relax(int l, int r, int x)
	{
		if (lz[x] == -1) return;
		st[x][0] = st[x][1] = lz[x];
		if (l == r)
		{
			lz[x] = -1;
			return;
		}
		lz[2*x] = lz[2*x + 1] = lz[x];
		lz[x] = -1;
	}
	array<int, 2> combine(array<int, 2> a, array<int, 2> b)
	{
		return {max(a[0], b[0]), min(a[1], b[1])};
	}
	void make(int l, int r, int x, int lx, int rx, int val)
	{
		if (lx > rx) return;
		relax(l, r, x);
		if (l > rx || r < lx) return;
		if (l >= lx && r <= rx)
		{
			lz[x] = val;
			relax(l, r, x);
			return;
		}
		int mid = (l + r) >> 1;
		make(l, mid, 2*x, lx, rx, val);
		make(mid + 1, r, 2*x + 1, lx, rx, val);
		st[x] = combine(st[2*x], st[2*x + 1]);
	}
	array<int, 2> get(int l, int r, int x, int lx, int rx)
	{
		if (lx > rx) return {-inf, inf};
		if (l > rx || r < lx) return {-inf, inf};
		relax(l, r, x);
		if (l >= lx && r <= rx) return st[x];
		int mid = (l + r) >> 1;
		return combine(get(l, mid, 2*x, lx, rx), get(mid + 1, r, 2*x + 1, lx, rx));
	}
};

const int MXN = 1e5 + 5;
const int LOG = 18;

int n;
vector<int> adj[MXN];
int sz[MXN], p[LOG][MXN], dep[MXN], head[MXN], col[MXN];
int in[MXN], out[MXN], tim;

void _init(int a)
{
	sz[a] = 1;
	int mx = 0;
	for (int i = 0; i < adj[a].size(); i++)
	{
		dep[adj[a][i]] = dep[a] + 1;
		_init(adj[a][i]);
		sz[a] += sz[adj[a][i]];
		if (sz[adj[a][i]] > sz[adj[a][mx]]) mx = i;
	}
	if (!adj[a].empty()) swap(adj[a][mx], adj[a][0]);
}

void dfs(int a)
{
	in[a] = ++tim;
	for (int i = 0; i < adj[a].size(); i++)
	{
		if (!i) head[adj[a][i]] = head[a];
		else head[adj[a][i]] = adj[a][i];
		dfs(adj[a][i]);
	}
	out[a] = tim;
}

BIT ft;
SegTree st;

void upd(int a, int val)
{
	while (a != 1)
	{
		if (head[a] == a)
		{
			col[a] = val;
			a = p[0][a];
		}
		else
		{
			st.make(1, n, 1, in[head[a]] + 1, in[a], val);
			a = head[a];
		}
	}
	col[1] = val;
}
vector<array<int, 2>> tmp;
int handle(int l, int r)
{
	int ans = 0;
	while (r >= l)
	{
		int lx = l, rx = r;
		int val = st.get(1, n, 1, r, r)[0];
		while (lx < rx)
		{
			int mid = (lx + rx) >> 1;
			array<int, 2> x = st.get(1, n, 1, mid, r);
			if (x[0] == x[1]) rx = mid;
			else lx = mid + 1;
		}
		ans += ft.get(val - 1) * (r - lx + 1);
		tmp.push_back({val, r - lx + 1});
		ft.add(val, r - lx + 1);
		r = lx - 1;
	}
	return ans;
}
int getans(int a)
{
	int res = 0;
	while (a != 1)
	{
		if (head[a] == a)
		{
			res += ft.get(col[a] - 1);
			ft.add(col[a], 1);
			tmp.push_back({col[a], 1});
			a = p[0][a];
		}
		else
		{
			res += handle(in[head[a]] + 1, in[a]);
			a = head[a];
		}
	}
	res += ft.get(col[1] - 1);
	while (!tmp.empty())
	{
		ft.add(tmp.back()[0], -tmp.back()[1]);
		tmp.pop_back();
	}
	return res;
}

vector<int> mp;

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	st.init(n);
	for (int i = 1; i <= n; i++) cin >> col[i], mp.push_back(col[i]);
	sort(mp.begin(), mp.end());
	mp.resize(unique(mp.begin(), mp.end()) - mp.begin());
	for (int i = 1; i <= n; i++) col[i] = lower_bound(mp.begin(), mp.end(), col[i]) - mp.begin() + 1;
	ft.init(mp.size());
	vector<array<int, 2>> e;
	head[1] = 1;
	for (int i = 1; i < n; i++)
	{
		int u, v;
		cin >> u >> v;
		e.push_back({u, v});
		p[0][v] = u;
		adj[u].push_back(v);
	}
	p[0][1] = 1;
	for (int i = 1; i < LOG; i++) for (int j = 1; j <= n; j++) p[i][j] = p[i - 1][p[i - 1][j]];
	_init(1);
	dfs(1);
	st.make(1, n, 1, 1, 1, col[1]);
	for (int i = 0; i < e.size(); i++)
	{
		cout << getans(e[i][0]) << '\n';
		upd(e[i][1], col[e[i][1]]);
	}
}

Compilation message

construction.cpp: In function 'void _init(long long int)':
construction.cpp:93:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |  for (int i = 0; i < adj[a].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~
construction.cpp: In function 'void dfs(long long int)':
construction.cpp:106:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  106 |  for (int i = 0; i < adj[a].size(); i++)
      |                  ~~^~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:212:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  212 |  for (int i = 0; i < e.size(); i++)
      |                  ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21340 KB Output is correct
2 Correct 3 ms 21340 KB Output is correct
3 Correct 3 ms 21340 KB Output is correct
4 Correct 3 ms 21340 KB Output is correct
5 Correct 3 ms 21496 KB Output is correct
6 Correct 3 ms 21340 KB Output is correct
7 Correct 3 ms 21492 KB Output is correct
8 Correct 4 ms 21340 KB Output is correct
9 Correct 3 ms 21340 KB Output is correct
10 Correct 3 ms 21340 KB Output is correct
11 Correct 3 ms 21340 KB Output is correct
12 Correct 3 ms 21340 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 3 ms 21340 KB Output is correct
15 Correct 3 ms 21340 KB Output is correct
16 Correct 3 ms 21340 KB Output is correct
17 Correct 3 ms 21340 KB Output is correct
18 Correct 3 ms 21340 KB Output is correct
19 Correct 3 ms 21340 KB Output is correct
20 Correct 4 ms 21340 KB Output is correct
21 Correct 3 ms 21472 KB Output is correct
22 Correct 3 ms 21340 KB Output is correct
23 Correct 3 ms 21340 KB Output is correct
24 Correct 3 ms 21340 KB Output is correct
25 Correct 3 ms 21340 KB Output is correct
26 Correct 4 ms 21340 KB Output is correct
27 Correct 4 ms 21500 KB Output is correct
28 Correct 3 ms 21336 KB Output is correct
29 Correct 3 ms 21484 KB Output is correct
30 Correct 3 ms 21340 KB Output is correct
31 Correct 3 ms 21336 KB Output is correct
32 Correct 3 ms 21340 KB Output is correct
33 Correct 3 ms 21464 KB Output is correct
34 Correct 3 ms 21340 KB Output is correct
35 Correct 3 ms 21340 KB Output is correct
36 Correct 3 ms 21340 KB Output is correct
37 Correct 3 ms 21340 KB Output is correct
38 Correct 3 ms 21340 KB Output is correct
39 Correct 4 ms 21340 KB Output is correct
40 Correct 3 ms 21336 KB Output is correct
41 Correct 3 ms 21340 KB Output is correct
42 Correct 3 ms 21340 KB Output is correct
43 Correct 3 ms 21340 KB Output is correct
44 Correct 3 ms 21340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21340 KB Output is correct
2 Correct 3 ms 21340 KB Output is correct
3 Correct 3 ms 21340 KB Output is correct
4 Correct 3 ms 21340 KB Output is correct
5 Correct 3 ms 21496 KB Output is correct
6 Correct 3 ms 21340 KB Output is correct
7 Correct 3 ms 21492 KB Output is correct
8 Correct 4 ms 21340 KB Output is correct
9 Correct 3 ms 21340 KB Output is correct
10 Correct 3 ms 21340 KB Output is correct
11 Correct 3 ms 21340 KB Output is correct
12 Correct 3 ms 21340 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 3 ms 21340 KB Output is correct
15 Correct 3 ms 21340 KB Output is correct
16 Correct 3 ms 21340 KB Output is correct
17 Correct 3 ms 21340 KB Output is correct
18 Correct 3 ms 21340 KB Output is correct
19 Correct 3 ms 21340 KB Output is correct
20 Correct 4 ms 21340 KB Output is correct
21 Correct 3 ms 21472 KB Output is correct
22 Correct 3 ms 21340 KB Output is correct
23 Correct 3 ms 21340 KB Output is correct
24 Correct 3 ms 21340 KB Output is correct
25 Correct 3 ms 21340 KB Output is correct
26 Correct 4 ms 21340 KB Output is correct
27 Correct 4 ms 21500 KB Output is correct
28 Correct 3 ms 21336 KB Output is correct
29 Correct 3 ms 21484 KB Output is correct
30 Correct 3 ms 21340 KB Output is correct
31 Correct 3 ms 21336 KB Output is correct
32 Correct 3 ms 21340 KB Output is correct
33 Correct 3 ms 21464 KB Output is correct
34 Correct 3 ms 21340 KB Output is correct
35 Correct 3 ms 21340 KB Output is correct
36 Correct 3 ms 21340 KB Output is correct
37 Correct 3 ms 21340 KB Output is correct
38 Correct 3 ms 21340 KB Output is correct
39 Correct 4 ms 21340 KB Output is correct
40 Correct 3 ms 21336 KB Output is correct
41 Correct 3 ms 21340 KB Output is correct
42 Correct 3 ms 21340 KB Output is correct
43 Correct 3 ms 21340 KB Output is correct
44 Correct 3 ms 21340 KB Output is correct
45 Correct 4 ms 21596 KB Output is correct
46 Correct 11 ms 22104 KB Output is correct
47 Correct 9 ms 22104 KB Output is correct
48 Correct 8 ms 22108 KB Output is correct
49 Correct 16 ms 22620 KB Output is correct
50 Correct 12 ms 22364 KB Output is correct
51 Correct 12 ms 22408 KB Output is correct
52 Correct 14 ms 22100 KB Output is correct
53 Correct 13 ms 22108 KB Output is correct
54 Correct 13 ms 22108 KB Output is correct
55 Correct 14 ms 22104 KB Output is correct
56 Correct 14 ms 22152 KB Output is correct
57 Correct 10 ms 22108 KB Output is correct
58 Correct 10 ms 22104 KB Output is correct
59 Correct 9 ms 22108 KB Output is correct
60 Correct 10 ms 22108 KB Output is correct
61 Correct 14 ms 22104 KB Output is correct
62 Correct 15 ms 22080 KB Output is correct
63 Correct 15 ms 22104 KB Output is correct
64 Correct 7 ms 22084 KB Output is correct
65 Correct 8 ms 21940 KB Output is correct
66 Correct 9 ms 21848 KB Output is correct
67 Correct 9 ms 21848 KB Output is correct
68 Correct 11 ms 22364 KB Output is correct
69 Correct 14 ms 22284 KB Output is correct
70 Correct 13 ms 22108 KB Output is correct
71 Correct 12 ms 22256 KB Output is correct
72 Correct 10 ms 22108 KB Output is correct
73 Correct 9 ms 22072 KB Output is correct
74 Correct 14 ms 22108 KB Output is correct
75 Correct 12 ms 22104 KB Output is correct
76 Correct 12 ms 22108 KB Output is correct
77 Correct 12 ms 22104 KB Output is correct
78 Correct 12 ms 22104 KB Output is correct
79 Correct 12 ms 22144 KB Output is correct
80 Correct 11 ms 22104 KB Output is correct
81 Correct 14 ms 22216 KB Output is correct
82 Correct 13 ms 22108 KB Output is correct
83 Correct 12 ms 22176 KB Output is correct
84 Correct 13 ms 22108 KB Output is correct
85 Correct 12 ms 21896 KB Output is correct
86 Correct 12 ms 22108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21340 KB Output is correct
2 Correct 3 ms 21340 KB Output is correct
3 Correct 3 ms 21340 KB Output is correct
4 Correct 3 ms 21340 KB Output is correct
5 Correct 3 ms 21496 KB Output is correct
6 Correct 3 ms 21340 KB Output is correct
7 Correct 3 ms 21492 KB Output is correct
8 Correct 4 ms 21340 KB Output is correct
9 Correct 3 ms 21340 KB Output is correct
10 Correct 3 ms 21340 KB Output is correct
11 Correct 3 ms 21340 KB Output is correct
12 Correct 3 ms 21340 KB Output is correct
13 Correct 4 ms 21340 KB Output is correct
14 Correct 3 ms 21340 KB Output is correct
15 Correct 3 ms 21340 KB Output is correct
16 Correct 3 ms 21340 KB Output is correct
17 Correct 3 ms 21340 KB Output is correct
18 Correct 3 ms 21340 KB Output is correct
19 Correct 3 ms 21340 KB Output is correct
20 Correct 4 ms 21340 KB Output is correct
21 Correct 3 ms 21472 KB Output is correct
22 Correct 3 ms 21340 KB Output is correct
23 Correct 3 ms 21340 KB Output is correct
24 Correct 3 ms 21340 KB Output is correct
25 Correct 3 ms 21340 KB Output is correct
26 Correct 4 ms 21340 KB Output is correct
27 Correct 4 ms 21500 KB Output is correct
28 Correct 3 ms 21336 KB Output is correct
29 Correct 3 ms 21484 KB Output is correct
30 Correct 3 ms 21340 KB Output is correct
31 Correct 3 ms 21336 KB Output is correct
32 Correct 3 ms 21340 KB Output is correct
33 Correct 3 ms 21464 KB Output is correct
34 Correct 3 ms 21340 KB Output is correct
35 Correct 3 ms 21340 KB Output is correct
36 Correct 3 ms 21340 KB Output is correct
37 Correct 3 ms 21340 KB Output is correct
38 Correct 3 ms 21340 KB Output is correct
39 Correct 4 ms 21340 KB Output is correct
40 Correct 3 ms 21336 KB Output is correct
41 Correct 3 ms 21340 KB Output is correct
42 Correct 3 ms 21340 KB Output is correct
43 Correct 3 ms 21340 KB Output is correct
44 Correct 3 ms 21340 KB Output is correct
45 Correct 4 ms 21596 KB Output is correct
46 Correct 11 ms 22104 KB Output is correct
47 Correct 9 ms 22104 KB Output is correct
48 Correct 8 ms 22108 KB Output is correct
49 Correct 16 ms 22620 KB Output is correct
50 Correct 12 ms 22364 KB Output is correct
51 Correct 12 ms 22408 KB Output is correct
52 Correct 14 ms 22100 KB Output is correct
53 Correct 13 ms 22108 KB Output is correct
54 Correct 13 ms 22108 KB Output is correct
55 Correct 14 ms 22104 KB Output is correct
56 Correct 14 ms 22152 KB Output is correct
57 Correct 10 ms 22108 KB Output is correct
58 Correct 10 ms 22104 KB Output is correct
59 Correct 9 ms 22108 KB Output is correct
60 Correct 10 ms 22108 KB Output is correct
61 Correct 14 ms 22104 KB Output is correct
62 Correct 15 ms 22080 KB Output is correct
63 Correct 15 ms 22104 KB Output is correct
64 Correct 7 ms 22084 KB Output is correct
65 Correct 8 ms 21940 KB Output is correct
66 Correct 9 ms 21848 KB Output is correct
67 Correct 9 ms 21848 KB Output is correct
68 Correct 11 ms 22364 KB Output is correct
69 Correct 14 ms 22284 KB Output is correct
70 Correct 13 ms 22108 KB Output is correct
71 Correct 12 ms 22256 KB Output is correct
72 Correct 10 ms 22108 KB Output is correct
73 Correct 9 ms 22072 KB Output is correct
74 Correct 14 ms 22108 KB Output is correct
75 Correct 12 ms 22104 KB Output is correct
76 Correct 12 ms 22108 KB Output is correct
77 Correct 12 ms 22104 KB Output is correct
78 Correct 12 ms 22104 KB Output is correct
79 Correct 12 ms 22144 KB Output is correct
80 Correct 11 ms 22104 KB Output is correct
81 Correct 14 ms 22216 KB Output is correct
82 Correct 13 ms 22108 KB Output is correct
83 Correct 12 ms 22176 KB Output is correct
84 Correct 13 ms 22108 KB Output is correct
85 Correct 12 ms 21896 KB Output is correct
86 Correct 12 ms 22108 KB Output is correct
87 Correct 29 ms 23000 KB Output is correct
88 Correct 67 ms 26444 KB Output is correct
89 Correct 264 ms 37368 KB Output is correct
90 Correct 280 ms 37576 KB Output is correct
91 Correct 283 ms 37544 KB Output is correct
92 Correct 459 ms 46540 KB Output is correct
93 Correct 444 ms 47196 KB Output is correct
94 Correct 449 ms 47224 KB Output is correct
95 Correct 576 ms 43124 KB Output is correct
96 Correct 517 ms 43700 KB Output is correct
97 Correct 522 ms 43720 KB Output is correct
98 Correct 525 ms 43680 KB Output is correct
99 Correct 532 ms 41996 KB Output is correct
100 Correct 355 ms 38084 KB Output is correct
101 Correct 433 ms 38516 KB Output is correct
102 Correct 373 ms 38344 KB Output is correct
103 Correct 387 ms 38344 KB Output is correct
104 Correct 598 ms 41928 KB Output is correct
105 Correct 600 ms 41992 KB Output is correct
106 Correct 608 ms 42024 KB Output is correct
107 Correct 202 ms 36564 KB Output is correct
108 Correct 238 ms 36684 KB Output is correct
109 Correct 298 ms 37324 KB Output is correct
110 Correct 433 ms 45900 KB Output is correct
111 Correct 571 ms 43216 KB Output is correct
112 Correct 520 ms 42184 KB Output is correct
113 Correct 526 ms 40652 KB Output is correct
114 Correct 372 ms 38084 KB Output is correct
115 Correct 387 ms 37260 KB Output is correct
116 Correct 656 ms 40652 KB Output is correct
117 Correct 523 ms 39504 KB Output is correct
118 Correct 492 ms 39000 KB Output is correct
119 Correct 494 ms 38584 KB Output is correct
120 Correct 510 ms 38540 KB Output is correct
121 Correct 499 ms 38108 KB Output is correct
122 Correct 504 ms 37452 KB Output is correct
123 Correct 645 ms 39772 KB Output is correct
124 Correct 619 ms 39116 KB Output is correct
125 Correct 579 ms 38608 KB Output is correct
126 Correct 578 ms 38720 KB Output is correct
127 Correct 574 ms 38088 KB Output is correct
128 Correct 582 ms 37580 KB Output is correct