Submission #483498

# Submission time Handle Problem Language Result Execution time Memory
483498 2021-10-30T04:31:06 Z blue Sky Walking (IOI19_walk) C++17
100 / 100
3327 ms 393712 KB
#include "walk.h"
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;

int N;
vector<int> X;
vector<int> H;

int M;
vector<int> L;
vector<int> R;
vector<int> Y;

int S;
int G;


int ht(int i)
{
	if(i > 0) return H[i-1];
	else return Y[-i-1];
}

bool cmp(int i, int j)
{
	if(ht(i) == ht(j)) return i > j;
	else return ht(i) > ht(j);
}


vector<int> newL, newR, newY;




const int mx = 4'000'000;
const long long INF = 1'000'000'000'000'000'000LL;


vector<int> edge[mx];
vector<long long> wt[mx];

void add_edge(int u, int v, long long w)
{
	// cerr << "add edge " << u << ' ' << v << ' ' << w << '\n';
	edge[u].push_back(v);
	wt[u].push_back(w);

	edge[v].push_back(u);
	wt[v].push_back(w);
}

vector<long long> distS(mx, INF);

struct distcomp
{
	int i;
};

bool operator < (distcomp A, distcomp B)
{
	if(distS[A.i] == distS[B.i]) return A.i < B.i;
	return distS[A.i] < distS[B.i];
}


set<pair<int, int> > points;  //stored by building index + y coordinate







struct loc
{
	int b;
	int y;

	int ind;
};

bool operator < (loc A, loc B)
{
	if(A.y == B.y) return A.b < B.b;
	else return A.y < B.y;
}



