답안 #827650

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
827650 2023-08-16T15:51:40 Z MohamedAhmed04 유괴 2 (JOI17_abduction2) C++14
100 / 100
592 ms 17416 KB
#include <bits/stdc++.h>

using namespace std ;

const int MAX = 5e4 + 10 ;

int a[MAX] , b[MAX] ;
int n , m , q ;

int table_row[MAX][20] , table_col[MAX][20] ;
int lg[MAX] ;

void preprocess()
{
	int sz = max(n , m) ;
	lg[1] = 0 ;
	for(int i = 2 ; i <= sz ; ++i)
		lg[i] = lg[i/2] + 1 ;
	for(int i = 1 ; i <= sz ; ++i)
		table_row[i][0] = a[i] , table_col[i][0] = b[i] ;
	for(int j = 1 ; (1 << j) <= sz ; ++j)
	{
		for(int i = 1 ; i + (1 << j) - 1 <= sz ; ++i)
		{
			table_row[i][j] = max(table_row[i][j-1] , table_row[i + (1 << (j-1))][j-1]) ;
			table_col[i][j] = max(table_col[i][j-1] , table_col[i + (1 << (j-1))][j-1]) ;
		}
	}
}

int Range_Row_Max(int l , int r)
{
	if(l > r)
		return 0 ;
	int k = lg[r-l+1] ;
	return max(table_row[l][k] , table_row[r - (1 << k) + 1][k]) ;
}

int Range_Col_Max(int l , int r)
{
	if(l > r)
		return 0 ;
	int k = lg[r-l+1] ;
	return max(table_col[l][k] , table_col[r - (1 << k) + 1][k]) ;
}

int FindLeft(int i , int j)
{
	if(i < 1 || i > n || j < 1 || j > m)
		return -1 ;
	int l = 1 , r = j-1 ;
	int idx = -1 ;
	while(l <= r)
	{
		int mid = (l + r) >> 1 ;
		if(Range_Col_Max(mid , j-1) > a[i])
			idx = mid , l = mid+1 ;
		else
			r = mid-1 ;
	}
	return idx ;
}

int FindRight(int i , int j)
{
	if(i < 1 || i > n || j < 1 || j > m)
		return -1 ;
	int l = j+1 , r = m ;
	int idx = -1 ;
	while(l <= r)
	{
		int mid = (l + r) >> 1 ;
		if(Range_Col_Max(j+1 , mid) > a[i])
			idx = mid , r = mid-1 ;
		else
			l = mid+1 ;
	}
	return idx ;
}

int FindUp(int i , int j)
{
	if(i < 1 || i > n || j < 1 || j > m)
		return -1 ;
	int l = 1 , r = i-1 ;
	int idx = -1 ;
	while(l <= r)
	{
		int mid = (l + r) >> 1 ;
		if(Range_Row_Max(mid , i-1) > b[j])
			idx = mid , l = mid+1 ;
		else
			r = mid-1 ;
	}
	return idx ;
}

int FindDown(int i , int j)
{
	if(i < 1 || i > n || j < 1 || j > m)
		return -1 ;
	int l = i+1 , r = n ;
	int idx = -1 ;
	while(l <= r)
	{
		int mid = (l + r) >> 1 ;
		if(Range_Row_Max(i+1 , mid) > b[j])
			idx = mid , r = mid-1 ;
		else
			l = mid+1 ;
	}
	return idx ;
}

vector< pair<int , int> >vp ;
vector< pair<int , long long> >vr[MAX] , vc[MAX] ;

