#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int, int>
#define f first
#define s second
#define REP(i,n) for (int i = 0; i<n; ++i)
#define REP1(i,n) for (int i = 1; i<=n; ++i)
#define SZ(x) (int)((x).size())
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<" "<<#__VA_ARGS__<<" - ", _do(__VA_ARGS__)
template <typename T> void _do( T && x) {cerr<<x<<endl;}
template <typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
//const int maxn = 7000*7000+5;
vector<vector<int> > G;
vector<int> legal[50005];
void upd(int a, int b) {
if (a < b) {
legal[a].pb(b);
}
}
bool ok(int a, int b) {
return find(ALL(legal[a]), b) != legal[a].end();
}
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int n,m; cin>>n>>m;
G.resize(n, vector<int> (m));
vector<int> ol;
REP(i,n) REP(j,m) {
cin>>G[i][j];
ol.pb(G[i][j]);
}
sort(ALL(ol));
REP(i,n) REP(j,m) {
G[i][j] = lower_bound(ALL(ol), G[i][j]) - ol.begin();
}
if (n == 1) {
vector<int> df;
REP(j,m-1) {
df.pb(G[0][j] > G[0][j+1]);
}
ll re = 0;
int cnt = 0;
REP(i, SZ(df) + 1) {
if (i) if (i == SZ(df) || df[i] != df[i-1]) {
++cnt;
re += cnt * (cnt+1) / 2 - cnt;
bug(cnt, cnt * (cnt+1) / 2 - cnt);
cnt = 0;
}
++cnt;
}
bug(re);
cout<<re +m <<endl; return 0;
}
REP(i,n) REP(j,m) {
if (i) upd(G[i][j], G[i-1][j]);
if (i<n-1) upd(G[i][j], G[i+1][j]);
if (j<m-1) upd(G[i][j], G[i][j+1]);
if (j) upd(G[i][j], G[i][j-1]);
}
int re =0 ;
REP(h2, n) {
REP(h1, h2+1) {
// using only rows from h1 to h2
// play ball
for( int w1 = 0; w1 < m; ++w1) {
// for subtask... just enumerate w1, w2...
vector<int> tmp;
for( int w2 = w1; w2 < m; ++w2) {
for (int k = h1; k<=h2; ++k) {
tmp.pb(G[k][w2]);
}
sort(ALL(tmp));
REP(i, SZ(tmp)-1) {
if (!ok(tmp[i], tmp[i+1])) {
goto nope;
}
}
re++;
nope:;
}
}
}
}
cout<<re<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1484 KB |
Output is correct |
2 |
Correct |
10 ms |
3396 KB |
Output is correct |
3 |
Correct |
14 ms |
3396 KB |
Output is correct |
4 |
Correct |
11 ms |
3524 KB |
Output is correct |
5 |
Correct |
12 ms |
3524 KB |
Output is correct |
6 |
Correct |
15 ms |
3536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
2 ms |
1484 KB |
Output is correct |
4 |
Correct |
2 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
2 ms |
1484 KB |
Output is correct |
4 |
Correct |
2 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
7 |
Execution timed out |
5062 ms |
1644 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
2 ms |
1484 KB |
Output is correct |
4 |
Correct |
2 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
7 |
Execution timed out |
5062 ms |
1644 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
1612 KB |
Output is correct |
2 |
Correct |
2 ms |
1484 KB |
Output is correct |
3 |
Correct |
2 ms |
1484 KB |
Output is correct |
4 |
Correct |
2 ms |
1484 KB |
Output is correct |
5 |
Correct |
2 ms |
1488 KB |
Output is correct |
6 |
Correct |
2 ms |
1484 KB |
Output is correct |
7 |
Execution timed out |
5062 ms |
1644 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |