답안 #698416

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
698416 2023-02-13T12:24:06 Z amunduzbaev 유괴 2 (JOI17_abduction2) C++17
44 / 100
1547 ms 20728 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<ar<int, 2>> 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});
		}
		
		int res = 0;
		for(auto [t, i] : p){
			if(tot[pos[t][i]].empty()) continue;
			int c = t ^ 1;
			vector<ar<int, 2>>& v = tot[pos[t][i]];
			sort(v.begin(), v.end());
			int L = 0, R = 0;
			int l = v[0][0], r = v.back()[0];
			//~ 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){
		int 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, j - 1);
			
			if(r <= n[c]) res = max(res, solve(i, r) + abs(r - j));
			else res = max(res, 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, j - 1);
			
			if(r <= n[c]) res = max(res, solve(r, i) + abs(r - j));
			else res = max(res, 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:7: warning: 'res' may be used uninitialized in this function [-Wmaybe-uninitialized]
  115 |   int res = max(res, solve(i, j));
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2692 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2692 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3236 KB Output is correct
13 Correct 3 ms 3328 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 3 ms 3332 KB Output is correct
18 Correct 3 ms 3236 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3324 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3360 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2692 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3236 KB Output is correct
13 Correct 3 ms 3328 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 3 ms 3332 KB Output is correct
18 Correct 3 ms 3236 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3324 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3360 KB Output is correct
23 Correct 37 ms 17748 KB Output is correct
24 Correct 35 ms 17852 KB Output is correct
25 Correct 37 ms 17740 KB Output is correct
26 Correct 37 ms 17796 KB Output is correct
27 Correct 37 ms 17764 KB Output is correct
28 Correct 52 ms 18664 KB Output is correct
29 Correct 37 ms 17768 KB Output is correct
30 Correct 61 ms 18960 KB Output is correct
31 Correct 60 ms 19372 KB Output is correct
32 Correct 34 ms 17664 KB Output is correct
33 Correct 38 ms 17992 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 3352 KB Output is correct
2 Correct 9 ms 3328 KB Output is correct
3 Correct 8 ms 3248 KB Output is correct
4 Correct 8 ms 3284 KB Output is correct
5 Correct 8 ms 3236 KB Output is correct
6 Correct 36 ms 3284 KB Output is correct
7 Correct 36 ms 3372 KB Output is correct
8 Correct 50 ms 3388 KB Output is correct
9 Correct 55 ms 3408 KB Output is correct
10 Correct 60 ms 3396 KB Output is correct
11 Correct 76 ms 3420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2692 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2676 KB Output is correct
6 Correct 2 ms 2644 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 2 ms 2644 KB Output is correct
10 Correct 2 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 3 ms 3236 KB Output is correct
13 Correct 3 ms 3328 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 3 ms 3332 KB Output is correct
18 Correct 3 ms 3236 KB Output is correct
19 Correct 3 ms 3284 KB Output is correct
20 Correct 4 ms 3324 KB Output is correct
21 Correct 3 ms 3284 KB Output is correct
22 Correct 4 ms 3360 KB Output is correct
23 Correct 37 ms 17748 KB Output is correct
24 Correct 35 ms 17852 KB Output is correct
25 Correct 37 ms 17740 KB Output is correct
26 Correct 37 ms 17796 KB Output is correct
27 Correct 37 ms 17764 KB Output is correct
28 Correct 52 ms 18664 KB Output is correct
29 Correct 37 ms 17768 KB Output is correct
30 Correct 61 ms 18960 KB Output is correct
31 Correct 60 ms 19372 KB Output is correct
32 Correct 34 ms 17664 KB Output is correct
33 Correct 38 ms 17992 KB Output is correct
34 Correct 8 ms 3352 KB Output is correct
35 Correct 9 ms 3328 KB Output is correct
36 Correct 8 ms 3248 KB Output is correct
37 Correct 8 ms 3284 KB Output is correct
38 Correct 8 ms 3236 KB Output is correct
39 Correct 36 ms 3284 KB Output is correct
40 Correct 36 ms 3372 KB Output is correct
41 Correct 50 ms 3388 KB Output is correct
42 Correct 55 ms 3408 KB Output is correct
43 Correct 60 ms 3396 KB Output is correct
44 Correct 76 ms 3420 KB Output is correct
45 Correct 193 ms 17736 KB Output is correct
46 Correct 177 ms 17856 KB Output is correct
47 Correct 167 ms 17868 KB Output is correct
48 Correct 203 ms 17736 KB Output is correct
49 Correct 190 ms 17876 KB Output is correct
50 Correct 851 ms 19120 KB Output is correct
51 Correct 792 ms 19260 KB Output is correct
52 Correct 1417 ms 20268 KB Output is correct
53 Correct 1381 ms 19908 KB Output is correct
54 Correct 1306 ms 19812 KB Output is correct
55 Incorrect 1547 ms 20728 KB Output isn't correct
56 Halted 0 ms 0 KB -