long long solve(int i , int j)
{
	for(int i = 1 ; i <= max(n , m) ; ++i)
		vr[i].clear() , vc[i].clear() ;
	long long ans = 0 ;
	//preprocess some points
	int j2 = FindLeft(i , j) ;
	if(j2 == -1)
		ans = max(ans , 1ll*j-1) ;
	else
		vc[j2].emplace_back(i , j-j2) ;
	j2 = FindRight(i , j) ;
	if(j2 == -1)
		ans = max(ans , 1ll*m-j) ;
	else
		vc[j2].emplace_back(i , j2-j) ;
	int i2 = FindUp(i , j) ;
	if(i2 == -1)
		ans = max(ans , 1ll*i-1) ;
	else
		vr[i2].emplace_back(j , i-i2) ;
	i2 = FindDown(i , j) ;
	if(i2 == -1)
		ans = max(ans , 1ll*n-i) ;
	else
		vr[i2].emplace_back(j , i2-i) ;
	//Answer
	for(auto &p : vp)
	{
		if(p.second > 0)
		{
			i2 = p.second ;
			sort(vr[i2].begin() , vr[i2].end()) ;
			//Add to right columns
			long long prv = 2e9 , Max = -1e9 ;
			for(auto &p2 : vr[i2])
			{
				if(Range_Col_Max(prv+1 , p2.first-1) > a[i2])
				{
					j2 = FindRight(i2 , prv) ;
					vc[j2].emplace_back(i2 , Max + j2 - prv) ;
					Max = p2.second ;
				}
				else
					Max = max(p2.second , Max + p2.first-prv) ;
				prv = p2.first ;
			}
			j2 = FindRight(i2 , prv) ;
			if(j2 == -1)
				ans = max(ans , Max + m - prv) ;
			else
				vc[j2].emplace_back(i2 , Max + j2 - prv) ;
			//Add to left columns
			reverse(vr[i2].begin() , vr[i2].end()) ;
			prv = -2e9 , Max = -1e9 ;
			for(auto &p2 : vr[i2])
			{
				if(Range_Col_Max(p2.first+1 , prv-1) > a[i2])
				{
					j2 = FindLeft(i2 , prv) ;
					vc[j2].emplace_back(i2 , Max + prv - j2) ;
					Max = p2.second ;
				}
				else
					Max = max(p2.second , Max + prv - p2.first) ;
				prv = p2.first ;
			}
			j2 = FindLeft(i2 , prv) ;
			if(j2 == -1)
				ans = max(ans , Max + prv - 1) ;
			else
				vc[j2].emplace_back(i2 , Max + prv - j2) ;
		}
		else
		{
			j2 = -1 * p.second ;
			sort(vc[j2].begin() , vc[j2].end()) ;
			//Add to down rows
			long long prv = 2e9 , Max = -1e9 ;
			for(auto &p2 : vc[j2])
			{
				if(Range_Row_Max(prv+1 , p2.first-1) > b[j2])
				{
					i2 = FindDown(prv , j2) ;
					vr[i2].emplace_back(j2 , Max + i2 - prv) ;
					Max = p2.second ;
				}
				else
					Max = max(p2.second , Max + p2.first-prv) ;
				prv = p2.first ;
			}
			i2 = FindDown(prv , j2) ;
			if(i2 == -1)
				ans = max(ans , Max + n - prv) ;
			else
				vr[i2].emplace_back(j2 , Max + i2 - prv) ;
			//Add to up columns
			reverse(vc[j2].begin() , vc[j2].end()) ;
			prv = -2e9 , Max = -1e9 ;
			for(auto &p2 : vc[j2])
			{
				if(Range_Row_Max(p2.first+1 , prv-1) > b[j2])
				{
					i2 = FindUp(prv , j2) ;
					vr[i2].emplace_back(j2 , Max + prv - i2) ;
					Max = p2.second ;
				}
				else
					Max = max(p2.second , Max + prv - p2.first) ;
				prv = p2.first ;
			}
			i2 = FindUp(prv , j2) ;
			if(i2 == -1)
				ans = max(ans , Max + prv - 1) ;
			else
				vr[i2].emplace_back(j2 , Max + prv - i2) ;
		}
	}
	return ans ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n>>m>>q ;
	for(int i = 1 ; i <= n ; ++i)
		cin>>a[i] ;
	for(int i = 1 ; i <= m ; ++i)
		cin>>b[i] ;
	for(int i = 1 ; i <= n ; ++i)
		vp.emplace_back(a[i] , i) ;
	for(int i = 1 ; i <= m ; ++i)
		vp.emplace_back(b[i] , -i) ;
	sort(vp.begin() , vp.end()) ;
	preprocess() ;
	while(q--)
	{
		int i , j ;
		cin>>i>>j ;
		cout<<solve(i , j)<<"\n" ;
	}
	return 0 ;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 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 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 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 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 3076 KB Output is correct
18 Correct 2 ms 3076 KB Output is correct
19 Correct 2 ms 3072 KB Output is correct
20 Correct 2 ms 3208 KB Output is correct
21 Correct 2 ms 3156 KB Output is correct
22 Correct 3 ms 3156 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 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 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 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 3076 KB Output is correct
18 Correct 2 ms 3076 KB Output is correct
19 Correct 2 ms 3072 KB Output is correct
20 Correct 2 ms 3208 KB Output is correct
21 Correct 2 ms 3156 KB Output is correct
22 Correct 3 ms 3156 KB Output is correct
23 Correct 23 ms 12912 KB Output is correct
24 Correct 23 ms 12900 KB Output is correct
25 Correct 25 ms 12896 KB Output is correct
26 Correct 23 ms 12916 KB Output is correct
27 Correct 23 ms 12964 KB Output is correct
28 Correct 29 ms 13820 KB Output is correct
29 Correct 24 ms 12892 KB Output is correct
30 Correct 29 ms 14708 KB Output is correct
31 Correct 37 ms 15252 KB Output is correct
32 Correct 19 ms 12944 KB Output is correct
33 Correct 23 ms 13312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3028 KB Output is correct
2 Correct 8 ms 3156 KB Output is correct
3 Correct 8 ms 3140 KB Output is correct
4 Correct 8 ms 3028 KB Output is correct
5 Correct 8 ms 3116 KB Output is correct
6 Correct 12 ms 3076 KB Output is correct
7 Correct 13 ms 3164 KB Output is correct
8 Correct 17 ms 3272 KB Output is correct
9 Correct 19 ms 3244 KB Output is correct
10 Correct 20 ms 3200 KB Output is correct
11 Correct 15 ms 3300 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 2644 KB Output is correct
4 Correct 1 ms 2644 KB Output is correct
5 Correct 2 ms 2644 KB Output is correct
6 Correct 1 ms 2644 KB Output is correct
7 Correct 1 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 2 ms 3028 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 3 ms 3028 KB Output is correct
17 Correct 2 ms 3076 KB Output is correct
18 Correct 2 ms 3076 KB Output is correct
19 Correct 2 ms 3072 KB Output is correct
20 Correct 2 ms 3208 KB Output is correct
21 Correct 2 ms 3156 KB Output is correct
22 Correct 3 ms 3156 KB Output is correct
23 Correct 23 ms 12912 KB Output is correct
24 Correct 23 ms 12900 KB Output is correct
25 Correct 25 ms 12896 KB Output is correct
26 Correct 23 ms 12916 KB Output is correct
27 Correct 23 ms 12964 KB Output is correct
28 Correct 29 ms 13820 KB Output is correct
29 Correct 24 ms 12892 KB Output is correct
30 Correct 29 ms 14708 KB Output is correct
31 Correct 37 ms 15252 KB Output is correct
32 Correct 19 ms 12944 KB Output is correct
33 Correct 23 ms 13312 KB Output is correct
34 Correct 9 ms 3028 KB Output is correct
35 Correct 8 ms 3156 KB Output is correct
36 Correct 8 ms 3140 KB Output is correct
37 Correct 8 ms 3028 KB Output is correct
38 Correct 8 ms 3116 KB Output is correct
39 Correct 12 ms 3076 KB Output is correct
40 Correct 13 ms 3164 KB Output is correct
41 Correct 17 ms 3272 KB Output is correct
42 Correct 19 ms 3244 KB Output is correct
43 Correct 20 ms 3200 KB Output is correct
44 Correct 15 ms 3300 KB Output is correct
45 Correct 158 ms 12872 KB Output is correct
46 Correct 166 ms 13008 KB Output is correct
47 Correct 177 ms 12988 KB Output is correct
48 Correct 155 ms 12932 KB Output is correct
49 Correct 170 ms 12872 KB Output is correct
50 Correct 349 ms 14200 KB Output is correct
51 Correct 315 ms 14328 KB Output is correct
52 Correct 592 ms 16424 KB Output is correct
53 Correct 536 ms 16308 KB Output is correct
54 Correct 561 ms 16036 KB Output is correct
55 Correct 440 ms 17416 KB Output is correct
56 Correct 282 ms 17064 KB Output is correct
57 Correct 180 ms 15716 KB Output is correct
58 Correct 176 ms 15716 KB Output is correct
59 Correct 175 ms 16028 KB Output is correct