Submission #373068

# Submission time Handle Problem Language Result Execution time Memory
373068 2021-03-03T08:44:04 Z Kanon Sky Walking (IOI19_walk) C++14
100 / 100
2811 ms 221232 KB
#include <iostream>
#include <vector>
#include <memory.h>
#include <algorithm>
#include <set>
#include <map>

#define pb push_back
#define mp make_pair

using namespace std;

const int maxn = 1000000 + 10;
const long long inf = 1e18;
int n, m, X[maxn], Y[maxn], H[maxn], L[maxn], R[maxn], a[maxn], b[maxn], node, y_map[maxn];
vector<int> neiL[maxn], neiR[maxn];
long long dis[maxn];
vector<pair<int, long long>> adj[maxn];
map<pair<int, int>, int> nodes;
vector<int> all_y;

void devide(int idx)
{
	memset(a, -1, sizeof(a));
	memset(b, -1, sizeof(b));
	vector<pair<pair<int, int>, int>> sky;
	vector<pair<int, int>> st;
	st.pb(mp(H[idx], idx));
	for (int i = idx - 1; i >= 0; i--)
	{
		if (H[i] > st.back().first)
			st.push_back(mp(H[i], i));
		for (int j = 0; j < neiL[i].size(); j++)
		{
			int cur = neiL[i][j];
			if (R[cur] > idx)
			{
				int pos = upper_bound(st.begin(), st.end(), mp(Y[cur], -1)) - st.begin();
				a[cur] = st[pos].second;
			}
		}
	}
	st.clear();
	st.pb(mp(H[idx], idx));
	for (int i = idx + 1; i < n; i++)
	{
		if (H[i] > st.back().first)
			st.push_back(mp(H[i], i));
		for (int j = 0; j < neiR[i].size(); j++)
		{
			int cur = neiR[i][j];
			if (L[cur] < idx)
			{
				int pos = upper_bound(st.begin(), st.end(), mp(Y[cur], -1)) - st.begin();
				b[cur] = st[pos].second;
			}
		}
	}
	for (int i = 0; i < m; i++)
	{
		if (a[i] != -1)
		{
			if (a[i] != L[i])
				sky.pb(mp(mp(L[i], a[i]), Y[i]));
			if (a[i] != b[i])
				sky.pb(mp(mp(a[i], b[i]), Y[i]));
			if (b[i] != R[i])
				sky.pb(mp(mp(b[i], R[i]), Y[i]));
		}
		else
			sky.pb(mp(mp(L[i], R[i]), Y[i]));
	}
	for (int i = 0; i < n; i++)
	{
		neiL[i].clear();
		neiR[i].clear();
	}
	m = sky.size();
	for (int i = 0; i < m; i++)
	{
		L[i] = sky[i].first.first;
		R[i] = sky[i].first.second;
		Y[i] = sky[i].second;
		neiL[L[i]].pb(i);
		neiR[R[i]].pb(i);
	}
}

long long dijkstra(int source, int sink)
{
	set<pair<long long, int>> S;
	for (int i = 0; i <= node; i++)
		dis[i] = inf;
	dis[source] = 0;
	S.insert(mp(0, source));
	while (S.size())
	{
		set<pair<long long, int>>::iterator it = S.begin();
		long long dist = (*it).first;
		int v = (*it).second;
		S.erase(it);
		for (int i = 0; i < adj[v].size(); i++)
		{
			int u = adj[v][i].first;
			long long w = adj[v][i].second;
			if (dist + w < dis[u])
			{
				S.erase(mp(dis[u], u));
				dis[u] = dist + w;
				S.insert(mp(dis[u], u));
			}
		}
	}
	if (dis[sink] == inf)
		return -1;
	return dis[sink];
}

int add_map(int x, int y)
{
	if (!nodes[mp(x, y)])
		nodes[mp(x, y)] = ++node;
	return nodes[mp(x, y)];
}

void add_edge(int x1, int y1, int x2, int y2)
{
	int u = add_map(x1, y1);
	int v = add_map(x2, y2);
	if (x1 == x2)
	{
		adj[u].push_back(mp(v, abs(y2 - y1)));
		adj[v].push_back(mp(u, abs(y2 - y1)));
	}
	else
	{
		adj[u].push_back(mp(v, abs(x2 - x1)));
		adj[v].push_back(mp(u, abs(x2 - x1)));
	}
}

