답안 #796192

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796192 2023-07-28T07:40:55 Z 박상훈(#10069) Bitaro's travel (JOI23_travel) C++17
100 / 100
296 ms 25960 KB
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

constexpr ll INF = 2e18;

ll X[200200];

struct Seg1{
	ll tree[800800];

	void init(int i, int l, int r){
		if (l==r){
			tree[i] = X[l+1] - X[l] * 2;
			return;
		}

		int m = (l+r)>>1;
		init(i<<1, l, m); init(i<<1|1, m+1, r);
		tree[i] = max(tree[i<<1], tree[i<<1|1]);
	}

	int prf(int i, int l, int r, int s, ll x){
		if (r<s) return -1;
		if (tree[i] < x) return -1;
		if (l==r) return l;

		int m = (l+r)>>1;
		
		int ret = prf(i<<1, l, m, s, x);
		if (ret!=-1) return ret;

		return prf(i<<1|1, m+1, r, s, x);
	}
}treeR;

struct Seg2{
	ll tree[800800];

	void init(int i, int l, int r){
		if (l==r){
			tree[i] = X[l] * 2 - X[l-1];
			return;
		}

		int m = (l+r)>>1;
		init(i<<1, l, m); init(i<<1|1, m+1, r);
		tree[i] = max(tree[i<<1], tree[i<<1|1]);
	}

	int suf(int i, int l, int r, int e, ll x){
		if (e<l) return -1;
		if (tree[i] < x) return -1;
		if (l==r) return l;

		int m = (l+r)>>1;
		
		int ret = suf(i<<1|1, m+1, r, e, x);
		if (ret!=-1) return ret;

		return suf(i<<1, l, m, e, x);
	}
}treeL;

ll ans, cnt;
int n;

int goRight(ll x, ll p, int i){
	int ret = treeR.prf(1, 1, n-1, i, -p);
	if (ret==-1){
		ret = n;
		cnt++;
	}

	ans += X[ret] - x;

	// printf(" go right %lld %lld %d -> %lld\n", x, p, i, X[ret]);
	return ret;
}

int goLeft(ll x, ll q, int j){
	int ret = treeL.suf(1, 2, n, j, q+1);
	if (ret==-1){
		ret = 1;
		cnt++;
	}

	ans += x - X[ret];

	// printf(" go left %lld %lld %d -> %lld\n", x, q, j, X[ret]);
	return ret;
}

int main(){
	scanf("%d", &n);

	for (int i=1;i<=n;i++) scanf("%lld", X+i);
	X[0] = -INF;
	X[n+1] = INF;

	if (n > 1) treeR.init(1, 1, n-1);
	if (n > 1) treeL.init(1, 2, n);

	multiset<pair<ll, int>> st;
	for (int i=1;i<=n;i++) st.emplace(X[i], i);

	int q;
	scanf("%d", &q);
	while(q--){
		ll x;
		int idx = -1;
		scanf("%lld", &x);

		if (n==1){
			printf("%lld\n", abs(x - X[1]));
			continue;
		}

		ans = 0;
		cnt = 0;

		auto iter = st.lower_bound(pair<ll, int>(x, -1));
		if (iter==st.end()){
			printf("%lld\n", x - X[1]);
			continue;
		}

		if (iter==st.begin()){
			printf("%lld\n", X[n] - x);
			continue;
		}

		if (abs((prev(iter)->first)-x) <= abs((iter->first)-x)){
			idx = goLeft(x, iter->first, prev(iter)->second);
		}

		while(true){
			ll cur = (idx==-1)?x:X[idx];
			ll lim = (idx==-1)?(prev(iter)->first):X[idx-1];
			int fuck = (idx==-1)?(iter->second):(idx+1);

			idx = goRight(cur, lim, fuck);
			if (cnt==2) break;

			cur = X[idx];
			lim = X[idx+1];
			fuck = idx-1;

			idx = goLeft(cur, lim, fuck);
			if (cnt==2) break;
		}

		printf("%lld\n", ans);
	}
}

Compilation message

travel.cpp: In function 'int main()':
travel.cpp:96:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d", &n);
      |  ~~~~~^~~~~~~~~~
travel.cpp:98:30: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |  for (int i=1;i<=n;i++) scanf("%lld", X+i);
      |                         ~~~~~^~~~~~~~~~~~~
travel.cpp:109:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  109 |  scanf("%d", &q);
      |  ~~~~~^~~~~~~~~~
travel.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |   scanf("%lld", &x);
      |   ~~~~~^~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 440 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 440 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 55 ms 23736 KB Output is correct
18 Correct 56 ms 23684 KB Output is correct
19 Correct 60 ms 23740 KB Output is correct
20 Correct 57 ms 23756 KB Output is correct
21 Correct 56 ms 23704 KB Output is correct
22 Correct 56 ms 23628 KB Output is correct
23 Correct 55 ms 23720 KB Output is correct
24 Correct 54 ms 23724 KB Output is correct
25 Correct 59 ms 23720 KB Output is correct
26 Correct 54 ms 23660 KB Output is correct
27 Correct 54 ms 23640 KB Output is correct
28 Correct 54 ms 23716 KB Output is correct
29 Correct 55 ms 23660 KB Output is correct
30 Correct 55 ms 23744 KB Output is correct
31 Correct 55 ms 23680 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 312 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 258 ms 25168 KB Output is correct
8 Correct 254 ms 25124 KB Output is correct
9 Correct 264 ms 25044 KB Output is correct
10 Correct 239 ms 25124 KB Output is correct
11 Correct 250 ms 25068 KB Output is correct
12 Correct 284 ms 25056 KB Output is correct
13 Correct 44 ms 3372 KB Output is correct
14 Correct 32 ms 1476 KB Output is correct
15 Correct 250 ms 25536 KB Output is correct
16 Correct 202 ms 25544 KB Output is correct
17 Correct 191 ms 25516 KB Output is correct
18 Correct 44 ms 3324 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 512 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 440 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 444 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 55 ms 23736 KB Output is correct
18 Correct 56 ms 23684 KB Output is correct
19 Correct 60 ms 23740 KB Output is correct
20 Correct 57 ms 23756 KB Output is correct
21 Correct 56 ms 23704 KB Output is correct
22 Correct 56 ms 23628 KB Output is correct
23 Correct 55 ms 23720 KB Output is correct
24 Correct 54 ms 23724 KB Output is correct
25 Correct 59 ms 23720 KB Output is correct
26 Correct 54 ms 23660 KB Output is correct
27 Correct 54 ms 23640 KB Output is correct
28 Correct 54 ms 23716 KB Output is correct
29 Correct 55 ms 23660 KB Output is correct
30 Correct 55 ms 23744 KB Output is correct
31 Correct 55 ms 23680 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 312 KB Output is correct
37 Correct 1 ms 212 KB Output is correct
38 Correct 258 ms 25168 KB Output is correct
39 Correct 254 ms 25124 KB Output is correct
40 Correct 264 ms 25044 KB Output is correct
41 Correct 239 ms 25124 KB Output is correct
42 Correct 250 ms 25068 KB Output is correct
43 Correct 284 ms 25056 KB Output is correct
44 Correct 44 ms 3372 KB Output is correct
45 Correct 32 ms 1476 KB Output is correct
46 Correct 250 ms 25536 KB Output is correct
47 Correct 202 ms 25544 KB Output is correct
48 Correct 191 ms 25516 KB Output is correct
49 Correct 44 ms 3324 KB Output is correct
50 Correct 296 ms 25912 KB Output is correct
51 Correct 289 ms 25928 KB Output is correct
52 Correct 261 ms 25928 KB Output is correct
53 Correct 287 ms 25892 KB Output is correct
54 Correct 274 ms 25916 KB Output is correct
55 Correct 274 ms 25960 KB Output is correct
56 Correct 135 ms 25804 KB Output is correct
57 Correct 144 ms 25852 KB Output is correct
58 Correct 136 ms 25800 KB Output is correct