#ifndef DEBUG
#pragma GCC optimize ("Ofast")
#endif
#include <bits/stdc++.h>
//Syntax Sugars
using namespace std;
using f64 = double;
using i8=char;using i16=short;using i32=int;using i64=long long;
using u8=unsigned char;using u16=unsigned short;using u32=unsigned;using u64=unsigned long long;
const f64 pi=acos(-1);
const f64 eps=1e-12;
const i32 prime=i32(2e9 + 11);
i32 mod=i32(1e9 + 7);
template<typename T> T inf() { return numeric_limits<T>::max() / 2; }
#define forh(var, s, e) for(int var=s; (int)s<=var && var<(int)e; ++var)//for: half-opened range
#define forhi(var, s, e) for(int var=e-1; (int)s<=var && var<(int)e; --var)//inversion
#define forho(var, s, e) int var=s; for(; (int)s<=var && var<(int)e; ++var)//out declaration
#define forhoi(var, s, e) int var=e-1; for(; (int)s<=var && var<(int)e; --var)
#define forc(var, s, e) for(int var=s; (int)s<=var && var<=(int)e; ++var)//for: closed range
#define forci(var, s, e) for(int var=e; (int)s<=var && var<=(int)e; --var)//inversion
#define forco(var, s, e) int var=s; for(; (int)s<=var && var<=(int)e; ++var)//out declaration
#define forcoi(var, s, e) int var=e; for(; (int)s<=var && var<=(int)e; --var)
//Extensions
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
//Settings
#define endl '\n'//do not use when solving interactive problem!!!
struct State{
int val, y, x;
bool operator<(const State& r)const{
return val>r.val;
}
};
int n,m,h;
int arr1[1010][1010];
int arr2[1010][1010];
int dist[1010][1010];
bool safe(int y, int x){return 0<=y&&y<n&&0<=x&&x<m;}
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
cout<<fixed<<setprecision(11);
srand((u32)time(0));
cin>>n>>m>>h;
forh(i,0,n+1)
forh(j,0,m)
cin>>arr1[i][j];
forh(i,0,n)
forh(j,0,m+1)
cin>>arr2[i][j];
priority_queue<State> pq;
forh(i,0,n)
forh(j,0,m)
dist[i][j]=h;
forh(j,0,m){
if(arr1[0][j]!=-1)
pq.push({dist[0][j]=min(dist[0][j], arr1[0][j]), 0, j});
if(arr1[n][j]!=-1)
pq.push({dist[n-1][j]=min(dist[n-1][j], arr1[n][j]), n-1, j});
}
forh(i,0,n){
if(arr2[i][0]!=-1)
pq.push({dist[i][0]=min(dist[i][0], arr2[i][0]), i, 0});
if(arr2[i][m]!=-1)
pq.push({dist[i][m-1]=min(dist[i][m-1], arr2[i][m]), i, m-1});
}
while(pq.size()){
auto cur=pq.top();
pq.pop();
if(cur.val<dist[cur.y][cur.x])
continue;
int ny,nx;
ny=cur.y+1, nx=cur.x;
if(safe(ny,nx)&&arr1[ny][nx]!=-1&&dist[ny][nx]>max(cur.val, arr1[ny][nx]))
pq.push({dist[ny][nx]=max(cur.val, arr1[ny][nx]), ny, nx});
ny=cur.y, nx=cur.x+1;
if(safe(ny,nx)&&arr2[ny][nx]!=-1&&dist[ny][nx]>max(cur.val, arr2[ny][nx]))
pq.push({dist[ny][nx]=max(cur.val, arr2[ny][nx]), ny, nx});
ny=cur.y-1, nx=cur.x;
if(safe(ny,nx)&&arr1[ny+1][nx]!=-1&&dist[ny][nx]>max(cur.val, arr1[ny+1][nx]))
pq.push({dist[ny][nx]=max(cur.val, arr1[ny+1][nx]), ny, nx});
ny=cur.y, nx=cur.x-1;
if(safe(ny,nx)&&arr2[ny][nx+1]!=-1&&dist[ny][nx]>max(cur.val, arr2[ny][nx+1]))
pq.push({dist[ny][nx]=max(cur.val, arr2[ny][nx+1]), ny, nx});
}
i64 ans=0;
forh(i,0,n)
forh(j,0,m)
ans+=dist[i][j];
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
512 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
4 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
12152 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
129 ms |
11128 KB |
Output is correct |
4 |
Correct |
194 ms |
11436 KB |
Output is correct |
5 |
Correct |
2 ms |
428 KB |
Output is correct |
6 |
Correct |
195 ms |
12408 KB |
Output is correct |
7 |
Correct |
136 ms |
11540 KB |
Output is correct |
8 |
Correct |
307 ms |
18528 KB |
Output is correct |
9 |
Correct |
136 ms |
11272 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
249 ms |
13624 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1024 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
484 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
3 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
896 KB |
Output is correct |
8 |
Correct |
4 ms |
1024 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
3 ms |
896 KB |
Output is correct |
11 |
Correct |
3 ms |
896 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
1024 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
1024 KB |
Output is correct |
17 |
Correct |
3 ms |
896 KB |
Output is correct |
18 |
Correct |
4 ms |
1024 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
940 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
150 ms |
18168 KB |
Output is correct |
7 |
Correct |
3 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
136 ms |
17328 KB |
Output is correct |
10 |
Correct |
3 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
129 ms |
18040 KB |
Output is correct |
13 |
Correct |
132 ms |
16120 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
145 ms |
16348 KB |
Output is correct |
16 |
Correct |
3 ms |
384 KB |
Output is correct |
17 |
Correct |
3 ms |
384 KB |
Output is correct |
18 |
Correct |
2 ms |
384 KB |
Output is correct |
19 |
Correct |
135 ms |
16220 KB |
Output is correct |
20 |
Correct |
3 ms |
1024 KB |
Output is correct |
21 |
Correct |
675 ms |
32300 KB |
Output is correct |
22 |
Correct |
4 ms |
1024 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
3 ms |
768 KB |
Output is correct |
25 |
Correct |
4 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
266 ms |
19192 KB |
Output is correct |
29 |
Correct |
197 ms |
17272 KB |
Output is correct |
30 |
Correct |
138 ms |
16504 KB |
Output is correct |
31 |
Correct |
229 ms |
22512 KB |
Output is correct |
32 |
Correct |
203 ms |
17784 KB |
Output is correct |
33 |
Correct |
2 ms |
384 KB |
Output is correct |
34 |
Correct |
186 ms |
15992 KB |
Output is correct |
35 |
Correct |
4 ms |
1024 KB |
Output is correct |
36 |
Correct |
2 ms |
384 KB |
Output is correct |
37 |
Correct |
4 ms |
384 KB |
Output is correct |
38 |
Correct |
259 ms |
18032 KB |
Output is correct |
39 |
Correct |
768 ms |
30396 KB |
Output is correct |
40 |
Correct |
2 ms |
384 KB |
Output is correct |
41 |
Correct |
3 ms |
1024 KB |
Output is correct |
42 |
Correct |
4 ms |
948 KB |
Output is correct |
43 |
Correct |
4 ms |
1024 KB |
Output is correct |
44 |
Correct |
138 ms |
17140 KB |
Output is correct |
45 |
Correct |
473 ms |
25148 KB |
Output is correct |
46 |
Correct |
2 ms |
384 KB |
Output is correct |