void build_graph()
{
	set<int> line;
	line.insert(0);
	set<int>::iterator it;
	map<int, int> st;
	vector<int> set_res;
	memset(y_map, -1, sizeof(y_map));
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < neiL[i].size(); j++)
		{
			int cur = neiL[i][j];
			line.insert(-Y[cur]);
		}
		for (int j = 0; j < neiL[i].size(); j++)
		{
			int cur = neiL[i][j];
			it = line.upper_bound(-Y[cur]);
			int y2 = Y[cur];
			int y1 = -(*it);
			int y2_id = lower_bound(all_y.begin(), all_y.end(), y2) - all_y.begin() + 1;
			int y1_id = lower_bound(all_y.begin(), all_y.end(), y1) - all_y.begin() + 1;
			set_res.pb(y1_id);
			set_res.pb(y2_id);
			add_edge(X[i], y1, X[i], y2);
			if (y_map[y1_id] != -1 && y1 != 0)
			{
				add_edge(X[i], y1, y_map[y1_id], y1);
			}
		}
		for (int j = 0; j < neiR[i].size(); j++)
		{
			int cur = neiR[i][j];
			it = line.upper_bound(-Y[cur]);
			int y2 = Y[cur];
			int y1 = -(*it);
			int y2_id = lower_bound(all_y.begin(), all_y.end(), y2) - all_y.begin() + 1;
			int y1_id = lower_bound(all_y.begin(), all_y.end(), y1) - all_y.begin() + 1;
			set_res.pb(y1_id);
			set_res.pb(y2_id);
			add_edge(X[i], y1, X[i], y2);
			add_edge(X[i], y2, y_map[y2_id], y2);
			if (y_map[y1_id] != -1 && y1 != 0)
			{
				add_edge(X[i], y1, y_map[y1_id], y1);
			}
		}

		for (int j = 0; j < set_res.size(); j++)
			y_map[set_res[j]] = X[i];
		set_res.clear();

		for (int j = 0; j < neiR[i].size(); j++)
		{
			int cur = neiR[i][j];
			int y2 = lower_bound(all_y.begin(), all_y.end(), Y[cur]) - all_y.begin() + 1;
			y_map[y2] = -1;
			line.erase(-Y[cur]);
		}
		for (int j = 0; j < neiL[i].size(); j++)
		{
			int cur = neiL[i][j];
			int y2 = lower_bound(all_y.begin(), all_y.end(), Y[cur]) - all_y.begin() + 1;
			y_map[y2] = X[i];
			line.insert(-Y[cur]);
		}
	}
}


bool cmp(pair<int, pair<int, int> > aa, pair<int, pair<int, int> > bb){
	if(aa.first < bb.first)
		return true;
	if(aa.first > bb.first)
		return false;
	return aa.second.first < bb.second.first;
	
}

void init(){
	vector<pair<int, pair<int, int> > > tmp, nw;
	for(int i = 0; i < m; i++)
		tmp.push_back(mp(Y[i], mp(L[i], R[i])));
	sort(tmp.begin(), tmp.end(), cmp);
	for(int i = 0; i < m; i++){
		if(tmp[i].second.first == tmp[i].second.second) continue;
		int j = i;
		int hei = tmp[i].first;
		int lo = tmp[i].second.first;
		int hi = tmp[i].second.second;
		while(tmp[j].first == hei && tmp[j].second.first <= hi && tmp[j].second.first >= lo){
			hi = max(hi, tmp[j].second.second);
			j++;
		}
		nw.push_back(mp(hei, mp(lo, hi)));
		i = j - 1;
	}
	m = nw.size();
	for (int i = 0; i < m; i++)
	{
		L[i] = nw[i].second.first;
		R[i] = nw[i].second.second;
		Y[i] = nw[i].first;
		neiL[L[i]].pb(i);
		neiR[R[i]].pb(i);
	}
	
}

