#include "rect.h"
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using ll = int;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll,ll>;
#define rep(i,j,k) for(ll i=ll(j); i<ll(k); i++)
#define REP(i,j,k) for(ll i=ll(j); i<=ll(k); i++)
#define per(i,j,k) for(ll i=ll(j); i>=ll(k); i--)
#define all(a) a.begin(),a.end()
#define pb emplace_back
#define mp std::make_pair
#define mtp std::make_tuple
#define ln "\n"
constexpr ll inf = (1ll<<30);
/*
constexpr ll LOG = 12;
ll left_table[2510][2510][LOG+1];
ll right_table[2510][2510][LOG+1];
ll up_table[2510][2510][LOG+1];
ll down_table[2510][2510][LOG+1];*/
struct binary_indexed_tree{
vi node;
ll N;
binary_indexed_tree(int n): N(n){
node.resize(N+1);
}
void add(ll idx, ll val){
idx++;
for(; idx<=N; idx+=(idx&-idx)) node[idx] += val;
}
ll sum(ll idx){
idx++;
ll ret = 0;
for(; 0<idx; idx-=(idx&-idx)) ret += node[idx];
return ret;
}
};
long long count_rectangles(std::vector<std::vector<int> > a) {
ll H = a.size(), W = a[0].size();
long long ans = 0;
vector<vector<vector<pii>>> query(H,vector<vector<pii>>(W));
{
vii dp_right(W,vi(W));
vii right(W,vi(W));
vii rem(W);
vii new_rem(W);
per(i,H-1,0){
std::deque<ll> max, idx;
max.pb(inf);
idx.pb(-1);
rep(j,0,W){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
if(idx[0] != -1){
right[idx[0]][j] = 1;
new_rem[idx[0]].pb(j);
}
max.emplace_front(a[i][j]);
idx.emplace_front(j);
}
max.clear();
idx.clear();
max.pb(inf);
idx.pb(W);
per(j,W-1,0){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
if(idx[0] != W && a[i][j] != max[0]){
right[j][idx[0]] = 1;
new_rem[j].pb(idx[0]);
}
max.emplace_front(a[i][j]);
idx.emplace_front(j);
}
rep(j,0,W){
if(j+1 < W) right[j][j+1] = 0;
for(auto el: new_rem[j]){
if(el == j+1) continue;
right[j][el] += dp_right[j][el];
}
}
rep(j,1,W-1){
for(auto el: new_rem[j-1]){
if(el == j) continue;
ll bottom = i+right[j-1][el];
// bottom 以下 かつ
ll lasting = el-j;
// lasting 以上
query[i][j].pb(mp(lasting, -bottom));
}
}
rep(j,0,W){
for(auto el: rem[j]) dp_right[j][el] = 0;
for(auto el: new_rem[j]){
dp_right[j][el] = right[j][el];
right[j][el] = 0;
}
rem[j] = new_rem[j];
new_rem[j].clear();
}
}
}
{
vii dp_down(H,vi(H));
vii down(H,vi(H));
vii rem(H);
vii new_rem(H);
per(j,W-1,0){
std::deque<ll> max, idx;
max.pb(inf);
idx.pb(-1);
rep(i,0,H){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
if(idx[0] != -1){
down[idx[0]][i] = 1;
new_rem[idx[0]].pb(i);
}
max.emplace_front(a[i][j]);
idx.emplace_front(i);
}
max.clear();
idx.clear();
max.pb(inf);
idx.pb(H);
per(i,H-1,0){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
if(idx[0] != H && a[i][j] != max[0]){
down[i][idx[0]] = 1;
new_rem[i].pb(idx[0]);
}
max.emplace_front(a[i][j]);
idx.emplace_front(i);
}
rep(i,0,H){
if(i+1 < H) down[i][i+1] = 0;
for(auto el: new_rem[i]){
if(el == i+1) continue;
down[i][el] += dp_down[i][el];
}
}
rep(i,1,H-1){
for(auto el: new_rem[i-1]){
if(el == i) continue;
query[i][j].pb(mp(down[i-1][el], el));
}
}
rep(i,0,H){
for(auto el: rem[i]) dp_down[i][el] = 0;
for(auto el: new_rem[i]){
dp_down[i][el] = down[i][el];
down[i][el] = 0;
}
rem[i] = new_rem[i];
new_rem[i].clear();
}
}
}
binary_indexed_tree bit(H+10);
rep(i,1,H-1){
rep(j,1,W-1){
auto query_ = query[i][j];
sort(all(query_));
reverse(all(query_));
for(auto el: query_){
if(el.second < 0) ans += bit.sum(-el.second);
else bit.add(el.second,1);
}
for(auto el: query_){
if(el.second > 0) bit.add(el.second,-1);
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
2 ms |
724 KB |
Output is correct |
23 |
Correct |
2 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
2 ms |
596 KB |
Output is correct |
26 |
Correct |
2 ms |
724 KB |
Output is correct |
27 |
Correct |
3 ms |
724 KB |
Output is correct |
28 |
Correct |
3 ms |
724 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
9 ms |
3156 KB |
Output is correct |
31 |
Correct |
11 ms |
3156 KB |
Output is correct |
32 |
Correct |
10 ms |
3156 KB |
Output is correct |
33 |
Correct |
9 ms |
2516 KB |
Output is correct |
34 |
Correct |
14 ms |
3028 KB |
Output is correct |
35 |
Correct |
15 ms |
3156 KB |
Output is correct |
36 |
Correct |
14 ms |
3028 KB |
Output is correct |
37 |
Correct |
19 ms |
3028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
9 ms |
3156 KB |
Output is correct |
26 |
Correct |
11 ms |
3156 KB |
Output is correct |
27 |
Correct |
10 ms |
3156 KB |
Output is correct |
28 |
Correct |
9 ms |
2516 KB |
Output is correct |
29 |
Correct |
14 ms |
3028 KB |
Output is correct |
30 |
Correct |
15 ms |
3156 KB |
Output is correct |
31 |
Correct |
14 ms |
3028 KB |
Output is correct |
32 |
Correct |
19 ms |
3028 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
340 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
121 ms |
34948 KB |
Output is correct |
39 |
Correct |
118 ms |
30248 KB |
Output is correct |
40 |
Correct |
107 ms |
32600 KB |
Output is correct |
41 |
Correct |
95 ms |
28740 KB |
Output is correct |
42 |
Correct |
181 ms |
35000 KB |
Output is correct |
43 |
Correct |
177 ms |
34960 KB |
Output is correct |
44 |
Correct |
174 ms |
35020 KB |
Output is correct |
45 |
Correct |
163 ms |
32984 KB |
Output is correct |
46 |
Correct |
126 ms |
25400 KB |
Output is correct |
47 |
Correct |
138 ms |
27500 KB |
Output is correct |
48 |
Correct |
208 ms |
34892 KB |
Output is correct |
49 |
Correct |
216 ms |
35080 KB |
Output is correct |
50 |
Correct |
112 ms |
19712 KB |
Output is correct |
51 |
Correct |
109 ms |
16640 KB |
Output is correct |
52 |
Correct |
211 ms |
33756 KB |
Output is correct |
53 |
Correct |
205 ms |
33848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
50260 KB |
Output is correct |
2 |
Correct |
15 ms |
36308 KB |
Output is correct |
3 |
Correct |
20 ms |
49876 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
22 ms |
49984 KB |
Output is correct |
6 |
Correct |
21 ms |
49956 KB |
Output is correct |
7 |
Correct |
22 ms |
50032 KB |
Output is correct |
8 |
Correct |
23 ms |
50004 KB |
Output is correct |
9 |
Correct |
23 ms |
50004 KB |
Output is correct |
10 |
Correct |
20 ms |
49752 KB |
Output is correct |
11 |
Correct |
23 ms |
49864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
809 ms |
155064 KB |
Output is correct |
8 |
Correct |
1852 ms |
319572 KB |
Output is correct |
9 |
Correct |
1824 ms |
321156 KB |
Output is correct |
10 |
Correct |
1826 ms |
321160 KB |
Output is correct |
11 |
Correct |
470 ms |
149404 KB |
Output is correct |
12 |
Correct |
916 ms |
235396 KB |
Output is correct |
13 |
Correct |
974 ms |
247432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
288 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
2 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
9 ms |
3156 KB |
Output is correct |
26 |
Correct |
11 ms |
3156 KB |
Output is correct |
27 |
Correct |
10 ms |
3156 KB |
Output is correct |
28 |
Correct |
9 ms |
2516 KB |
Output is correct |
29 |
Correct |
14 ms |
3028 KB |
Output is correct |
30 |
Correct |
15 ms |
3156 KB |
Output is correct |
31 |
Correct |
14 ms |
3028 KB |
Output is correct |
32 |
Correct |
19 ms |
3028 KB |
Output is correct |
33 |
Correct |
121 ms |
34948 KB |
Output is correct |
34 |
Correct |
118 ms |
30248 KB |
Output is correct |
35 |
Correct |
107 ms |
32600 KB |
Output is correct |
36 |
Correct |
95 ms |
28740 KB |
Output is correct |
37 |
Correct |
181 ms |
35000 KB |
Output is correct |
38 |
Correct |
177 ms |
34960 KB |
Output is correct |
39 |
Correct |
174 ms |
35020 KB |
Output is correct |
40 |
Correct |
163 ms |
32984 KB |
Output is correct |
41 |
Correct |
126 ms |
25400 KB |
Output is correct |
42 |
Correct |
138 ms |
27500 KB |
Output is correct |
43 |
Correct |
208 ms |
34892 KB |
Output is correct |
44 |
Correct |
216 ms |
35080 KB |
Output is correct |
45 |
Correct |
112 ms |
19712 KB |
Output is correct |
46 |
Correct |
109 ms |
16640 KB |
Output is correct |
47 |
Correct |
211 ms |
33756 KB |
Output is correct |
48 |
Correct |
205 ms |
33848 KB |
Output is correct |
49 |
Correct |
24 ms |
50260 KB |
Output is correct |
50 |
Correct |
15 ms |
36308 KB |
Output is correct |
51 |
Correct |
20 ms |
49876 KB |
Output is correct |
52 |
Correct |
0 ms |
212 KB |
Output is correct |
53 |
Correct |
22 ms |
49984 KB |
Output is correct |
54 |
Correct |
21 ms |
49956 KB |
Output is correct |
55 |
Correct |
22 ms |
50032 KB |
Output is correct |
56 |
Correct |
23 ms |
50004 KB |
Output is correct |
57 |
Correct |
23 ms |
50004 KB |
Output is correct |
58 |
Correct |
20 ms |
49752 KB |
Output is correct |
59 |
Correct |
23 ms |
49864 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
809 ms |
155064 KB |
Output is correct |
62 |
Correct |
1852 ms |
319572 KB |
Output is correct |
63 |
Correct |
1824 ms |
321156 KB |
Output is correct |
64 |
Correct |
1826 ms |
321160 KB |
Output is correct |
65 |
Correct |
470 ms |
149404 KB |
Output is correct |
66 |
Correct |
916 ms |
235396 KB |
Output is correct |
67 |
Correct |
974 ms |
247432 KB |
Output is correct |
68 |
Correct |
1 ms |
212 KB |
Output is correct |
69 |
Correct |
0 ms |
212 KB |
Output is correct |
70 |
Correct |
0 ms |
340 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
0 ms |
212 KB |
Output is correct |
73 |
Correct |
2397 ms |
444064 KB |
Output is correct |
74 |
Correct |
2434 ms |
376320 KB |
Output is correct |
75 |
Correct |
2079 ms |
404408 KB |
Output is correct |
76 |
Correct |
1870 ms |
349864 KB |
Output is correct |
77 |
Correct |
3071 ms |
442772 KB |
Output is correct |
78 |
Correct |
2028 ms |
267440 KB |
Output is correct |
79 |
Correct |
2063 ms |
321068 KB |
Output is correct |
80 |
Correct |
3676 ms |
458712 KB |
Output is correct |
81 |
Correct |
2101 ms |
263996 KB |
Output is correct |
82 |
Correct |
3053 ms |
378128 KB |
Output is correct |
83 |
Correct |
3619 ms |
461140 KB |
Output is correct |
84 |
Correct |
1951 ms |
267144 KB |
Output is correct |
85 |
Correct |
3357 ms |
446924 KB |
Output is correct |
86 |
Correct |
3305 ms |
432740 KB |
Output is correct |
87 |
Correct |
1651 ms |
293752 KB |
Output is correct |
88 |
Correct |
3020 ms |
457676 KB |
Output is correct |
89 |
Correct |
3022 ms |
456908 KB |
Output is correct |
90 |
Correct |
3101 ms |
456832 KB |
Output is correct |