#include <bits/stdc++.h>
using namespace std;
#define int long long
#define sp << ' ' <<
#define nl << '\n'
signed main(){
cin.tie(0)->sync_with_stdio(0);
int n, m; cin >> n >> m; --m;
int a[n][n][n][n];
for(int i=0; i<n; ++i)
for(int j=0; j<n; ++j)
for(int k=0; k<n; ++k)
for(int &l : a[i][j][k]) cin >> l;
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
for(int k=0; k<n; ++k){
deque<int> q;
for(int l=0; l<n; ++l){
if(!q.empty() && q.front() < l-m) q.pop_front();
while(!q.empty() && a[i][j][k][q.back()] >= a[i][j][k][l]) q.pop_back();
q.push_back(l);
if(l >= m) a[i][j][k][l-m] = a[i][j][k][q.front()];
}
}
for(int l=0; l<n-m; ++l){
deque<int> q;
for(int k=0; k<n; ++k){
if(!q.empty() && q.front() < k-m) q.pop_front();
while(!q.empty() && a[i][j][q.back()][l] >= a[i][j][k][l]) q.pop_back();
q.push_back(k);
if(k >= m) a[i][j][k-m][l] = a[i][j][q.front()][l];
}
}
}
}
for(int k=0; k<n-m; ++k){
for(int l=0; l<n-m; ++l){
for(int i=0; i<n; ++i){
deque<int> q;
for(int j=0; j<n; ++j){
if(!q.empty() && q.front() < j-m) q.pop_front();
while(!q.empty() && a[i][q.back()][k][l] >= a[i][j][k][l]) q.pop_back();
q.push_back(j);
if(j >= m) a[i][j-m][k][l] = a[i][q.front()][k][l];
}
}
for(int j=0; j<n-m; ++j){
deque<int> q;
for(int i=0; i<n; ++i){
if(!q.empty() && q.front() < i-m) q.pop_front();
while(!q.empty() && a[q.back()][j][k][l] >= a[i][j][k][l]) q.pop_back();
q.push_back(i);
if(i >= m) a[i-m][j][k][l] = a[q.front()][j][k][l];
}
}
}
}
for(int i=0; i<n-m; ++i)
for(int j=0; j<n-m; ++j)
for(int k=0; k<n-m; ++k)
for(int l=0; l<n-m; ++l)
cout << a[i][j][k][l] << ' ';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
2 ms |
452 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
5 |
Correct |
3 ms |
456 KB |
Output is correct |
6 |
Correct |
12 ms |
1360 KB |
Output is correct |
7 |
Correct |
12 ms |
1204 KB |
Output is correct |
8 |
Correct |
26 ms |
3076 KB |
Output is correct |
9 |
Correct |
50 ms |
4740 KB |
Output is correct |
10 |
Correct |
28 ms |
3192 KB |
Output is correct |
11 |
Correct |
81 ms |
7952 KB |
Output is correct |
12 |
Correct |
155 ms |
15964 KB |
Output is correct |
13 |
Correct |
126 ms |
14804 KB |
Output is correct |
14 |
Correct |
230 ms |
20688 KB |
Output is correct |
15 |
Correct |
348 ms |
33232 KB |
Output is correct |
16 |
Correct |
187 ms |
21576 KB |
Output is correct |
17 |
Correct |
238 ms |
23072 KB |
Output is correct |
18 |
Correct |
495 ms |
40376 KB |
Output is correct |
19 |
Correct |
287 ms |
29444 KB |
Output is correct |
20 |
Correct |
243 ms |
27332 KB |
Output is correct |