#include<bits/stdc++.h>
using namespace std;
int dx[]={0,-1,0,1},
dy[]={-1,0,1,0};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int n,m;cin >> n >> m;
vector<vector<int>> a;
if(n<m){
a.assign(n,vector<int>(m));
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[i][j];
}
else{
a.assign(m,vector<int>(n));
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin >> a[j][i];
swap(n,m);
}
vector<int> com;
for(int i=0;i<n;i++) for(int j=0;j<m;j++) com.push_back(a[i][j]);
sort(com.begin(),com.end());
for(int i=0;i<n;i++) for(int j=0;j<m;j++) a[i][j]=lower_bound(com.begin(),com.end(),a[i][j])-com.begin()+1;
vector<vector<vector<int>>> p(n+1,vector<vector<int>>(m+1,vector<int>(16,0)));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) for(int k=0;k<16;k++){
vector<int> v;
for(int t=0;t<4;t++) if(k>>t&1){
int xt=i+dx[t],yt=j+dy[t];
if(xt<=0 || yt<=0 || xt>n || yt>m) continue;
v.push_back(a[xt-1][yt-1]);
}
sort(v.begin(),v.end());
int lt=0,rt=n*m+1;
for(int x:v){
if(x<a[i-1][j-1]) lt=x;
else rt=a[i-1][j-1];
}
p[i][j][k]=rt-lt+p[i-1][j][k]+p[i][j-1][k]-p[i-1][j-1][k];
}
auto f = [&](int x,int y,int u,int v,int k){
return p[u][v][k]-p[x-1][v][k]-p[u][y-1][k]+p[x-1][y-1][k];
};
long long total=n*m;
for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++){
vector<int> lt(m+1),mt(m+1),rt(m+1);
for(int k=1;k<=m;k++){
lt[k]=f(i,k,i,k,12)+f(j,k,j,k,6)+f(i+1,k,j-1,k,14);
rt[k]=f(i,k,i,k,9)+f(j,k,j,k,3)+f(i+1,k,j-1,k,11);
mt[k]=f(i,k,i,k,13)+f(j,k,j,k,7)+f(i+1,k,j-1,k,15);
mt[k]+=mt[k-1];
}
map<int,int> mp;
for(int k=1;k<=m;k++){
total+=mp[n*m+1-(rt[k]+mt[k-1])];
mp[lt[k]-mt[k]]++;
}
}
for(int i=0;i<n;i++){
int t=1;
for(int j=1;j<m;j++){
while((a[i][t]>a[i][t-1])!=(a[i][j]>a[i][j-1])) t++;
total+=j-t+1;
}
}
for(int i=0;i<m;i++){
int t=1;
for(int j=1;j<n;j++){
while((a[t][i]>a[t-1][i])!=(a[j][i]>a[j-1][i])) t++;
total+=j-t+1;
}
}
cout << total << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
36 ms |
16276 KB |
Output is correct |
3 |
Correct |
41 ms |
16028 KB |
Output is correct |
4 |
Correct |
36 ms |
16396 KB |
Output is correct |
5 |
Correct |
41 ms |
16280 KB |
Output is correct |
6 |
Correct |
40 ms |
16276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
648 KB |
Output is correct |
10 |
Correct |
2 ms |
612 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
656 KB |
Output is correct |
16 |
Correct |
6 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
648 KB |
Output is correct |
10 |
Correct |
2 ms |
612 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
656 KB |
Output is correct |
16 |
Correct |
6 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
2648 KB |
Output is correct |
18 |
Correct |
22 ms |
1116 KB |
Output is correct |
19 |
Correct |
13 ms |
1372 KB |
Output is correct |
20 |
Correct |
10 ms |
1116 KB |
Output is correct |
21 |
Correct |
20 ms |
1116 KB |
Output is correct |
22 |
Correct |
30 ms |
1304 KB |
Output is correct |
23 |
Correct |
29 ms |
1116 KB |
Output is correct |
24 |
Correct |
30 ms |
1036 KB |
Output is correct |
25 |
Correct |
36 ms |
1116 KB |
Output is correct |
26 |
Correct |
44 ms |
1304 KB |
Output is correct |
27 |
Correct |
35 ms |
1116 KB |
Output is correct |
28 |
Correct |
57 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
352 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
4 ms |
648 KB |
Output is correct |
10 |
Correct |
2 ms |
612 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
4 ms |
492 KB |
Output is correct |
14 |
Correct |
3 ms |
344 KB |
Output is correct |
15 |
Correct |
5 ms |
656 KB |
Output is correct |
16 |
Correct |
6 ms |
604 KB |
Output is correct |
17 |
Correct |
5 ms |
2648 KB |
Output is correct |
18 |
Correct |
22 ms |
1116 KB |
Output is correct |
19 |
Correct |
13 ms |
1372 KB |
Output is correct |
20 |
Correct |
10 ms |
1116 KB |
Output is correct |
21 |
Correct |
20 ms |
1116 KB |
Output is correct |
22 |
Correct |
30 ms |
1304 KB |
Output is correct |
23 |
Correct |
29 ms |
1116 KB |
Output is correct |
24 |
Correct |
30 ms |
1036 KB |
Output is correct |
25 |
Correct |
36 ms |
1116 KB |
Output is correct |
26 |
Correct |
44 ms |
1304 KB |
Output is correct |
27 |
Correct |
35 ms |
1116 KB |
Output is correct |
28 |
Correct |
57 ms |
1372 KB |
Output is correct |
29 |
Correct |
66 ms |
16268 KB |
Output is correct |
30 |
Correct |
266 ms |
6296 KB |
Output is correct |
31 |
Correct |
592 ms |
6372 KB |
Output is correct |
32 |
Correct |
42 ms |
11288 KB |
Output is correct |
33 |
Correct |
111 ms |
6356 KB |
Output is correct |
34 |
Correct |
344 ms |
6272 KB |
Output is correct |
35 |
Correct |
338 ms |
4328 KB |
Output is correct |
36 |
Correct |
566 ms |
6100 KB |
Output is correct |
37 |
Correct |
655 ms |
6104 KB |
Output is correct |
38 |
Correct |
717 ms |
6104 KB |
Output is correct |
39 |
Correct |
601 ms |
6184 KB |
Output is correct |
40 |
Correct |
644 ms |
6224 KB |
Output is correct |
41 |
Correct |
639 ms |
6100 KB |
Output is correct |
42 |
Correct |
609 ms |
6124 KB |
Output is correct |
43 |
Correct |
633 ms |
6204 KB |
Output is correct |
44 |
Correct |
600 ms |
6316 KB |
Output is correct |