답안 #415473

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415473 2021-06-01T06:48:25 Z 장태환(#7545) Road Construction (JOI21_road_construction) C++17
100 / 100
4854 ms 822000 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <functional>
#define int long long
using namespace std;
struct node
{
	pair<int,int> val;
	int l, r;
	node()
	{
		val = { 1LL<<50,0 };
		l = r = -1;
	}
};
int treeN;
struct pst
{
	vector<node>ps;
	int root[1000100];
	int rootc = 0;
	void init()
	{
		ps.clear();
		node be;
		ps.push_back(be);
		root[0] = 0;
		rootc = 1;
	}
	void upd(int po, int ind, int s, int e,pair<int,int>v)
	{
		if (s == e)
		{
			ps[ind].val = v;
			return;
		}
		int m = (s + e) >> 1;
		if (po <= m)
		{
			node x;
			if (ps[ind].l >= 0)
				x = ps[ps[ind].l];
			ps.push_back(x);
			ps[ind].l = ps.size() - 1;
			upd(po, ps.size() - 1, s, m, v);
		}
		else
		{
			node x;
			if(ps[ind].r>=0)
				x=ps[ps[ind].r];
			ps.push_back(x);
			ps[ind].r = ps.size() - 1;
			upd(po, ps.size() - 1, m+1, e, v);
		}
		ps[ind].val = { 1LL<<50,0 };
		if (ps[ind].l>=0)
		{
			ps[ind].val = min(ps[ind].val, ps[ps[ind].l].val);
		}
		if (ps[ind].r >= 0)
		{
			ps[ind].val = min(ps[ind].val, ps[ps[ind].r].val);
		}
	}
	void update(int n,pair<int,int> k,int bef)
	{
		node be;
		be = ps[root[bef]];
		ps.push_back(be);
		root[rootc++] = ps.size()-1;
		upd(n, root[rootc - 1], 0, treeN - 1, k);
	}
	pair<int,int> qur(int qs,int qe, int ind, int s, int e)
	{
		if (qs > e || s > qe||ind==-1)
		{
			return { 1LL<<50,0 };
		}
		if (qs <= s && e <= qe)
			return ps[ind].val;
		int m = (s + e) >> 1;
		return min(qur(qs,qe,ps[ind].l, s, m),qur(qs, qe, ps[ind].r, m+1,e));
	}
	pair<int, int> query(int ind, int s, int e)
	{
		return qur(s, e, root[ind], 0, treeN - 1);
	}
}u,d;
int ru[300100];
int rd[300100];
vector<pair<int, int>>x;
vector<pair<int, int>>y;
signed main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int N, K;
	cin >> N >> K;
	int i;
	
	for (i = 0; i < N; i++)
	{
		ru[i] = rd[i] = i;
		int a, b;
		cin >> a >> b;
		x.push_back({ a,b });
	}
	sort(x.begin(), x.end());
	for (i = 0; i < N; i++)
	{
		y.push_back({ x[i].second,i });
	}
	sort(y.begin(), y.end());
	y.erase(unique(y.begin(), y.end()), y.end());
	for (treeN = 1; treeN < y.size(); treeN *=2);
	u.init();
	d.init();
	for (i = 0; i < N; i++)
	{
		u.update(lower_bound(y.begin(), y.end(), make_pair( x[i].second,i )) - y.begin(), { -x[i].first + x[i].second,i }, i);
		d.update(lower_bound(y.begin(), y.end(), make_pair(x[i].second, i)) - y.begin(), { -x[i].first - x[i].second,i }, i);
	}
	priority_queue<pair<int, int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;
	for (i = 0; i < N; i++)
	{
		auto f = u.query(ru[i] , lower_bound(y.begin(), y.end(), make_pair(x[i].second, i)) - y.begin(), treeN - 1);
		auto s = d.query(rd[i], 0, lower_bound(y.begin(), y.end(), make_pair(x[i].second, i)) - y.begin());
		f.first += x[i].first - x[i].second;
		s.first += x[i].first + x[i].second;
		if (f.first < s.first)
		{
			pq.push({ f.first,i });
			u.update(lower_bound(y.begin(), y.end(), make_pair(x[f.second].second, f.second)) - y.begin(), { 1LL << 50,f.second }, ru[i]);
			ru[i] = u.rootc - 1;
		}
		else
		{
			swap(f, s);
			pq.push({ f.first,i });
			d.update(lower_bound(y.begin(), y.end(), make_pair(x[f.second].second, f.second)) - y.begin(), { 1LL << 50,f.second }, rd[i]);
			rd[i] = d.rootc - 1;
		}
	}
	for (i = 0; i < K; i++)
	{
		auto a = pq.top();
		pq.pop();
		cout << a.first << '\n';
		auto f = u.query(ru[a.second], lower_bound(y.begin(), y.end(), make_pair(x[a.second].second, a.second)) - y.begin(), treeN - 1);
		auto s = d.query(rd[a.second], 0,lower_bound(y.begin(), y.end(), make_pair(x[a.second].second, a.second)) - y.begin());
		f.first += x[a.second].first - x[a.second].second;
		s.first += x[a.second].first + x[a.second].second;
		if (f.first < s.first)
		{
			pq.push({ f.first,a.second });
			u.update(lower_bound(y.begin(), y.end(), make_pair(x[f.second].second, f.second)) - y.begin(), { 1LL << 50,f.second }, ru[a.second]);
			ru[a.second] = u.rootc - 1;
		}
		else
		{
			swap(f, s);
			pq.push({ f.first,a.second });
			d.update(lower_bound(y.begin(), y.end(), make_pair(x[f.second].second, f.second)) - y.begin(), { 1LL << 50,f.second }, rd[a.second]);
			rd[a.second] = d.rootc - 1;
		}
	}
}