long long min_distance(vector<int> XX, vector<int> HH, vector<int> LL, vector<int> RR, vector<int> YY, int S, int G)
{
	n = XX.size();
	m = LL.size();
	for (int i = 0; i < n; i++)
	{
		X[i] = XX[i];
		H[i] = HH[i];
	}
	for (int i = 0; i < m; i++)
	{
		L[i] = LL[i];
		R[i] = RR[i];
		Y[i] = YY[i];
		all_y.push_back(Y[i]);
	}
	init();
	all_y.push_back(0);
	sort(all_y.begin(), all_y.end());
	all_y.resize(unique(all_y.begin(), all_y.end()) - all_y.begin());
	devide(S);
	devide(G);
	build_graph();
	return dijkstra(add_map(X[S], 0), add_map(X[G], 0));
}

Compilation message

walk.cpp: In function 'void devide(int)':
walk.cpp:33:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |   for (int j = 0; j < neiL[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:49:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |   for (int j = 0; j < neiR[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp: In function 'long long int dijkstra(int, int)':
walk.cpp:102:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  102 |   for (int i = 0; i < adj[v].size(); i++)
      |                   ~~^~~~~~~~~~~~~~~
walk.cpp: In function 'void build_graph()':
walk.cpp:152:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |   for (int j = 0; j < neiL[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:157:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  157 |   for (int j = 0; j < neiL[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:173:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  173 |   for (int j = 0; j < neiR[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:191:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  191 |   for (int j = 0; j < set_res.size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:195:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  195 |   for (int j = 0; j < neiR[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
walk.cpp:202:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  202 |   for (int j = 0; j < neiL[i].size(); j++)
      |                   ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 55 ms 82540 KB Output is correct
2 Correct 56 ms 82540 KB Output is correct
3 Correct 55 ms 82540 KB Output is correct
4 Correct 56 ms 82540 KB Output is correct
5 Correct 57 ms 82540 KB Output is correct
6 Correct 55 ms 82540 KB Output is correct
7 Correct 54 ms 82628 KB Output is correct
8 Correct 54 ms 82540 KB Output is correct
9 Correct 58 ms 82540 KB Output is correct
10 Correct 61 ms 82540 KB Output is correct
11 Correct 60 ms 82540 KB Output is correct
12 Correct 52 ms 82684 KB Output is correct
13 Correct 53 ms 82548 KB Output is correct
14 Correct 52 ms 82668 KB Output is correct
15 Correct 53 ms 82540 KB Output is correct
16 Correct 53 ms 82612 KB Output is correct
17 Correct 53 ms 82540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 82540 KB Output is correct
2 Correct 51 ms 82540 KB Output is correct
3 Correct 747 ms 133804 KB Output is correct
4 Correct 745 ms 141604 KB Output is correct
5 Correct 492 ms 126928 KB Output is correct
6 Correct 497 ms 125800 KB Output is correct
7 Correct 496 ms 127312 KB Output is correct
8 Correct 770 ms 135916 KB Output is correct
9 Correct 650 ms 137208 KB Output is correct
10 Correct 749 ms 140280 KB Output is correct
11 Correct 650 ms 130536 KB Output is correct
12 Correct 554 ms 132728 KB Output is correct
13 Correct 742 ms 144368 KB Output is correct
14 Correct 499 ms 126448 KB Output is correct
15 Correct 585 ms 130272 KB Output is correct
16 Correct 474 ms 131488 KB Output is correct
17 Correct 431 ms 131264 KB Output is correct
18 Correct 628 ms 133160 KB Output is correct
19 Correct 71 ms 85100 KB Output is correct
20 Correct 274 ms 107916 KB Output is correct
21 Correct 402 ms 131568 KB Output is correct
22 Correct 475 ms 135212 KB Output is correct
23 Correct 785 ms 144608 KB Output is correct
24 Correct 446 ms 134136 KB Output is correct
25 Correct 403 ms 130484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 84772 KB Output is correct
2 Correct 1051 ms 140768 KB Output is correct
3 Correct 1123 ms 143984 KB Output is correct
4 Correct 1191 ms 150512 KB Output is correct
5 Correct 1552 ms 154608 KB Output is correct
6 Correct 1506 ms 152056 KB Output is correct
7 Correct 476 ms 119892 KB Output is correct
8 Correct 561 ms 136496 KB Output is correct
9 Correct 1439 ms 152628 KB Output is correct
10 Correct 559 ms 132304 KB Output is correct
11 Correct 67 ms 84588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 84772 KB Output is correct
2 Correct 1051 ms 140768 KB Output is correct
3 Correct 1123 ms 143984 KB Output is correct
4 Correct 1191 ms 150512 KB Output is correct
5 Correct 1552 ms 154608 KB Output is correct
6 Correct 1506 ms 152056 KB Output is correct
7 Correct 476 ms 119892 KB Output is correct
8 Correct 561 ms 136496 KB Output is correct
9 Correct 1439 ms 152628 KB Output is correct
10 Correct 559 ms 132304 KB Output is correct
11 Correct 67 ms 84588 KB Output is correct
12 Correct 1116 ms 143788 KB Output is correct
13 Correct 1025 ms 150128 KB Output is correct
14 Correct 1530 ms 154984 KB Output is correct
15 Correct 838 ms 146928 KB Output is correct
16 Correct 844 ms 142568 KB Output is correct
17 Correct 928 ms 150520 KB Output is correct
18 Correct 855 ms 146984 KB Output is correct
19 Correct 846 ms 142568 KB Output is correct
20 Correct 557 ms 119588 KB Output is correct
21 Correct 91 ms 87660 KB Output is correct
22 Correct 595 ms 137552 KB Output is correct
23 Correct 568 ms 136844 KB Output is correct
24 Correct 536 ms 130692 KB Output is correct
25 Correct 579 ms 134096 KB Output is correct
26 Correct 487 ms 130104 KB Output is correct
27 Correct 1583 ms 154736 KB Output is correct
28 Correct 770 ms 149752 KB Output is correct
29 Correct 1554 ms 151920 KB Output is correct
30 Correct 483 ms 120052 KB Output is correct
31 Correct 1444 ms 152048 KB Output is correct
32 Correct 496 ms 130896 KB Output is correct
33 Correct 529 ms 133192 KB Output is correct
34 Correct 626 ms 136912 KB Output is correct
35 Correct 559 ms 136448 KB Output is correct
36 Correct 463 ms 132940 KB Output is correct
37 Correct 423 ms 131696 KB Output is correct
38 Correct 475 ms 135024 KB Output is correct
39 Correct 776 ms 144456 KB Output is correct
40 Correct 440 ms 134304 KB Output is correct
41 Correct 405 ms 130596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 55 ms 82540 KB Output is correct
2 Correct 56 ms 82540 KB Output is correct
3 Correct 55 ms 82540 KB Output is correct
4 Correct 56 ms 82540 KB Output is correct
5 Correct 57 ms 82540 KB Output is correct
6 Correct 55 ms 82540 KB Output is correct
7 Correct 54 ms 82628 KB Output is correct
8 Correct 54 ms 82540 KB Output is correct
9 Correct 58 ms 82540 KB Output is correct
10 Correct 61 ms 82540 KB Output is correct
11 Correct 60 ms 82540 KB Output is correct
12 Correct 52 ms 82684 KB Output is correct
13 Correct 53 ms 82548 KB Output is correct
14 Correct 52 ms 82668 KB Output is correct
15 Correct 53 ms 82540 KB Output is correct
16 Correct 53 ms 82612 KB Output is correct
17 Correct 53 ms 82540 KB Output is correct
18 Correct 52 ms 82540 KB Output is correct
19 Correct 51 ms 82540 KB Output is correct
20 Correct 747 ms 133804 KB Output is correct
21 Correct 745 ms 141604 KB Output is correct
22 Correct 492 ms 126928 KB Output is correct
23 Correct 497 ms 125800 KB Output is correct
24 Correct 496 ms 127312 KB Output is correct
25 Correct 770 ms 135916 KB Output is correct
26 Correct 650 ms 137208 KB Output is correct
27 Correct 749 ms 140280 KB Output is correct
28 Correct 650 ms 130536 KB Output is correct
29 Correct 554 ms 132728 KB Output is correct
30 Correct 742 ms 144368 KB Output is correct
31 Correct 499 ms 126448 KB Output is correct
32 Correct 585 ms 130272 KB Output is correct
33 Correct 474 ms 131488 KB Output is correct
34 Correct 431 ms 131264 KB Output is correct
35 Correct 628 ms 133160 KB Output is correct
36 Correct 71 ms 85100 KB Output is correct
37 Correct 274 ms 107916 KB Output is correct
38 Correct 402 ms 131568 KB Output is correct
39 Correct 475 ms 135212 KB Output is correct
40 Correct 785 ms 144608 KB Output is correct
41 Correct 446 ms 134136 KB Output is correct
42 Correct 403 ms 130484 KB Output is correct
43 Correct 80 ms 84772 KB Output is correct
44 Correct 1051 ms 140768 KB Output is correct
45 Correct 1123 ms 143984 KB Output is correct
46 Correct 1191 ms 150512 KB Output is correct
47 Correct 1552 ms 154608 KB Output is correct
48 Correct 1506 ms 152056 KB Output is correct
49 Correct 476 ms 119892 KB Output is correct
50 Correct 561 ms 136496 KB Output is correct
51 Correct 1439 ms 152628 KB Output is correct
52 Correct 559 ms 132304 KB Output is correct
53 Correct 67 ms 84588 KB Output is correct
54 Correct 1116 ms 143788 KB Output is correct
55 Correct 1025 ms 150128 KB Output is correct
56 Correct 1530 ms 154984 KB Output is correct
57 Correct 838 ms 146928 KB Output is correct
58 Correct 844 ms 142568 KB Output is correct
59 Correct 928 ms 150520 KB Output is correct
60 Correct 855 ms 146984 KB Output is correct
61 Correct 846 ms 142568 KB Output is correct
62 Correct 557 ms 119588 KB Output is correct
63 Correct 91 ms 87660 KB Output is correct
64 Correct 595 ms 137552 KB Output is correct
65 Correct 568 ms 136844 KB Output is correct
66 Correct 536 ms 130692 KB Output is correct
67 Correct 579 ms 134096 KB Output is correct
68 Correct 487 ms 130104 KB Output is correct
69 Correct 1583 ms 154736 KB Output is correct
70 Correct 770 ms 149752 KB Output is correct
71 Correct 1554 ms 151920 KB Output is correct
72 Correct 483 ms 120052 KB Output is correct
73 Correct 1444 ms 152048 KB Output is correct
74 Correct 496 ms 130896 KB Output is correct
75 Correct 529 ms 133192 KB Output is correct
76 Correct 626 ms 136912 KB Output is correct
77 Correct 559 ms 136448 KB Output is correct
78 Correct 463 ms 132940 KB Output is correct
79 Correct 423 ms 131696 KB Output is correct
80 Correct 475 ms 135024 KB Output is correct
81 Correct 776 ms 144456 KB Output is correct
82 Correct 440 ms 134304 KB Output is correct
83 Correct 405 ms 130596 KB Output is correct
84 Correct 78 ms 85020 KB Output is correct
85 Correct 1209 ms 147936 KB Output is correct
86 Correct 2113 ms 181656 KB Output is correct
87 Correct 125 ms 92904 KB Output is correct
88 Correct 124 ms 92136 KB Output is correct
89 Correct 124 ms 92776 KB Output is correct
90 Correct 64 ms 83684 KB Output is correct
91 Correct 57 ms 82668 KB Output is correct
92 Correct 79 ms 85356 KB Output is correct
93 Correct 398 ms 109988 KB Output is correct
94 Correct 94 ms 87916 KB Output is correct
95 Correct 640 ms 140040 KB Output is correct
96 Correct 571 ms 137228 KB Output is correct
97 Correct 520 ms 131160 KB Output is correct
98 Correct 542 ms 134096 KB Output is correct
99 Correct 2811 ms 221232 KB Output is correct
100 Correct 1036 ms 150580 KB Output is correct
101 Correct 1936 ms 176504 KB Output is correct
102 Correct 516 ms 120172 KB Output is correct
103 Correct 497 ms 130888 KB Output is correct
104 Correct 516 ms 132808 KB Output is correct
105 Correct 634 ms 134736 KB Output is correct
106 Correct 551 ms 132200 KB Output is correct
107 Correct 649 ms 136552 KB Output is correct
108 Correct 113 ms 88044 KB Output is correct
109 Correct 1082 ms 138728 KB Output is correct
110 Correct 832 ms 149312 KB Output is correct
111 Correct 782 ms 149628 KB Output is correct
112 Correct 570 ms 134952 KB Output is correct
113 Correct 506 ms 133384 KB Output is correct