답안 #415741

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
415741 2021-06-01T12:41:23 Z 송준혁(#7513) Road Construction (JOI21_road_construction) C++17
100 / 100
2020 ms 355968 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define MOD 1000000007
#define INF (1ll<<60)
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,int> pli;

int N, K, num;
int T1[250505], T2[250505], Y[250505];
pii P[250505];
vector<pii> C;

struct Node{
	int l, r, p;
	LL v;
} S[40404040];

void upd(int pv, int nw, int s, int e, int t, LL v){
	int m=s+e>>1;
	if (s == e){
		S[nw].p = s, S[nw].v = v;
		return;
	}
	if (t <= m){
		S[nw].l = ++num, S[nw].r = S[pv].r;
		upd(S[pv].l, S[nw].l, s, m, t, v);
	}
	else{
		S[nw].r = ++num, S[nw].l = S[pv].l;
		upd(S[pv].r, S[nw].r, m+1, e, t, v);
	}
	if (S[S[nw].l].v < S[S[nw].r].v) S[nw].v = S[S[nw].l].v, S[nw].p = S[S[nw].l].p;
	else S[nw].v = S[S[nw].r].v, S[nw].p = S[S[nw].r].p;
}

int rmq(int id, int s, int e, int ts, int te){
	if (e < ts || te < s) return 0;
	if (ts <= s && e <= te) return id;
	int m = s+e>>1;
	int r1 = rmq(S[id].l, s, m, ts, te);
	int r2 = rmq(S[id].r, m+1, e, ts, te);
	return (S[r1].v < S[r2].v)?r1:r2;
}

priority_queue<pli> PQ;

int main(){
	S[0].v = INF;
	scanf("%d %d", &N, &K);
	for (int i=1; i<=N; i++){
		scanf("%d %d", &P[i].fi, &P[i].se);
		C.pb(pii(P[i].se, P[i].fi));
	}
	sort(C.begin(), C.end());
	sort(P+1, P+N+1);
	for (int i=1; i<=N; i++){
		Y[i] = lb(C.begin(), C.end(), pii(P[i].se, P[i].fi))-C.begin()+1;
		T1[i] = ++num, upd(T1[i-1], T1[i], 1, N, Y[i], -P[i].fi-P[i].se);
		T2[i] = ++num, upd(T2[i-1], T2[i], 1, N, Y[i], -P[i].fi+P[i].se);
		PQ.push(pli(-P[i].fi-P[i].se - S[rmq(T1[i], 1, N, 1, Y[i]-1)].v, i));
		PQ.push(pli(-P[i].fi+P[i].se - S[rmq(T2[i], 1, N, Y[i]+1, N)].v, -i));
	}
	while (K--){
		LL l;
		int x;
		tie(l, x) = PQ.top(); PQ.pop();
		printf("%lld\n", -l);
		if (x>0){
			int t = rmq(T1[x], 1, N, 1, Y[x]-1);
			int p=T1[x]; T1[x] = ++num;
			upd(p, T1[x], 1, N, S[t].p, INF);
			PQ.push(pli(-P[x].fi-P[x].se - S[rmq(T1[x], 1, N, 1, Y[x]-1)].v, x));
		}
		else{
			x=-x;
			int t = rmq(T2[x], 1, N, Y[x]+1, N);
			int p=T2[x]; T2[x] = ++num;
			upd(p, T2[x], 1, N, S[t].p, INF);
			PQ.push(pli(-P[x].fi+P[x].se - S[rmq(T2[x], 1, N, Y[x]+1, N)].v, -x));
		}
	}
	return 0;
}

Compilation message

road_construction.cpp: In function 'void upd(int, int, int, int, int, LL)':
road_construction.cpp:24:9: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   24 |  int m=s+e>>1;
      |        ~^~
road_construction.cpp: In function 'int rmq(int, int, int, int, int)':
road_construction.cpp:44:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |  int m = s+e>>1;
      |          ~^~
road_construction.cpp: In function 'int main()':
road_construction.cpp:54:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   54 |  scanf("%d %d", &N, &K);
      |  ~~~~~^~~~~~~~~~~~~~~~~
road_construction.cpp:56:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |   scanf("%d %d", &P[i].fi, &P[i].se);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 503 ms 67900 KB Output is correct
2 Correct 513 ms 67876 KB Output is correct
3 Correct 451 ms 65204 KB Output is correct
4 Correct 302 ms 65244 KB Output is correct
5 Correct 391 ms 66748 KB Output is correct
6 Correct 6 ms 1356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1211 ms 350028 KB Output is correct
2 Correct 1140 ms 353136 KB Output is correct
3 Correct 217 ms 65136 KB Output is correct
4 Correct 841 ms 352844 KB Output is correct
5 Correct 918 ms 352988 KB Output is correct
6 Correct 881 ms 353080 KB Output is correct
7 Correct 860 ms 352428 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1083 ms 237548 KB Output is correct
2 Correct 865 ms 237624 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 537 ms 237556 KB Output is correct
5 Correct 708 ms 237568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1083 ms 237548 KB Output is correct
2 Correct 865 ms 237624 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 537 ms 237556 KB Output is correct
5 Correct 708 ms 237568 KB Output is correct
6 Correct 901 ms 237600 KB Output is correct
7 Correct 1046 ms 237540 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 2 ms 204 KB Output is correct
10 Correct 884 ms 237568 KB Output is correct
11 Correct 518 ms 237680 KB Output is correct
12 Correct 598 ms 237520 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 503 ms 67900 KB Output is correct
2 Correct 513 ms 67876 KB Output is correct
3 Correct 451 ms 65204 KB Output is correct
4 Correct 302 ms 65244 KB Output is correct
5 Correct 391 ms 66748 KB Output is correct
6 Correct 6 ms 1356 KB Output is correct
7 Correct 1091 ms 195172 KB Output is correct
8 Correct 1128 ms 195184 KB Output is correct
9 Correct 298 ms 65312 KB Output is correct
10 Correct 742 ms 194528 KB Output is correct
11 Correct 1033 ms 194360 KB Output is correct
12 Correct 641 ms 195208 KB Output is correct
13 Correct 684 ms 193684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 503 ms 67900 KB Output is correct
2 Correct 513 ms 67876 KB Output is correct
3 Correct 451 ms 65204 KB Output is correct
4 Correct 302 ms 65244 KB Output is correct
5 Correct 391 ms 66748 KB Output is correct
6 Correct 6 ms 1356 KB Output is correct
7 Correct 1211 ms 350028 KB Output is correct
8 Correct 1140 ms 353136 KB Output is correct
9 Correct 217 ms 65136 KB Output is correct
10 Correct 841 ms 352844 KB Output is correct
11 Correct 918 ms 352988 KB Output is correct
12 Correct 881 ms 353080 KB Output is correct
13 Correct 860 ms 352428 KB Output is correct
14 Correct 1083 ms 237548 KB Output is correct
15 Correct 865 ms 237624 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 537 ms 237556 KB Output is correct
18 Correct 708 ms 237568 KB Output is correct
19 Correct 901 ms 237600 KB Output is correct
20 Correct 1046 ms 237540 KB Output is correct
21 Correct 1 ms 204 KB Output is correct
22 Correct 2 ms 204 KB Output is correct
23 Correct 884 ms 237568 KB Output is correct
24 Correct 518 ms 237680 KB Output is correct
25 Correct 598 ms 237520 KB Output is correct
26 Correct 1091 ms 195172 KB Output is correct
27 Correct 1128 ms 195184 KB Output is correct
28 Correct 298 ms 65312 KB Output is correct
29 Correct 742 ms 194528 KB Output is correct
30 Correct 1033 ms 194360 KB Output is correct
31 Correct 641 ms 195208 KB Output is correct
32 Correct 684 ms 193684 KB Output is correct
33 Correct 2011 ms 355884 KB Output is correct
34 Correct 2020 ms 355968 KB Output is correct
35 Correct 1509 ms 355144 KB Output is correct
36 Correct 1045 ms 355920 KB Output is correct
37 Correct 1161 ms 355948 KB Output is correct
38 Correct 1264 ms 354632 KB Output is correct