long long min_distance(vector<int> x, vector<int> h, vector<int> l, vector<int> r, vector<int> y, int s, int g)
{
	N = int(x.size());
	X = x;
	H = h;

	M = int(l.size());
	L = l;
	R = r;
	Y = y;

	S = s;
	G = g;



	vector<int> obj;
	for(int i = 0; i < N; i++) obj.push_back(i+1);
	for(int j = 0; j < M; j++) obj.push_back(-(j+1));
	sort(obj.begin(), obj.end(), cmp);

	vector<int> walk_buildings[M];

	set<int> buildings;
	for(int o: obj)
	{
		if(o > 0)
		{
			buildings.insert(o-1);
		}
		else
		{
			int w = -o-1;
			set<int> endpoints;
			endpoints.insert(L[w]);
			endpoints.insert(R[w]);
			points.insert(make_pair(L[w], Y[w]));
			// cerr << "inserting point A " << L[w] << ' ' << Y[w] << '\n';
			points.insert(make_pair(R[w], Y[w]));
			// cerr << "inserting point B " << R[w] << ' ' << Y[w] << '\n';
			walk_buildings[w].push_back(L[w]);
			walk_buildings[w].push_back(R[w]);

			for(int z: {s, g})
			{
				auto f = buildings.lower_bound(z);
				if(f != buildings.end())
					if(L[w] < *f && *f < R[w])
					{
						points.insert(make_pair(*f, Y[w]));
						// cerr << "inserting point C " << *f << ' ' << Y[w] << '\n';
						walk_buildings[w].push_back(*f);
					}
						// endpoints.insert(*f);

				if(f != buildings.begin())
				{
					f--;
					if(L[w] < *f && *f < R[w])
					{
						points.insert(make_pair(*f, Y[w]));
						// cerr << "inserting point D " << *f << ' ' << Y[w] << '\n';
						walk_buildings[w].push_back(*f);
					}
				}
			}
		}
	}

	vector<int> ep[N];

	for(int j = 0; j < M; j++)
	{
		walk_buildings[j].erase(unique(walk_buildings[j].begin(), walk_buildings[j].end()), walk_buildings[j].end());
		for(int i: walk_buildings[j])
			ep[i].push_back(j);
	}

	points.insert(make_pair(S, 0));
	points.insert(make_pair(G, 0));


	vector<int> left_ep[N], right_ep[N];
	for(int j = 0; j < M; j++)
	{
		left_ep[L[j]].push_back(j);
		right_ep[R[j]].push_back(j);
	}

	set< pair<int, int> > curr_walks;
	for(int i = 0; i < N; i++)
	{
		for(int w: left_ep[i])
		{
			curr_walks.insert(make_pair(Y[w], w));
		}

		for(int w: ep[i])
		{
			auto f = curr_walks.find(make_pair(Y[w], w));
			assert(f != curr_walks.end());
			auto f1 = f;

			if(f != curr_walks.begin())
			{
				f--;
				if(L[f->second] <= i && i <= R[f->second] && Y[f->second] <= H[i])
					points.insert(make_pair(i, f->first));
				// cerr << "inserting point " << *f << ' ' << Y[w] << '\n';
			}

			// if(f1 != curr_walks.end())
			// {
			// 	f1++;
			// 	if(f1 != curr_walks.end())
			// 		if(L[f1->second] <= i && i <= R[f1->second] && Y[f1->second] <= H[i])
			// 			points.insert(make_pair(i, f1->first));
			// }
		}

		for(int w: right_ep[i])
		{
			curr_walks.erase(make_pair(Y[w], w));
		}
	}

		// cerr << "all points: ";
		// for(auto p:points) cerr << p.first << ' ' << p.second << '\n';

	vector<loc> places;

	int ct = -1;
	for(auto p: points)
	{
		ct++;
		places.push_back(loc{p.first, p.second, ct});
	}

	sort(places.begin(), places.end(), [] (loc k, loc l)
	{
		if(k.b == l.b)
			return k.y < l.y;
		else
			return k.b < l.b;
	});

	// cerr << "vertical edges: \n";

	for(int i = 0; i+1 < int(places.size()); i++)
	{
		if(places[i].b == places[i+1].b)
			add_edge(places[i].ind, places[i+1].ind, places[i+1].y - places[i].y);
	}

	sort(places.begin(), places.end(), [] (loc k, loc l)
	{
		if(k.y == l.y)
			return k.b < l.b;
		else
			return k.y < l.y;
	});

	// cerr << "horiz edges: \n";

	// cerr << "\n\n\n\nsorted places: \n";
	// for(auto q: places) cerr << q.b << ' ' << q.y << ' ' << q.ind << '\n';
	// cerr << "\n\n\n";

	for(int j = 0; j < M; j++)
	{
		// cerr << "j = " << j << '\n';
		auto f = lower_bound(places.begin(), places.end(), loc{L[j], Y[j], -1});
		auto g = f;
		g++;

		int ite = 0;

		while(1)
		{
			// cerr << "ite = " << ite << '\n';
			ite++;
			// cerr << int(g == places.end()) << '\n';
			if(g == places.end()) break;
			if(g->b > R[j]) break;
			if(g->y != Y[j]) break;
			// cerr << "gb = " << g->b << ", fb = " << f->b << '\n';
			// cerr << "g = " << g->b << ' ' << g->y << ", " << "f = " << f->b << ' ' << f->y << '\n';
			add_edge(f->ind, g->ind, X[g->b] - X[f->b]);
			f++;
			g++;
			// cerr << "done\n";
		}
	}
	// cerr << "j done\n";

	int S_loc, G_loc;

	for(int i = 0; i <= ct; i++)
	{
		if(places[i].b == S && places[i].y == 0)
			S_loc = places[i].ind;
		else if(places[i].b == G && places[i].y == 0)
			G_loc = places[i].ind;
	}


	// for(int i = 0; i <= ct; i++) cerr << "position " << places[i].b << ' ' << places[i].y << " = index " << places[i].ind << '\n';


	distS[S_loc] = 0;

	set<distcomp> tbv;
	for(int i = 0; i <= ct; i++)
		tbv.insert(distcomp{i});

	// cerr << "hello\n";


	while(!tbv.empty())
	{
		int u = tbv.begin()->i;
		tbv.erase(tbv.begin());

		// cerr << "u = " << u << ", distS = " << distS[u] << '\n';

		for(int e = 0; e < int(edge[u].size()); e++)
		{
			int v = edge[u][e];
			long long w = wt[u][e];

			if(distS[v] <= distS[u] + w) continue;
			tbv.erase(distcomp{v});
			distS[v] = distS[u] + w;
			tbv.insert(distcomp{v});
		}
	}

	if(distS[G_loc] >= INF)
		distS[G_loc] = -1;

	return distS[G_loc];
}

