#include "rect.h"
#include <bits/stdc++.h>
#ifndef EVAL
#include "grader.cpp"
#endif
using std::cin;
using std::cout;
using std::endl;
using std::vector;
using std::string;
using ll = long long;
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
constexpr ll inf = (1ll<<60);
/*
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();
vii left(H,vi(W)), right(H,vi(W)), down(H,vi(W)), up(H,vi(W));
ll ans = 0;
{
rep(i,0,H){
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();
}
left[i][j] = idx[0];
max.emplace_front(a[i][j]);
idx.emplace_front(j);
}
}
}
{
rep(i,0,H){
std::deque<ll> max, idx;
max.pb(inf);
idx.pb(W);
per(j,W-1,0){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
right[i][j] = idx[0];
max.emplace_front(a[i][j]);
idx.emplace_front(j);
}
}
}
{
rep(j,0,W){
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();
}
up[i][j] = idx[0];
max.emplace_front(a[i][j]);
idx.emplace_front(i);
}
}
}
{
rep(j,0,W){
std::deque<ll> max, idx;
max.pb(inf);
idx.pb(H);
per(i,H-1,0){
while(max[0] < a[i][j]){
max.pop_front();
idx.pop_front();
}
down[i][j] = idx[0];
max.emplace_front(a[i][j]);
idx.emplace_front(i);
}
}
}
rep(j,1,W-1){
vi up_state(H,-inf);
vi down_state(H,inf);
rep(k,j,W-1){
rep(i,0,H){
up_state[i] = std::max(up_state[i], up[i][k]);
down_state[i] = std::min(down_state[i], down[i][k]);
}
binary_indexed_tree bit(H);
vii mem(H+1);
ll last = 0;
rep(l,1,H-1){
mem[down_state[l-1]].pb(l);
for(auto el: mem[l]) bit.add(el, -1);
bit.add(l,1);
if(right[l][j-1] <= k || left[l][k+1] >= j){
last = l;
}
else{
ll b = std::max(last, up_state[l+1]);
ans += bit.sum(l)-bit.sum(b);
}
}
}
}
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 |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 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 |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
4 ms |
596 KB |
Output is correct |
23 |
Correct |
4 ms |
596 KB |
Output is correct |
24 |
Correct |
6 ms |
560 KB |
Output is correct |
25 |
Correct |
13 ms |
468 KB |
Output is correct |
26 |
Correct |
13 ms |
592 KB |
Output is correct |
27 |
Correct |
13 ms |
596 KB |
Output is correct |
28 |
Correct |
15 ms |
584 KB |
Output is correct |
29 |
Correct |
4 ms |
432 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 |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
4 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
560 KB |
Output is correct |
20 |
Correct |
13 ms |
468 KB |
Output is correct |
21 |
Correct |
13 ms |
592 KB |
Output is correct |
22 |
Correct |
13 ms |
596 KB |
Output is correct |
23 |
Correct |
15 ms |
584 KB |
Output is correct |
24 |
Correct |
4 ms |
432 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
56 ms |
2036 KB |
Output is correct |
31 |
Correct |
52 ms |
2028 KB |
Output is correct |
32 |
Correct |
52 ms |
2004 KB |
Output is correct |
33 |
Correct |
198 ms |
1980 KB |
Output is correct |
34 |
Correct |
206 ms |
2036 KB |
Output is correct |
35 |
Correct |
208 ms |
2004 KB |
Output is correct |
36 |
Correct |
205 ms |
1992 KB |
Output is correct |
37 |
Correct |
211 ms |
2008 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 |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
4 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
560 KB |
Output is correct |
20 |
Correct |
13 ms |
468 KB |
Output is correct |
21 |
Correct |
13 ms |
592 KB |
Output is correct |
22 |
Correct |
13 ms |
596 KB |
Output is correct |
23 |
Correct |
15 ms |
584 KB |
Output is correct |
24 |
Correct |
4 ms |
432 KB |
Output is correct |
25 |
Correct |
56 ms |
2036 KB |
Output is correct |
26 |
Correct |
52 ms |
2028 KB |
Output is correct |
27 |
Correct |
52 ms |
2004 KB |
Output is correct |
28 |
Correct |
198 ms |
1980 KB |
Output is correct |
29 |
Correct |
206 ms |
2036 KB |
Output is correct |
30 |
Correct |
208 ms |
2004 KB |
Output is correct |
31 |
Correct |
205 ms |
1992 KB |
Output is correct |
32 |
Correct |
211 ms |
2008 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Execution timed out |
5014 ms |
19908 KB |
Time limit exceeded |
39 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
672 KB |
Output is correct |
2 |
Correct |
138 ms |
612 KB |
Output is correct |
3 |
Correct |
193 ms |
564 KB |
Output is correct |
4 |
Correct |
0 ms |
300 KB |
Output is correct |
5 |
Correct |
184 ms |
660 KB |
Output is correct |
6 |
Correct |
183 ms |
616 KB |
Output is correct |
7 |
Correct |
195 ms |
656 KB |
Output is correct |
8 |
Correct |
181 ms |
632 KB |
Output is correct |
9 |
Correct |
180 ms |
652 KB |
Output is correct |
10 |
Correct |
115 ms |
420 KB |
Output is correct |
11 |
Correct |
133 ms |
540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Execution timed out |
5066 ms |
112892 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
300 KB |
Output is correct |
5 |
Correct |
1 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
4 ms |
596 KB |
Output is correct |
18 |
Correct |
4 ms |
596 KB |
Output is correct |
19 |
Correct |
6 ms |
560 KB |
Output is correct |
20 |
Correct |
13 ms |
468 KB |
Output is correct |
21 |
Correct |
13 ms |
592 KB |
Output is correct |
22 |
Correct |
13 ms |
596 KB |
Output is correct |
23 |
Correct |
15 ms |
584 KB |
Output is correct |
24 |
Correct |
4 ms |
432 KB |
Output is correct |
25 |
Correct |
56 ms |
2036 KB |
Output is correct |
26 |
Correct |
52 ms |
2028 KB |
Output is correct |
27 |
Correct |
52 ms |
2004 KB |
Output is correct |
28 |
Correct |
198 ms |
1980 KB |
Output is correct |
29 |
Correct |
206 ms |
2036 KB |
Output is correct |
30 |
Correct |
208 ms |
2004 KB |
Output is correct |
31 |
Correct |
205 ms |
1992 KB |
Output is correct |
32 |
Correct |
211 ms |
2008 KB |
Output is correct |
33 |
Execution timed out |
5014 ms |
19908 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |