답안 #698417

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698417 2023-02-13T12:26:28 Z amunduzbaev 유괴 2 (JOI17_abduction2) C++17
100 / 100
1495 ms 22084 KB
#include "bits/stdc++.h"
using namespace std;

#define ar array
typedef long long ll;
//~ #define int ll

const int N = 5e4 + 5;
const int M = 16;
vector<pair<int, ll>> tot[N + N];
int a[2][N], pos[2][N];
int L[2][N][M], R[2][N][M];

signed main(){
	ios::sync_with_stdio(0); cin.tie(0);
	
	ar<int, 2> n; int q; 
	cin >> n[0] >> n[1] >> q;
	
	for(int t=0;t<2;t++){
		a[t][0] = a[t][n[t] + 1] = 2e9;
		for(int i=1;i<=n[t];i++){
			cin >> a[t][i];
		}
		
		vector<int> ss; ss.push_back(0);
		for(int i=1;i<=n[t];i++){
			while(!ss.empty() && a[t][ss.back()] < a[t][i]){
				ss.pop_back();
			}
			
			L[t][i][0] = ss.back();
			ss.push_back(i);
		}
		
		ss.clear(); ss.push_back(n[t] + 1);
		for(int i=n[t];i>0;i--){
			while(!ss.empty() && a[t][ss.back()] < a[t][i]){
				ss.pop_back();
			}
			
			R[t][i][0] = ss.back();
			ss.push_back(i);
		}
		L[t][0][0] = 0;
		R[t][n[t] + 1][0] = n[t] + 1;
		for(int j=1;j<M;j++){
			for(int i=0;i<=n[t]+1;i++){
				L[t][i][j] = L[t][L[t][i][j-1]][j-1];
				R[t][i][j] = R[t][R[t][i][j-1]][j-1];
			}
		}
	}
	vector<ar<int, 2>> p;
	for(int t=0;t<2;t++){
		for(int i=1;i<=n[t];i++){
			p.push_back({t, i});
		}
	}
	sort(p.begin(), p.end(), [&](auto i, auto j){
		return a[i[0]][i[1]] < a[j[0]][j[1]];
	});
	for(int i=0;i<(int)p.size();i++){
		pos[p[i][0]][p[i][1]] = i;
	}
	
	auto solve = [&](int i, int j){
		//~ memset(d, 0, sizeof d);
		if(a[0][i] > a[1][j]){
			tot[pos[0][i]].push_back({j, 0});
		} else {
			tot[pos[1][j]].push_back({i, 0});
		}
		
		ll res = 0;
		for(auto [t, i] : p){
			if(tot[pos[t][i]].empty()) continue;
			int c = t ^ 1;
			vector<pair<int, ll>>& v = tot[pos[t][i]];
			sort(v.begin(), v.end());
			ll L = 0, R = 0;
			int l = v[0].first, r = v.back().first;
			//~ cout<<l<<" "<<r<<"\n";
			for(int j=M-1;~j;j--){
				if(a[c][::L[c][l][j]] < a[t][i]) l = ::L[c][l][j];
				if(a[c][::R[c][r][j]] < a[t][i]) r = ::R[c][r][j];
			}
			//~ cout<<l<<" "<<r<<"\n";
			l = ::L[c][l][0];
			r = ::R[c][r][0];
			for(auto [j, d] : v){
				//~ cout<<t<<" "<<i<<" "<<j<<" "<<d<<"\n";
				L = max(L, d + abs(j - l));
				R = max(R, d + abs(j - r));
			}
			//~ cout<<i<<" "<<l<<" "<<r<<"\n";
			if(l){
				tot[pos[c][l]].push_back({i, L});
			} else {
				res = max(res, L - 1);
			}
			
			if(r <= n[c]){
				tot[pos[c][r]].push_back({i, R});
			} else {
				res = max(res, R - 1);
			}
			v.clear();
		}
		//~ cout<<i<<" "<<j<<" "<<res<<"\n";
		return res;
	};
	
	auto Answer = [&](int i, int j){
		ll res = max(res, solve(i, j));
		if(a[0][i] < a[1][j]){
			int c = 1, t = 0;
			int l = j - 1, r = j + 1;
			while(l && a[c][l] < a[t][i]) l--;
			while(r <= n[c] && a[c][r] < a[t][i]) r++;
			
			if(l) res = max(res, solve(i, l) + abs(j - l));
			else res = max(res, 1ll * j - 1);
			
			if(r <= n[c]) res = max(res, solve(i, r) + abs(r - j));
			else res = max(res, 1ll * n[c] - j);
		}
		else { swap(i, j);
			int c = 0, t = 1;
			int l = j - 1, r = j + 1;
			while(l && a[c][l] < a[t][i]) l--;
			while(r <= n[c] && a[c][r] < a[t][i]) r++;
			
			if(l) res = max(res, solve(l, i) + abs(j - l));
			else res = max(res, 1ll * j - 1);
			
			if(r <= n[c]) res = max(res, solve(r, i) + abs(r - j));
			else res = max(res, 1ll * n[c] - j);
		}
		
		return res;
	};
	
	while(q--){
		int i, j; cin >> i >> j;
		cout<<Answer(i, j)<<"\n";
	}
}

Compilation message

abduction2.cpp: In function 'int main()':
abduction2.cpp:115:6: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
  115 |   ll res = max(res, solve(i, j));
      |      ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 3 ms 3284 KB Output is correct
13 Correct 2 ms 3284 KB Output is correct
14 Correct 3 ms 3284 KB Output is correct
15 Correct 3 ms 3284 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 2 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3284 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 3 ms 3284 KB Output is correct
13 Correct 2 ms 3284 KB Output is correct
14 Correct 3 ms 3284 KB Output is correct
15 Correct 3 ms 3284 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 2 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3284 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3412 KB Output is correct
23 Correct 45 ms 16768 KB Output is correct
24 Correct 36 ms 16856 KB Output is correct
25 Correct 35 ms 16836 KB Output is correct
26 Correct 36 ms 16884 KB Output is correct
27 Correct 35 ms 16908 KB Output is correct
28 Correct 50 ms 17724 KB Output is correct
29 Correct 37 ms 16800 KB Output is correct
30 Correct 55 ms 18572 KB Output is correct
31 Correct 66 ms 19064 KB Output is correct
32 Correct 31 ms 16736 KB Output is correct
33 Correct 39 ms 17264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3284 KB Output is correct
2 Correct 7 ms 3284 KB Output is correct
3 Correct 7 ms 3284 KB Output is correct
4 Correct 8 ms 3316 KB Output is correct
5 Correct 8 ms 3284 KB Output is correct
6 Correct 36 ms 3336 KB Output is correct
7 Correct 33 ms 3284 KB Output is correct
8 Correct 49 ms 3408 KB Output is correct
9 Correct 56 ms 3420 KB Output is correct
10 Correct 58 ms 3436 KB Output is correct
11 Correct 72 ms 3424 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2648 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 1 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 1 ms 2644 KB Output is correct
12 Correct 3 ms 3284 KB Output is correct
13 Correct 2 ms 3284 KB Output is correct
14 Correct 3 ms 3284 KB Output is correct
15 Correct 3 ms 3284 KB Output is correct
16 Correct 3 ms 3284 KB Output is correct
17 Correct 2 ms 3284 KB Output is correct
18 Correct 3 ms 3284 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3284 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3412 KB Output is correct
23 Correct 45 ms 16768 KB Output is correct
24 Correct 36 ms 16856 KB Output is correct
25 Correct 35 ms 16836 KB Output is correct
26 Correct 36 ms 16884 KB Output is correct
27 Correct 35 ms 16908 KB Output is correct
28 Correct 50 ms 17724 KB Output is correct
29 Correct 37 ms 16800 KB Output is correct
30 Correct 55 ms 18572 KB Output is correct
31 Correct 66 ms 19064 KB Output is correct
32 Correct 31 ms 16736 KB Output is correct
33 Correct 39 ms 17264 KB Output is correct
34 Correct 7 ms 3284 KB Output is correct
35 Correct 7 ms 3284 KB Output is correct
36 Correct 7 ms 3284 KB Output is correct
37 Correct 8 ms 3316 KB Output is correct
38 Correct 8 ms 3284 KB Output is correct
39 Correct 36 ms 3336 KB Output is correct
40 Correct 33 ms 3284 KB Output is correct
41 Correct 49 ms 3408 KB Output is correct
42 Correct 56 ms 3420 KB Output is correct
43 Correct 58 ms 3436 KB Output is correct
44 Correct 72 ms 3424 KB Output is correct
45 Correct 160 ms 16840 KB Output is correct
46 Correct 182 ms 16776 KB Output is correct
47 Correct 176 ms 16904 KB Output is correct
48 Correct 161 ms 16816 KB Output is correct
49 Correct 170 ms 16768 KB Output is correct
50 Correct 829 ms 18324 KB Output is correct
51 Correct 756 ms 18228 KB Output is correct
52 Correct 1368 ms 20352 KB Output is correct
53 Correct 1342 ms 19868 KB Output is correct
54 Correct 1258 ms 19812 KB Output is correct
55 Correct 1495 ms 21492 KB Output is correct
56 Correct 606 ms 22084 KB Output is correct
57 Correct 284 ms 20680 KB Output is correct
58 Correct 270 ms 20656 KB Output is correct
59 Correct 291 ms 20928 KB Output is correct