Submission #63176966


Source Code Expand

#include <bits/stdc++.h>
//#define int long long 
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define fr first
#define se second
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
mt19937_64 rnd(time(0));

const int N = 2e5 + 10;

int n, m, q;
int h[510][510];
map<pair<pii,pii>,int> ans;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};


void BFS(int X, int Y){
	priority_queue<array<int,3>> pq;
	ans[{{X,Y},{X,Y}}] = h[X][Y];
	pq.push({h[X][Y], X, Y});
	while(pq.size()){
		auto [H, x, y] = pq.top();
		pq.pop();
		for(int k = 0; k < 4; k ++){
			int tx = x + dx[k];
			int ty = y + dy[k];
			if(tx <= 0 || ty <= 0 || tx > n || ty > m || ans.count({{X,Y},{tx,ty}})) continue;
			ans[{{X,Y},{tx,ty}}] = min(H, h[tx][ty]);
			pq.push({ans[{{X,Y},{tx,ty}}], tx, ty});
		}
	} 
}

void solve()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i ++){
		for(int j = 1; j <= m; j ++){
			cin >> h[i][j];
		}
	}
	
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= m; j ++){
			BFS(i, j);
		}
	}

	// cout << ans.count({{1,1},{3,1}}) << endl;
	// return;

	cin >> q;
	while(q--){
		int a, b, y, c, d, z;
		cin >> a >> b >> y >> c >> d >> z;
		int H = ans[{{a,b},{c,d}}];
		// cout << H << endl;
		if(y <= H){ // 可以直接到(c,d)
			cout << abs(z - y) << endl;
		}
		else{ // 必须先下楼到H,再到z
			cout << (y - H) + abs(z - H) << endl;
		}
	}
}


signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T=1; //cin>>T;
	while(T--) solve();
	return 0;
}

Submission Info

Submission Time
Task G - Dense Buildings
User jjjxs
Language C++ 17 (gcc 12.2)
Score 0
Code Size 1634 Byte
Status TLE
Exec Time 5546 ms
Memory 478236 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 600
Status
AC × 1
AC × 3
TLE × 35
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3544 KiB
hand_00.txt TLE 5542 ms 462356 KiB
hand_01.txt TLE 5546 ms 478236 KiB
hand_02.txt TLE 5535 ms 318300 KiB
hand_03.txt TLE 5542 ms 464392 KiB
hand_04.txt TLE 5542 ms 462972 KiB
hand_05.txt AC 49 ms 3616 KiB
hand_06.txt AC 1 ms 3620 KiB
random_00.txt TLE 5535 ms 322088 KiB
random_01.txt TLE 5536 ms 336188 KiB
random_02.txt TLE 5535 ms 324708 KiB
random_03.txt TLE 5535 ms 313872 KiB
random_04.txt TLE 5537 ms 303660 KiB
random_05.txt TLE 5535 ms 328092 KiB
random_06.txt TLE 5535 ms 324172 KiB
random_07.txt TLE 5533 ms 355184 KiB
random_08.txt TLE 5535 ms 334844 KiB
random_09.txt TLE 5535 ms 326480 KiB
random_10.txt TLE 5534 ms 318080 KiB
random_11.txt TLE 5536 ms 335212 KiB
random_12.txt TLE 5535 ms 332612 KiB
random_13.txt TLE 5535 ms 328800 KiB
random_14.txt TLE 5535 ms 326780 KiB
random_15.txt TLE 5534 ms 309708 KiB
random_16.txt TLE 5536 ms 336068 KiB
random_17.txt TLE 5535 ms 324936 KiB
random_18.txt TLE 5535 ms 335808 KiB
random_19.txt TLE 5536 ms 337704 KiB
random_20.txt TLE 5538 ms 325392 KiB
random_21.txt TLE 5535 ms 324848 KiB
random_22.txt TLE 5536 ms 339624 KiB
random_23.txt TLE 5539 ms 339940 KiB
random_24.txt TLE 5535 ms 326188 KiB
random_25.txt TLE 5536 ms 341792 KiB
random_26.txt TLE 5535 ms 331712 KiB
random_27.txt TLE 5536 ms 339384 KiB
random_28.txt TLE 5535 ms 325980 KiB
random_29.txt TLE 5536 ms 330492 KiB