#include <bits/stdc++.h>
#include <bits/extc++.h>
#define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define iter(a) a.begin(), a.end()
#define riter(a) a.rbegin(), a.rend()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(riter(a))
#define pb(a) push_back(a)
#define eb(a) emplace_back(a)
#define pf(a) push_front(a)
#define ef(a) emplace_front(a)
#define pob pop_back()
#define pof pop_front()
#define mp(a, b) make_pair(a, b)
#define F first
#define S second
#define mt make_tuple
#define gt(t, i) get<i>(t)
#define tomax(a, b) ((a) = max((a), (b)))
#define tomin(a, b) ((a) = min((a), (b)))
#define topos(a) ((a) = (((a) % MOD + MOD) % MOD))
#define uni(a) a.resize(unique(iter(a)) - a.begin())
#define printv(a, b) {bool pvaspace=false; \
for(auto pva : a){ \
if(pvaspace) b << " "; pvaspace=true;\
b << pva;\
}\
b << "\n";}
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;
using tiii = tuple<int, int, int>;
const ll MOD = 1000000007;
const ll MAX = 10000001;
template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
return o << '(' << p.F << ',' << p.S << ')';
}
ll ifloor(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a < 0) return (a - b + 1) / b;
else return a / b;
}
ll iceil(ll a, ll b){
if(b < 0) a *= -1, b *= -1;
if(a > 0) return (a + b - 1) / b;
else return a / b;
}
vector<pii> dir = {mp(-1, 0), mp(0, 1), mp(1, 0), mp(0, -1)};
struct sum2d{
vector<vector<ll>> a;
void init(vector<vector<ll>>& t){
a = t;
int n = a.size(), m = a[0].size();
n--; m--;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) a[i][j] += a[i][j - 1];
}
for(int j = 1; j <= m; j++){
for(int i = 1; i <= n; i++) a[i][j] += a[i - 1][j];
}
}
ll query(int x1, int x2, int y1, int y2){
if(x1 > x2 || y1 > y2) return 0;
return a[x2][y2] - a[x1 - 1][y2] - a[x2][y1 - 1] + a[x1 - 1][y1 - 1];
}
};
int n, m;
vector<sum2d> diff;
vector<vector<int>> a;
void build_diff(int msk){
vector<vector<ll>> t(n + 1, vector<ll>(m + 1));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
ll nxt = 0;
bool sp = true;
for(int k = 0; k < 4; k++){
if(!(1 << k & msk)) continue;
int nx = i + dir[k].F, ny = j + dir[k].S;
if(a[nx][ny] > a[i][j]) sp = false;
else nxt = max(nxt, (ll)a[nx][ny]);
}
nxt = a[i][j] - nxt;
if(sp) nxt += MAX - a[i][j];
t[i][j] = nxt;
}
}
diff[msk].init(t);
}
ll ans = 0;
ll colleft(int u, int d, int i){
if(u == d){
return diff[0b0100].query(u, d, i, i);
}
ll ans = diff[0b0110].query(u, u, i, i) + diff[0b1100].query(d, d, i, i);
ans += diff[0b1110].query(u + 1, d - 1, i, i);
return ans;
}
ll colmid(int u, int d, int i){
if(u == d){
return diff[0b0101].query(u, d, i, i);
}
ll ans = diff[0b0111].query(u, u, i, i) + diff[0b1101].query(d, d, i, i);
ans += diff[0b1111].query(u + 1, d - 1, i, i);
return ans;
}
ll colright(int u, int d, int i){
if(u == d){
return diff[0b0001].query(u, d, i, i);
}
ll ans = diff[0b0011].query(u, u, i, i) + diff[0b1001].query(d, d, i, i);
ans += diff[0b1011].query(u + 1, d - 1, i, i);
return ans;
}
ll coltwo(int u, int d, int i){
if(u == d){
return diff[0b0000].query(u, d, i, i);
}
ll ans = diff[0b0010].query(u, u, i, i) + diff[0b1000].query(d, d, i, i);
ans += diff[0b1010].query(u + 1, d - 1, i, i);
return ans;
}
void solve(int u, int d){
map<ll, ll> tmp;
ll pre = 0;
//cerr << "solve " << u << " " << d << "\n";
for(int i = 1; i <= m; i++){
if(coltwo(u, d, i) == MAX){
//cerr << "two " << i << "\n";
ans++;
}
if(tmp.find(MAX - pre - colright(u, d, i)) != tmp.end()){
//cerr << "ok " << i << "\n";
ans += tmp[MAX - pre - colright(u, d, i)];
}
//cerr << "test " << i << " " << pre + colright(u, d, i) << " " << MAX - pre - colright(u, d, i) << " ";
pre += colmid(u, d, i);
tmp[- pre + colleft(u, d, i)]++;
//cerr << -pre + colleft(u, d, i) << "\n";
}
}
int main(){
StarBurstStream
reverse(iter(dir));
cin >> n >> m;
a.resize(n + 2, vector<int>(m + 2));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
}
}
if(n > m){
vector<vector<int>> ta(m + 2, vector<int>(n + 2));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++) ta[j][i] = a[i][j];
}
a = ta;
swap(n, m);
}
diff.resize(16);
for(int i = 0; i < 16; i++) build_diff(i);
/*for(int i = 1; i <= m; i++){
cerr << "diff " << i << " ";
for(int j = 0; j < 16; j++){
cerr << diff[j].query(1, 1, i, i) << " ";
}
cerr<< "\n";
}*/
for(int i = 1; i <= n; i++){
for(int j = i; j <= n; j++) solve(i, j);
}
cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
22 ms |
14900 KB |
Output is correct |
3 |
Correct |
37 ms |
16624 KB |
Output is correct |
4 |
Correct |
23 ms |
14948 KB |
Output is correct |
5 |
Correct |
28 ms |
14976 KB |
Output is correct |
6 |
Correct |
49 ms |
15856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
452 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
4 ms |
460 KB |
Output is correct |
16 |
Correct |
5 ms |
444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
452 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
4 ms |
460 KB |
Output is correct |
16 |
Correct |
5 ms |
444 KB |
Output is correct |
17 |
Correct |
5 ms |
2488 KB |
Output is correct |
18 |
Correct |
33 ms |
1356 KB |
Output is correct |
19 |
Correct |
10 ms |
1356 KB |
Output is correct |
20 |
Correct |
12 ms |
1356 KB |
Output is correct |
21 |
Correct |
29 ms |
1368 KB |
Output is correct |
22 |
Correct |
44 ms |
1228 KB |
Output is correct |
23 |
Correct |
31 ms |
1344 KB |
Output is correct |
24 |
Correct |
25 ms |
1244 KB |
Output is correct |
25 |
Correct |
35 ms |
1368 KB |
Output is correct |
26 |
Correct |
33 ms |
1368 KB |
Output is correct |
27 |
Correct |
38 ms |
1356 KB |
Output is correct |
28 |
Correct |
32 ms |
1336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
4 ms |
452 KB |
Output is correct |
10 |
Correct |
2 ms |
460 KB |
Output is correct |
11 |
Correct |
2 ms |
588 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
13 |
Correct |
4 ms |
460 KB |
Output is correct |
14 |
Correct |
3 ms |
460 KB |
Output is correct |
15 |
Correct |
4 ms |
460 KB |
Output is correct |
16 |
Correct |
5 ms |
444 KB |
Output is correct |
17 |
Correct |
5 ms |
2488 KB |
Output is correct |
18 |
Correct |
33 ms |
1356 KB |
Output is correct |
19 |
Correct |
10 ms |
1356 KB |
Output is correct |
20 |
Correct |
12 ms |
1356 KB |
Output is correct |
21 |
Correct |
29 ms |
1368 KB |
Output is correct |
22 |
Correct |
44 ms |
1228 KB |
Output is correct |
23 |
Correct |
31 ms |
1344 KB |
Output is correct |
24 |
Correct |
25 ms |
1244 KB |
Output is correct |
25 |
Correct |
35 ms |
1368 KB |
Output is correct |
26 |
Correct |
33 ms |
1368 KB |
Output is correct |
27 |
Correct |
38 ms |
1356 KB |
Output is correct |
28 |
Correct |
32 ms |
1336 KB |
Output is correct |
29 |
Correct |
27 ms |
15736 KB |
Output is correct |
30 |
Correct |
191 ms |
7716 KB |
Output is correct |
31 |
Correct |
589 ms |
7340 KB |
Output is correct |
32 |
Correct |
20 ms |
11060 KB |
Output is correct |
33 |
Correct |
217 ms |
7712 KB |
Output is correct |
34 |
Correct |
600 ms |
7688 KB |
Output is correct |
35 |
Correct |
291 ms |
4912 KB |
Output is correct |
36 |
Correct |
433 ms |
7344 KB |
Output is correct |
37 |
Correct |
652 ms |
7348 KB |
Output is correct |
38 |
Correct |
679 ms |
7488 KB |
Output is correct |
39 |
Correct |
669 ms |
7608 KB |
Output is correct |
40 |
Correct |
703 ms |
7748 KB |
Output is correct |
41 |
Correct |
690 ms |
7560 KB |
Output is correct |
42 |
Correct |
663 ms |
7480 KB |
Output is correct |
43 |
Correct |
675 ms |
7296 KB |
Output is correct |
44 |
Correct |
739 ms |
7308 KB |
Output is correct |