답안 #1001183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1001183 2024-06-18T16:39:45 Z aykhn Construction of Highway (JOI18_construction) C++17
100 / 100
639 ms 47564 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 2 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 17244 KB Output is correct
4 Correct 2 ms 19292 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 3 ms 17240 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17500 KB Output is correct
9 Correct 3 ms 17376 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
11 Correct 3 ms 15256 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17400 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 17240 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 3 ms 17240 KB Output is correct
21 Correct 3 ms 17240 KB Output is correct
22 Correct 3 ms 17392 KB Output is correct
23 Correct 2 ms 17244 KB Output is correct
24 Correct 2 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 17244 KB Output is correct
28 Correct 3 ms 17244 KB Output is correct
29 Correct 3 ms 17244 KB Output is correct
30 Correct 3 ms 17240 KB Output is correct
31 Correct 3 ms 17244 KB Output is correct
32 Correct 3 ms 17244 KB Output is correct
33 Correct 3 ms 17244 KB Output is correct
34 Correct 3 ms 17244 KB Output is correct
35 Correct 3 ms 17244 KB Output is correct
36 Correct 2 ms 17244 KB Output is correct
37 Correct 2 ms 17244 KB Output is correct
38 Correct 2 ms 17244 KB Output is correct
39 Correct 3 ms 17396 KB Output is correct
40 Correct 3 ms 17244 KB Output is correct
41 Correct 3 ms 17244 KB Output is correct
42 Correct 2 ms 17244 KB Output is correct
43 Correct 3 ms 17388 KB Output is correct
44 Correct 2 ms 17456 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 17244 KB Output is correct
4 Correct 2 ms 19292 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 3 ms 17240 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17500 KB Output is correct
9 Correct 3 ms 17376 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
11 Correct 3 ms 15256 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17400 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 17240 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 3 ms 17240 KB Output is correct
21 Correct 3 ms 17240 KB Output is correct
22 Correct 3 ms 17392 KB Output is correct
23 Correct 2 ms 17244 KB Output is correct
24 Correct 2 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 17244 KB Output is correct
28 Correct 3 ms 17244 KB Output is correct
29 Correct 3 ms 17244 KB Output is correct
30 Correct 3 ms 17240 KB Output is correct
31 Correct 3 ms 17244 KB Output is correct
32 Correct 3 ms 17244 KB Output is correct
33 Correct 3 ms 17244 KB Output is correct
34 Correct 3 ms 17244 KB Output is correct
35 Correct 3 ms 17244 KB Output is correct
36 Correct 2 ms 17244 KB Output is correct
37 Correct 2 ms 17244 KB Output is correct
38 Correct 2 ms 17244 KB Output is correct
39 Correct 3 ms 17396 KB Output is correct
40 Correct 3 ms 17244 KB Output is correct
41 Correct 3 ms 17244 KB Output is correct
42 Correct 2 ms 17244 KB Output is correct
43 Correct 3 ms 17388 KB Output is correct
44 Correct 2 ms 17456 KB Output is correct
45 Correct 3 ms 17500 KB Output is correct
46 Correct 9 ms 16284 KB Output is correct
47 Correct 8 ms 18008 KB Output is correct
48 Correct 8 ms 18008 KB Output is correct
49 Correct 11 ms 18524 KB Output is correct
50 Correct 11 ms 18524 KB Output is correct
51 Correct 11 ms 18524 KB Output is correct
52 Correct 13 ms 18368 KB Output is correct
53 Correct 13 ms 18256 KB Output is correct
54 Correct 13 ms 18268 KB Output is correct
55 Correct 12 ms 18264 KB Output is correct
56 Correct 13 ms 18264 KB Output is correct
57 Correct 9 ms 18060 KB Output is correct
58 Correct 9 ms 18012 KB Output is correct
59 Correct 9 ms 18056 KB Output is correct
60 Correct 10 ms 18012 KB Output is correct
61 Correct 14 ms 18388 KB Output is correct
62 Correct 14 ms 18268 KB Output is correct
63 Correct 14 ms 18268 KB Output is correct
64 Correct 6 ms 16024 KB Output is correct
65 Correct 7 ms 18012 KB Output is correct
66 Correct 8 ms 20100 KB Output is correct
67 Correct 8 ms 18012 KB Output is correct
68 Correct 10 ms 18272 KB Output is correct
69 Correct 14 ms 18516 KB Output is correct
70 Correct 12 ms 18268 KB Output is correct
71 Correct 12 ms 18268 KB Output is correct
72 Correct 8 ms 18012 KB Output is correct
73 Correct 8 ms 18160 KB Output is correct
74 Correct 13 ms 18268 KB Output is correct
75 Correct 12 ms 18288 KB Output is correct
76 Correct 12 ms 18268 KB Output is correct
77 Correct 14 ms 18016 KB Output is correct
78 Correct 11 ms 18008 KB Output is correct
79 Correct 11 ms 18232 KB Output is correct
80 Correct 10 ms 18012 KB Output is correct
81 Correct 13 ms 18220 KB Output is correct
82 Correct 13 ms 18264 KB Output is correct
83 Correct 12 ms 18012 KB Output is correct
84 Correct 12 ms 18260 KB Output is correct
85 Correct 12 ms 18224 KB Output is correct
86 Correct 14 ms 18008 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13144 KB Output is correct
2 Correct 2 ms 13148 KB Output is correct
3 Correct 2 ms 17244 KB Output is correct
4 Correct 2 ms 19292 KB Output is correct
5 Correct 2 ms 17244 KB Output is correct
6 Correct 3 ms 17240 KB Output is correct
7 Correct 3 ms 17244 KB Output is correct
8 Correct 3 ms 17500 KB Output is correct
9 Correct 3 ms 17376 KB Output is correct
10 Correct 3 ms 17500 KB Output is correct
11 Correct 3 ms 15256 KB Output is correct
12 Correct 3 ms 17240 KB Output is correct
13 Correct 3 ms 17400 KB Output is correct
14 Correct 3 ms 17244 KB Output is correct
15 Correct 2 ms 17244 KB Output is correct
16 Correct 3 ms 17240 KB Output is correct
17 Correct 3 ms 17240 KB Output is correct
18 Correct 2 ms 17244 KB Output is correct
19 Correct 3 ms 17244 KB Output is correct
20 Correct 3 ms 17240 KB Output is correct
21 Correct 3 ms 17240 KB Output is correct
22 Correct 3 ms 17392 KB Output is correct
23 Correct 2 ms 17244 KB Output is correct
24 Correct 2 ms 17244 KB Output is correct
25 Correct 3 ms 17244 KB Output is correct
26 Correct 3 ms 17244 KB Output is correct
27 Correct 3 ms 17244 KB Output is correct
28 Correct 3 ms 17244 KB Output is correct
29 Correct 3 ms 17244 KB Output is correct
30 Correct 3 ms 17240 KB Output is correct
31 Correct 3 ms 17244 KB Output is correct
32 Correct 3 ms 17244 KB Output is correct
33 Correct 3 ms 17244 KB Output is correct
34 Correct 3 ms 17244 KB Output is correct
35 Correct 3 ms 17244 KB Output is correct
36 Correct 2 ms 17244 KB Output is correct
37 Correct 2 ms 17244 KB Output is correct
38 Correct 2 ms 17244 KB Output is correct
39 Correct 3 ms 17396 KB Output is correct
40 Correct 3 ms 17244 KB Output is correct
41 Correct 3 ms 17244 KB Output is correct
42 Correct 2 ms 17244 KB Output is correct
43 Correct 3 ms 17388 KB Output is correct
44 Correct 2 ms 17456 KB Output is correct
45 Correct 3 ms 17500 KB Output is correct
46 Correct 9 ms 16284 KB Output is correct
47 Correct 8 ms 18008 KB Output is correct
48 Correct 8 ms 18008 KB Output is correct
49 Correct 11 ms 18524 KB Output is correct
50 Correct 11 ms 18524 KB Output is correct
51 Correct 11 ms 18524 KB Output is correct
52 Correct 13 ms 18368 KB Output is correct
53 Correct 13 ms 18256 KB Output is correct
54 Correct 13 ms 18268 KB Output is correct
55 Correct 12 ms 18264 KB Output is correct
56 Correct 13 ms 18264 KB Output is correct
57 Correct 9 ms 18060 KB Output is correct
58 Correct 9 ms 18012 KB Output is correct
59 Correct 9 ms 18056 KB Output is correct
60 Correct 10 ms 18012 KB Output is correct
61 Correct 14 ms 18388 KB Output is correct
62 Correct 14 ms 18268 KB Output is correct
63 Correct 14 ms 18268 KB Output is correct
64 Correct 6 ms 16024 KB Output is correct
65 Correct 7 ms 18012 KB Output is correct
66 Correct 8 ms 20100 KB Output is correct
67 Correct 8 ms 18012 KB Output is correct
68 Correct 10 ms 18272 KB Output is correct
69 Correct 14 ms 18516 KB Output is correct
70 Correct 12 ms 18268 KB Output is correct
71 Correct 12 ms 18268 KB Output is correct
72 Correct 8 ms 18012 KB Output is correct
73 Correct 8 ms 18160 KB Output is correct
74 Correct 13 ms 18268 KB Output is correct
75 Correct 12 ms 18288 KB Output is correct
76 Correct 12 ms 18268 KB Output is correct
77 Correct 14 ms 18016 KB Output is correct
78 Correct 11 ms 18008 KB Output is correct
79 Correct 11 ms 18232 KB Output is correct
80 Correct 10 ms 18012 KB Output is correct
81 Correct 13 ms 18220 KB Output is correct
82 Correct 13 ms 18264 KB Output is correct
83 Correct 12 ms 18012 KB Output is correct
84 Correct 12 ms 18260 KB Output is correct
85 Correct 12 ms 18224 KB Output is correct
86 Correct 14 ms 18008 KB Output is correct
87 Correct 21 ms 19416 KB Output is correct
88 Correct 62 ms 23660 KB Output is correct
89 Correct 274 ms 38216 KB Output is correct
90 Correct 267 ms 38348 KB Output is correct
91 Correct 312 ms 38252 KB Output is correct
92 Correct 459 ms 47216 KB Output is correct
93 Correct 442 ms 47564 KB Output is correct
94 Correct 434 ms 47304 KB Output is correct
95 Correct 518 ms 43252 KB Output is correct
96 Correct 533 ms 43688 KB Output is correct
97 Correct 522 ms 43692 KB Output is correct
98 Correct 525 ms 43672 KB Output is correct
99 Correct 531 ms 41928 KB Output is correct
100 Correct 370 ms 38080 KB Output is correct
101 Correct 410 ms 38348 KB Output is correct
102 Correct 407 ms 38640 KB Output is correct
103 Correct 389 ms 38392 KB Output is correct
104 Correct 639 ms 41808 KB Output is correct
105 Correct 545 ms 41816 KB Output is correct
106 Correct 600 ms 41928 KB Output is correct
107 Correct 200 ms 36812 KB Output is correct
108 Correct 215 ms 36808 KB Output is correct
109 Correct 292 ms 37320 KB Output is correct
110 Correct 444 ms 45772 KB Output is correct
111 Correct 549 ms 43100 KB Output is correct
112 Correct 477 ms 42440 KB Output is correct
113 Correct 497 ms 40588 KB Output is correct
114 Correct 348 ms 38088 KB Output is correct
115 Correct 332 ms 37032 KB Output is correct
116 Correct 562 ms 40652 KB Output is correct
117 Correct 459 ms 40136 KB Output is correct
118 Correct 464 ms 38856 KB Output is correct
119 Correct 466 ms 38600 KB Output is correct
120 Correct 495 ms 38600 KB Output is correct
121 Correct 461 ms 37836 KB Output is correct
122 Correct 438 ms 37580 KB Output is correct
123 Correct 572 ms 39628 KB Output is correct
124 Correct 532 ms 38992 KB Output is correct
125 Correct 556 ms 38604 KB Output is correct
126 Correct 547 ms 38716 KB Output is correct
127 Correct 525 ms 37832 KB Output is correct
128 Correct 493 ms 37560 KB Output is correct