Compilation message

walk.cpp: In function 'long long int min_distance(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>, int, int)':
walk.cpp:196:9: warning: variable 'f1' set but not used [-Wunused-but-set-variable]
  196 |    auto f1 = f;
      |         ^~
walk.cpp:304:13: warning: 'S_loc' may be used uninitialized in this function [-Wmaybe-uninitialized]
  304 |  distS[S_loc] = 0;
      |             ^
walk.cpp:332:16: warning: 'G_loc' may be used uninitialized in this function [-Wmaybe-uninitialized]
  332 |  if(distS[G_loc] >= INF)
      |                ^
# Verdict Execution time Memory Grader output
1 Correct 110 ms 219460 KB Output is correct
2 Correct 110 ms 219468 KB Output is correct
3 Correct 111 ms 219460 KB Output is correct
4 Correct 110 ms 219408 KB Output is correct
5 Correct 111 ms 219432 KB Output is correct
6 Correct 109 ms 219512 KB Output is correct
7 Correct 110 ms 219516 KB Output is correct
8 Correct 111 ms 219460 KB Output is correct
9 Correct 110 ms 219460 KB Output is correct
10 Correct 113 ms 219460 KB Output is correct
11 Correct 111 ms 219444 KB Output is correct
12 Correct 111 ms 219384 KB Output is correct
13 Correct 110 ms 219396 KB Output is correct
14 Correct 111 ms 219460 KB Output is correct
15 Correct 110 ms 219436 KB Output is correct
16 Correct 115 ms 219460 KB Output is correct
17 Correct 111 ms 219488 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 112 ms 219620 KB Output is correct
2 Correct 113 ms 219420 KB Output is correct
3 Correct 1254 ms 303444 KB Output is correct
4 Correct 1144 ms 317020 KB Output is correct
5 Correct 687 ms 295308 KB Output is correct
6 Correct 692 ms 291520 KB Output is correct
7 Correct 709 ms 295496 KB Output is correct
8 Correct 1293 ms 308252 KB Output is correct
9 Correct 945 ms 308596 KB Output is correct
10 Correct 1174 ms 317316 KB Output is correct
11 Correct 913 ms 294444 KB Output is correct
12 Correct 739 ms 287408 KB Output is correct
13 Correct 1155 ms 321880 KB Output is correct
14 Correct 621 ms 281532 KB Output is correct
15 Correct 720 ms 285864 KB Output is correct
16 Correct 681 ms 289000 KB Output is correct
17 Correct 686 ms 286752 KB Output is correct
18 Correct 961 ms 289160 KB Output is correct
19 Correct 131 ms 222592 KB Output is correct
20 Correct 304 ms 250968 KB Output is correct
21 Correct 613 ms 283824 KB Output is correct
22 Correct 668 ms 287408 KB Output is correct
23 Correct 863 ms 297696 KB Output is correct
24 Correct 641 ms 287428 KB Output is correct
25 Correct 638 ms 285884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 238760 KB Output is correct
2 Correct 1457 ms 316072 KB Output is correct
3 Correct 1526 ms 318840 KB Output is correct
4 Correct 1803 ms 330204 KB Output is correct
5 Correct 2122 ms 329052 KB Output is correct
6 Correct 1935 ms 330152 KB Output is correct
7 Correct 744 ms 283924 KB Output is correct
8 Correct 701 ms 287220 KB Output is correct
9 Correct 2048 ms 330536 KB Output is correct
10 Correct 789 ms 293648 KB Output is correct
11 Correct 143 ms 227512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 272 ms 238760 KB Output is correct
2 Correct 1457 ms 316072 KB Output is correct
3 Correct 1526 ms 318840 KB Output is correct
4 Correct 1803 ms 330204 KB Output is correct
5 Correct 2122 ms 329052 KB Output is correct
6 Correct 1935 ms 330152 KB Output is correct
7 Correct 744 ms 283924 KB Output is correct
8 Correct 701 ms 287220 KB Output is correct
9 Correct 2048 ms 330536 KB Output is correct
10 Correct 789 ms 293648 KB Output is correct
11 Correct 143 ms 227512 KB Output is correct
12 Correct 1660 ms 318524 KB Output is correct
13 Correct 1218 ms 329892 KB Output is correct
14 Correct 1978 ms 328868 KB Output is correct
15 Correct 1370 ms 312860 KB Output is correct
16 Correct 1226 ms 316132 KB Output is correct
17 Correct 1387 ms 328436 KB Output is correct
18 Correct 1344 ms 312832 KB Output is correct
19 Correct 1231 ms 315964 KB Output is correct
20 Correct 823 ms 282860 KB Output is correct
21 Correct 184 ms 235532 KB Output is correct
22 Correct 893 ms 310436 KB Output is correct
23 Correct 838 ms 307876 KB Output is correct
24 Correct 700 ms 290224 KB Output is correct
25 Correct 788 ms 301492 KB Output is correct
26 Correct 618 ms 281236 KB Output is correct
27 Correct 1983 ms 329440 KB Output is correct
28 Correct 1151 ms 328996 KB Output is correct
29 Correct 1900 ms 329740 KB Output is correct
30 Correct 775 ms 283480 KB Output is correct
31 Correct 1963 ms 330312 KB Output is correct
32 Correct 669 ms 285488 KB Output is correct
33 Correct 679 ms 287196 KB Output is correct
34 Correct 798 ms 292476 KB Output is correct
35 Correct 851 ms 296540 KB Output is correct
36 Correct 706 ms 289124 KB Output is correct
37 Correct 616 ms 283172 KB Output is correct
38 Correct 671 ms 286844 KB Output is correct
39 Correct 868 ms 297300 KB Output is correct
40 Correct 652 ms 286924 KB Output is correct
41 Correct 654 ms 285296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 219460 KB Output is correct
2 Correct 110 ms 219468 KB Output is correct
3 Correct 111 ms 219460 KB Output is correct
4 Correct 110 ms 219408 KB Output is correct
5 Correct 111 ms 219432 KB Output is correct
6 Correct 109 ms 219512 KB Output is correct
7 Correct 110 ms 219516 KB Output is correct
8 Correct 111 ms 219460 KB Output is correct
9 Correct 110 ms 219460 KB Output is correct
10 Correct 113 ms 219460 KB Output is correct
11 Correct 111 ms 219444 KB Output is correct
12 Correct 111 ms 219384 KB Output is correct
13 Correct 110 ms 219396 KB Output is correct
14 Correct 111 ms 219460 KB Output is correct
15 Correct 110 ms 219436 KB Output is correct
16 Correct 115 ms 219460 KB Output is correct
17 Correct 111 ms 219488 KB Output is correct
18 Correct 112 ms 219620 KB Output is correct
19 Correct 113 ms 219420 KB Output is correct
20 Correct 1254 ms 303444 KB Output is correct
21 Correct 1144 ms 317020 KB Output is correct
22 Correct 687 ms 295308 KB Output is correct
23 Correct 692 ms 291520 KB Output is correct
24 Correct 709 ms 295496 KB Output is correct
25 Correct 1293 ms 308252 KB Output is correct
26 Correct 945 ms 308596 KB Output is correct
27 Correct 1174 ms 317316 KB Output is correct
28 Correct 913 ms 294444 KB Output is correct
29 Correct 739 ms 287408 KB Output is correct
30 Correct 1155 ms 321880 KB Output is correct
31 Correct 621 ms 281532 KB Output is correct
32 Correct 720 ms 285864 KB Output is correct
33 Correct 681 ms 289000 KB Output is correct
34 Correct 686 ms 286752 KB Output is correct
35 Correct 961 ms 289160 KB Output is correct
36 Correct 131 ms 222592 KB Output is correct
37 Correct 304 ms 250968 KB Output is correct
38 Correct 613 ms 283824 KB Output is correct
39 Correct 668 ms 287408 KB Output is correct
40 Correct 863 ms 297696 KB Output is correct
41 Correct 641 ms 287428 KB Output is correct
42 Correct 638 ms 285884 KB Output is correct
43 Correct 272 ms 238760 KB Output is correct
44 Correct 1457 ms 316072 KB Output is correct
45 Correct 1526 ms 318840 KB Output is correct
46 Correct 1803 ms 330204 KB Output is correct
47 Correct 2122 ms 329052 KB Output is correct
48 Correct 1935 ms 330152 KB Output is correct
49 Correct 744 ms 283924 KB Output is correct
50 Correct 701 ms 287220 KB Output is correct
51 Correct 2048 ms 330536 KB Output is correct
52 Correct 789 ms 293648 KB Output is correct
53 Correct 143 ms 227512 KB Output is correct
54 Correct 1660 ms 318524 KB Output is correct
55 Correct 1218 ms 329892 KB Output is correct
56 Correct 1978 ms 328868 KB Output is correct
57 Correct 1370 ms 312860 KB Output is correct
58 Correct 1226 ms 316132 KB Output is correct
59 Correct 1387 ms 328436 KB Output is correct
60 Correct 1344 ms 312832 KB Output is correct
61 Correct 1231 ms 315964 KB Output is correct
62 Correct 823 ms 282860 KB Output is correct
63 Correct 184 ms 235532 KB Output is correct
64 Correct 893 ms 310436 KB Output is correct
65 Correct 838 ms 307876 KB Output is correct
66 Correct 700 ms 290224 KB Output is correct
67 Correct 788 ms 301492 KB Output is correct
68 Correct 618 ms 281236 KB Output is correct
69 Correct 1983 ms 329440 KB Output is correct
70 Correct 1151 ms 328996 KB Output is correct
71 Correct 1900 ms 329740 KB Output is correct
72 Correct 775 ms 283480 KB Output is correct
73 Correct 1963 ms 330312 KB Output is correct
74 Correct 669 ms 285488 KB Output is correct
75 Correct 679 ms 287196 KB Output is correct
76 Correct 798 ms 292476 KB Output is correct
77 Correct 851 ms 296540 KB Output is correct
78 Correct 706 ms 289124 KB Output is correct
79 Correct 616 ms 283172 KB Output is correct
80 Correct 671 ms 286844 KB Output is correct
81 Correct 868 ms 297300 KB Output is correct
82 Correct 652 ms 286924 KB Output is correct
83 Correct 654 ms 285296 KB Output is correct
84 Correct 239 ms 235408 KB Output is correct
85 Correct 1772 ms 322760 KB Output is correct
86 Correct 2559 ms 361796 KB Output is correct
87 Correct 211 ms 241496 KB Output is correct
88 Correct 226 ms 242060 KB Output is correct
89 Correct 211 ms 241596 KB Output is correct
90 Correct 146 ms 226108 KB Output is correct
91 Correct 115 ms 219716 KB Output is correct
92 Correct 143 ms 224524 KB Output is correct
93 Correct 608 ms 267956 KB Output is correct
94 Correct 183 ms 235552 KB Output is correct
95 Correct 979 ms 314284 KB Output is correct
96 Correct 843 ms 308268 KB Output is correct
97 Correct 707 ms 289936 KB Output is correct
98 Correct 786 ms 301104 KB Output is correct
99 Correct 3327 ms 393712 KB Output is correct
100 Correct 1756 ms 333644 KB Output is correct
101 Correct 2339 ms 355172 KB Output is correct
102 Correct 805 ms 286328 KB Output is correct
103 Correct 683 ms 290768 KB Output is correct
104 Correct 671 ms 290048 KB Output is correct
105 Correct 793 ms 295252 KB Output is correct
106 Correct 735 ms 292152 KB Output is correct
107 Correct 777 ms 293868 KB Output is correct
108 Correct 182 ms 228300 KB Output is correct
109 Correct 1405 ms 310060 KB Output is correct
110 Correct 1181 ms 332552 KB Output is correct
111 Correct 1111 ms 333952 KB Output is correct
112 Correct 822 ms 297944 KB Output is correct
113 Correct 763 ms 295216 KB Output is correct