답안 #620761

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
620761 2022-08-03T08:52:24 Z 장태환(#8519) Railway Trip (JOI17_railway_trip) C++17
100 / 100
363 ms 84908 KB
#include <bits/stdc++.h>
using namespace std;
int depth[100100];
pair<int,int>glca[100100][20];
pair<int, int>pos[100100];
vector<int>li[100100];
vector<vector<pair<int, int>>>cst[100100];
vector<int>ll[100100], rr[100100];
int N,treeN;
int arr[100100];
struct tree
{
	pair<int, int>stree[1 << 18];
	void upd(int p, pair<int,int>t)
	{
		p += treeN;
		stree[p] = t;
		while (p > 1)
		{
			p /= 2;
			stree[p] = max(stree[p * 2], stree[p * 2 + 1]);
		}
	}
	pair<int,int> ge(int qs, int qe, int s = 0, int e = treeN - 1, int i = 1)
	{
		if (qs > e || s > qe)
			return { 0,0 };
		if (qs <= s && e <= qe)
			return stree[i];
		return max(ge(qs, qe, s, (s + e) / 2, i * 2), ge(qs, qe, (s + e) / 2 + 1, e, i * 2 + 1));
	}
}lp,rp;
int c = 0;
void bu(pair<int,int>p,int s=0, int e=N-1,int d=0)
{
	if (s > e)
		return;
	c++;
	glca[c][0] = p;
	depth[c] = d;
	int i;
	for (i = 0; i < 19; i++)
	{
		glca[c][i + 1] = glca[glca[c][i].first][i];
	}
	int rt = -lp.ge(s, e).second;
	deque<int>r;
	r.push_back(rt);
	int cur = rt;
	while (cur<e)
	{
		cur = -lp.ge(cur + 1, e).second;
		r.push_back(cur);
	}
	cur = rt;
	while (cur > s)
	{
		cur = rp.ge(s,cur-1).second;
		r.push_front(cur);
	}
	ll[c].push_back(0);
	for (i = 0; i < r.size(); i++)
	{
		if (i)
		{
			if (arr[r[i]] > arr[r[i - 1]])
			{
				ll[c].push_back(i);
			}
			if (arr[r[i]] < arr[r[i - 1]])
			{
				rr[c].push_back(i-1);
			}
		}
		li[c].push_back(r[i]);
		pos[r[i]] = { c,i };
		vector<pair<int, int>>r;
		int j;
		for (j = 0; j < 20; j++)
		{
			r.push_back({ 0,0 });
		}
		cst[c].push_back(r);
	}
	rr[c].push_back(r.size() - 1);
	for (i = 0; i < r.size(); i++)
	{
		int lv = N, rv = N;
		int lc = lower_bound(ll[c].begin(), ll[c].end(), i) - ll[c].begin();
		if (lc)
		{
			lv = min(lv, i - ll[c][lc - 1]+1);
		}
		if (lc != ll[c].size())
		{
			lv = min(lv, -i + ll[c][lc] + 1);
		}
		int rc = lower_bound(rr[c].begin(), rr[c].end(), i) - rr[c].begin();
		if (rc)
		{
			rv = min(rv, i - rr[c][rc - 1] + 1);
		}
		if (rc != rr[c].size())
		{
			rv = min(rv, -i + rr[c][rc] + 1);
		}
		cst[c][i][0] = { lv,rv };
		int j;
		for (j = 0; j < 19; j++)
		{
			cst[c][i][j + 1] = { min(cst[c][i][j].first + cst[glca[c][j].first][glca[c][j].second][j].first,cst[c][i][j].second + cst[glca[c][j].first][glca[c][j].second+1][j].first), min(cst[c][i][j].first + cst[glca[c][j].first][glca[c][j].second][j].second,cst[c][i][j].second + cst[glca[c][j].first][glca[c][j].second + 1][j].second) };
		}
	}
	int oc = c;
	for (i = 1; i < r.size(); i++)
	{
		bu({ oc,i - 1 }, r[i - 1] + 1, r[i] - 1,d+1);
	}
}
int lca(int a, int b)
{
	if (depth[a] > depth[b])
		swap(a, b);
	int dd = depth[b] - depth[a];
	int i;
	for (i = 19; i >= 0; i--)
	{
		if (dd & (1 << i))
		{
			b = glca[b][i].first;
		}
	}
	if (a == b)
		return a;
	for (i = 19; i >= 0; i--)
	{
		if (glca[a][i].first != glca[b][i].first)
		{
			a = glca[a][i].first;
			b = glca[b][i].first;
		}
	}
	return glca[a][0].first;
}
pair<pair<int, int>, pair<int, int>>infs(pair<int, int>r, int dd)
{
	pair<int, int>ct = { 0,0 };
	int c = 0;
	int i;
	for (i = 19; i >= 0; i--)
	{
		if (dd & (1 << i))
		{
			pair<int, int>ncst;
			if (c)
			{
				ncst.first = min(ct.first + cst[r.first][r.second][i].first, ct.second + cst[r.first][r.second + 1][i].first);
				ncst.second = min(ct.first + cst[r.first][r.second][i].second, ct.second + cst[r.first][r.second + 1][i].second);
			}
			else
			{
				ncst.first = ct.first + cst[r.first][r.second][i].first;
				ncst.second = ct.first + cst[r.first][r.second][i].second;
			}
			ct = ncst;
			r = glca[r.first][i];
			c = 1;
		}
	}
	return{ r,ct };
}
int main()
{
	int  K, Q;
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> K >> Q;
	for (treeN = 1; treeN <= N; treeN *= 2);
	int i;
	for (i = 0; i < N; i++)
	{
		cin >> arr[i];
		lp.upd(i, { arr[i],-i });
		rp.upd(i, { arr[i],i });
	}
	vector<pair<int, int>>r;
	int j;
	for (j = 0; j < 20; j++)
	{
		r.push_back({ 0,0 });
	}
	cst[0].push_back(r);
	cst[0].push_back(r);
	bu({ 0,0 });
	while (Q--)
	{
		int a, b;
		cin >> a >> b;
		a--;
		b--;
		pair<int, int>xp = pos[a];
		pair<int, int>yp = pos[b];
		int lc = lca(xp.first, yp.first);
		auto ld = infs(xp, depth[xp.first] - depth[lc]);
		auto rd = infs(yp, depth[yp.first] - depth[lc]);
		int ans = N;
		ans = min(ans, ld.second.first + rd.second.first + abs(ld.first.second - rd.first.second));
		if(depth[yp.first] - depth[lc])
			ans = min(ans, ld.second.first + rd.second.second+ abs(ld.first.second - rd.first.second-1));
		if(depth[xp.first] - depth[lc])
			ans = min(ans, ld.second.second + rd.second.first + abs(ld.first.second - rd.first.second+1));
		if (depth[yp.first] - depth[lc]&& depth[xp.first] - depth[lc])
			ans = min(ans, ld.second.second + rd.second.second + abs(ld.first.second - rd.first.second));
		if (lc != 1)
		{
			ld = infs(xp, depth[xp.first] - depth[lc]+1);
			rd = infs(yp, depth[yp.first] - depth[lc]+1);
			ans = min(ans, ld.second.first + rd.second.first + abs(ld.first.second - rd.first.second));
			ans = min(ans, ld.second.first + rd.second.second + abs(ld.first.second - rd.first.second - 1));
			ans = min(ans, ld.second.second + rd.second.first + abs(ld.first.second - rd.first.second + 1));
			ans = min(ans, ld.second.second + rd.second.second + abs(ld.first.second - rd.first.second));
		}
		cout << ans - 1 << '\n';
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9780 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 5 ms 9684 KB Output is correct
7 Correct 5 ms 9792 KB Output is correct
8 Correct 8 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 10068 KB Output is correct
2 Correct 146 ms 42724 KB Output is correct
3 Correct 130 ms 43004 KB Output is correct
4 Correct 136 ms 43112 KB Output is correct
5 Correct 126 ms 43236 KB Output is correct
6 Correct 121 ms 43336 KB Output is correct
7 Correct 117 ms 43300 KB Output is correct
8 Correct 92 ms 34544 KB Output is correct
9 Correct 131 ms 54072 KB Output is correct
10 Correct 108 ms 46132 KB Output is correct
11 Correct 134 ms 47408 KB Output is correct
12 Correct 124 ms 47612 KB Output is correct
13 Correct 133 ms 48196 KB Output is correct
14 Correct 124 ms 47436 KB Output is correct
15 Correct 126 ms 47880 KB Output is correct
16 Correct 137 ms 47312 KB Output is correct
17 Correct 122 ms 43272 KB Output is correct
18 Correct 125 ms 43340 KB Output is correct
19 Correct 97 ms 37836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 232 ms 44096 KB Output is correct
2 Correct 215 ms 44584 KB Output is correct
3 Correct 241 ms 44808 KB Output is correct
4 Correct 235 ms 44824 KB Output is correct
5 Correct 227 ms 44840 KB Output is correct
6 Correct 238 ms 44832 KB Output is correct
7 Correct 226 ms 44892 KB Output is correct
8 Correct 168 ms 42844 KB Output is correct
9 Correct 159 ms 36396 KB Output is correct
10 Correct 155 ms 36472 KB Output is correct
11 Correct 186 ms 36372 KB Output is correct
12 Correct 155 ms 36340 KB Output is correct
13 Correct 153 ms 36320 KB Output is correct
14 Correct 172 ms 36152 KB Output is correct
15 Correct 149 ms 36556 KB Output is correct
16 Correct 227 ms 39212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 246 ms 44060 KB Output is correct
2 Correct 239 ms 45324 KB Output is correct
3 Correct 256 ms 45260 KB Output is correct
4 Correct 225 ms 45256 KB Output is correct
5 Correct 146 ms 36392 KB Output is correct
6 Correct 263 ms 56344 KB Output is correct
7 Correct 258 ms 56352 KB Output is correct
8 Correct 234 ms 48324 KB Output is correct
9 Correct 283 ms 50048 KB Output is correct
10 Correct 268 ms 49684 KB Output is correct
11 Correct 238 ms 49364 KB Output is correct
12 Correct 280 ms 49500 KB Output is correct
13 Correct 273 ms 49424 KB Output is correct
14 Correct 308 ms 68940 KB Output is correct
15 Correct 363 ms 75868 KB Output is correct
16 Correct 357 ms 84908 KB Output is correct
17 Correct 357 ms 55428 KB Output is correct
18 Correct 303 ms 54580 KB Output is correct
19 Correct 316 ms 56680 KB Output is correct
20 Correct 286 ms 43816 KB Output is correct
21 Correct 288 ms 45364 KB Output is correct
22 Correct 232 ms 45376 KB Output is correct
23 Correct 216 ms 39852 KB Output is correct