Compilation message

road_construction.cpp: In function 'int main()':
road_construction.cpp:118:24: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  118 |  for (treeN = 1; treeN < y.size(); treeN *=2);
      |                  ~~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 717 ms 102728 KB Output is correct
2 Correct 650 ms 102588 KB Output is correct
3 Correct 606 ms 103232 KB Output is correct
4 Correct 543 ms 127908 KB Output is correct
5 Correct 583 ms 102440 KB Output is correct
6 Correct 8 ms 3132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2423 ms 701684 KB Output is correct
2 Correct 2372 ms 701616 KB Output is correct
3 Correct 423 ms 135600 KB Output is correct
4 Correct 1834 ms 701820 KB Output is correct
5 Correct 1820 ms 701772 KB Output is correct
6 Correct 1843 ms 701648 KB Output is correct
7 Correct 1788 ms 701936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2388 ms 477240 KB Output is correct
2 Correct 2556 ms 477320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1316 ms 701604 KB Output is correct
5 Correct 1735 ms 704104 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2388 ms 477240 KB Output is correct
2 Correct 2556 ms 477320 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1316 ms 701604 KB Output is correct
5 Correct 1735 ms 704104 KB Output is correct
6 Correct 2636 ms 477376 KB Output is correct
7 Correct 2706 ms 477200 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2632 ms 477416 KB Output is correct
11 Correct 1318 ms 701536 KB Output is correct
12 Correct 1688 ms 704148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 717 ms 102728 KB Output is correct
2 Correct 650 ms 102588 KB Output is correct
3 Correct 606 ms 103232 KB Output is correct
4 Correct 543 ms 127908 KB Output is correct
5 Correct 583 ms 102440 KB Output is correct
6 Correct 8 ms 3132 KB Output is correct
7 Correct 2625 ms 407832 KB Output is correct
8 Correct 2821 ms 408008 KB Output is correct
9 Correct 535 ms 143440 KB Output is correct
10 Correct 1773 ms 407612 KB Output is correct
11 Correct 2098 ms 407924 KB Output is correct
12 Correct 1272 ms 337276 KB Output is correct
13 Correct 1315 ms 335652 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 717 ms 102728 KB Output is correct
2 Correct 650 ms 102588 KB Output is correct
3 Correct 606 ms 103232 KB Output is correct
4 Correct 543 ms 127908 KB Output is correct
5 Correct 583 ms 102440 KB Output is correct
6 Correct 8 ms 3132 KB Output is correct
7 Correct 2423 ms 701684 KB Output is correct
8 Correct 2372 ms 701616 KB Output is correct
9 Correct 423 ms 135600 KB Output is correct
10 Correct 1834 ms 701820 KB Output is correct
11 Correct 1820 ms 701772 KB Output is correct
12 Correct 1843 ms 701648 KB Output is correct
13 Correct 1788 ms 701936 KB Output is correct
14 Correct 2388 ms 477240 KB Output is correct
15 Correct 2556 ms 477320 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1316 ms 701604 KB Output is correct
18 Correct 1735 ms 704104 KB Output is correct
19 Correct 2636 ms 477376 KB Output is correct
20 Correct 2706 ms 477200 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 2632 ms 477416 KB Output is correct
24 Correct 1318 ms 701536 KB Output is correct
25 Correct 1688 ms 704148 KB Output is correct
26 Correct 2625 ms 407832 KB Output is correct
27 Correct 2821 ms 408008 KB Output is correct
28 Correct 535 ms 143440 KB Output is correct
29 Correct 1773 ms 407612 KB Output is correct
30 Correct 2098 ms 407924 KB Output is correct
31 Correct 1272 ms 337276 KB Output is correct
32 Correct 1315 ms 335652 KB Output is correct
33 Correct 4854 ms 821336 KB Output is correct
34 Correct 4573 ms 822000 KB Output is correct
35 Correct 3617 ms 821528 KB Output is correct
36 Correct 2293 ms 704312 KB Output is correct
37 Correct 2424 ms 704080 KB Output is correct
38 Correct 2550 ms 703900 KB Output is correct