#include "rect.h"
#include <bits/stdc++.h>
using namespace std;
#define ss short
#define ll long long
struct range {
ss r, mx;
};
bool cmp0(range &a, range &b) {return a.r > b.r;}
bool cmp1(range &a, range &b) {return a.mx > b.mx;}
const int maxn = 2505, INF = 2505;
int n, m;
vector<vector<vector<pair<ss,ss>>>> pos[2];
vector<vector<vector<range>>> start[2];
int fen[maxn];
void update(int id, int val) {
while (id<maxn) {
fen[id] += val;
id += (id & -id);
}
}
int qry(int id) {
int val = 0;
while (id >= 1) {
val += fen[id];
id -= (id & -id);
}
return val;
}
long long count_rectangles(vector<vector<int32_t>> a) {
n = a.size(), m = a[0].size();
pos[0].resize(m);
for (int i=0;i<m;i++) pos[0][i].resize(m - i);
pos[1].resize(n);
for (int i=0;i<n;i++) pos[1][i].resize(n - i);
start[0].resize(n); start[1].resize(n);
for (int i=0;i<n;i++) start[0][i].resize(m), start[1][i].resize(m);
vector<pair<int,int>> cur;
vector<int> temp;
set<int> s;
for (int i=0;i<n;i++) {
cur.clear();
for (int j=0;j<m;j++) cur.push_back({a[i][j], j});
sort(cur.rbegin(), cur.rend());
s.clear(); s.insert(-1); s.insert(INF);
temp.clear();
for (int j=0;j<m;j++) {
auto [val, id] = cur[j];
if (j>0 && val!=cur[j-1].first) {
for (int x:temp) s.insert(x);
temp.clear();
}
auto it = s.upper_bound(id);
ss l = *prev(it), r = *it;
if (l!=-1 && r!=INF) {
l++, r--;
if (pos[0][l][r-l].size()==0 || pos[0][l][r-l].back().first!=i) {
pos[0][l][r-l].push_back({i, -1});
start[0][i][l].push_back({r, -1});
}
}
temp.push_back(id);
}
}
for (int j=0;j<m;j++) {
cur.clear();
for (int i=0;i<n;i++) cur.push_back({a[i][j], i});
sort(cur.rbegin(), cur.rend());
s.clear(); s.insert(-1); s.insert(INF);
temp.clear();
for (int i=0;i<n;i++) {
auto [val, id] = cur[i];
if (i>0 && val!=cur[i-1].first) {
for (int x:temp) s.insert(x);
temp.clear();
}
auto it = s.upper_bound(id);
ss l = *prev(it), r = *it;
if (l!=-1 && r!=INF) {
l++, r--;
if (pos[1][l][r-l].size()==0 || pos[1][l][r-l].back().first!=j) {
pos[1][l][r-l].push_back({j, -1});
start[1][l][j].push_back({r, -1});
}
}
temp.push_back(id);
}
}
for (int I=0;I<=1;I++) {
int nn = m;
if (I==1) nn = n;
for (int l=0;l<nn;l++) for (int r=l;r<nn;r++) {
sort(pos[I][l][r-l].begin(), pos[I][l][r-l].end());
int sz = pos[I][l][r-l].size();
for (int i=sz-1;i>=0;i--) {
if (i==sz-1 || pos[I][l][r-l][i+1].first!=pos[I][l][r-l][i].first+1) pos[I][l][r-l][i].second = pos[I][l][r-l][i].first;
else pos[I][l][r-l][i].second = pos[I][l][r-l][i+1].second;
}
}
}
for (int I=0;I<=1;I++) {
for (int i=0;i<n;i++) for (int j=0;j<m;j++) {
for (auto &[r, mx]:start[I][i][j]) {
ss x = j, y = i;
if (I==1) x = i, y = j;
mx = (*lower_bound(pos[I][x][r-x].begin(), pos[I][x][r-x].end(), make_pair(y, (ss)-1))).second;
}
}
}
ll ans = 0;
vector<int> LOG;
for (int i=0;i<n;i++) for (int j=0;j<m;j++) {
sort(start[0][i][j].begin(), start[0][i][j].end(), cmp0);
sort(start[1][i][j].begin(), start[1][i][j].end(), cmp1);
int pt = -1;
LOG.clear();
for (auto [r, mx]:start[0][i][j]) {
while (pt+1 < start[1][i][j].size() && start[1][i][j][pt+1].mx >= r) {
pt++;
update(start[1][i][j][pt].r, 1);
LOG.push_back(start[1][i][j][pt].r);
}
ans += qry(mx);
}
for (int x:LOG) update(x, -1);
}
return ans;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:122:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<range>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | while (pt+1 < start[1][i][j].size() && start[1][i][j][pt+1].mx >= r) {
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
436 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
436 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
3 ms |
1428 KB |
Output is correct |
24 |
Correct |
3 ms |
1372 KB |
Output is correct |
25 |
Correct |
3 ms |
1116 KB |
Output is correct |
26 |
Correct |
4 ms |
1116 KB |
Output is correct |
27 |
Correct |
4 ms |
1112 KB |
Output is correct |
28 |
Correct |
4 ms |
1116 KB |
Output is correct |
29 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1428 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
4 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
436 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
17 ms |
6748 KB |
Output is correct |
31 |
Correct |
18 ms |
6744 KB |
Output is correct |
32 |
Correct |
17 ms |
6748 KB |
Output is correct |
33 |
Correct |
17 ms |
4700 KB |
Output is correct |
34 |
Correct |
28 ms |
5456 KB |
Output is correct |
35 |
Correct |
28 ms |
5716 KB |
Output is correct |
36 |
Correct |
27 ms |
5408 KB |
Output is correct |
37 |
Correct |
26 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1428 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
4 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
17 ms |
6748 KB |
Output is correct |
26 |
Correct |
18 ms |
6744 KB |
Output is correct |
27 |
Correct |
17 ms |
6748 KB |
Output is correct |
28 |
Correct |
17 ms |
4700 KB |
Output is correct |
29 |
Correct |
28 ms |
5456 KB |
Output is correct |
30 |
Correct |
28 ms |
5716 KB |
Output is correct |
31 |
Correct |
27 ms |
5408 KB |
Output is correct |
32 |
Correct |
26 ms |
5468 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
0 ms |
436 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
224 ms |
73144 KB |
Output is correct |
39 |
Correct |
223 ms |
67412 KB |
Output is correct |
40 |
Correct |
215 ms |
67408 KB |
Output is correct |
41 |
Correct |
197 ms |
61780 KB |
Output is correct |
42 |
Correct |
264 ms |
78672 KB |
Output is correct |
43 |
Correct |
255 ms |
78932 KB |
Output is correct |
44 |
Correct |
253 ms |
79036 KB |
Output is correct |
45 |
Correct |
247 ms |
74508 KB |
Output is correct |
46 |
Correct |
175 ms |
48976 KB |
Output is correct |
47 |
Correct |
241 ms |
52564 KB |
Output is correct |
48 |
Correct |
464 ms |
63148 KB |
Output is correct |
49 |
Correct |
479 ms |
65192 KB |
Output is correct |
50 |
Correct |
183 ms |
34388 KB |
Output is correct |
51 |
Correct |
189 ms |
34256 KB |
Output is correct |
52 |
Correct |
421 ms |
62208 KB |
Output is correct |
53 |
Correct |
403 ms |
63568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
75096 KB |
Output is correct |
2 |
Correct |
28 ms |
54108 KB |
Output is correct |
3 |
Correct |
38 ms |
74328 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
43 ms |
74836 KB |
Output is correct |
6 |
Correct |
41 ms |
74840 KB |
Output is correct |
7 |
Correct |
42 ms |
74840 KB |
Output is correct |
8 |
Correct |
39 ms |
74832 KB |
Output is correct |
9 |
Correct |
40 ms |
74740 KB |
Output is correct |
10 |
Correct |
39 ms |
74256 KB |
Output is correct |
11 |
Correct |
41 ms |
74584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
436 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1150 ms |
299432 KB |
Output is correct |
8 |
Correct |
2548 ms |
618200 KB |
Output is correct |
9 |
Correct |
2549 ms |
620808 KB |
Output is correct |
10 |
Correct |
2557 ms |
621184 KB |
Output is correct |
11 |
Correct |
265 ms |
267116 KB |
Output is correct |
12 |
Correct |
485 ms |
471632 KB |
Output is correct |
13 |
Correct |
501 ms |
502352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1372 KB |
Output is correct |
18 |
Correct |
3 ms |
1428 KB |
Output is correct |
19 |
Correct |
3 ms |
1372 KB |
Output is correct |
20 |
Correct |
3 ms |
1116 KB |
Output is correct |
21 |
Correct |
4 ms |
1116 KB |
Output is correct |
22 |
Correct |
4 ms |
1112 KB |
Output is correct |
23 |
Correct |
4 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
860 KB |
Output is correct |
25 |
Correct |
17 ms |
6748 KB |
Output is correct |
26 |
Correct |
18 ms |
6744 KB |
Output is correct |
27 |
Correct |
17 ms |
6748 KB |
Output is correct |
28 |
Correct |
17 ms |
4700 KB |
Output is correct |
29 |
Correct |
28 ms |
5456 KB |
Output is correct |
30 |
Correct |
28 ms |
5716 KB |
Output is correct |
31 |
Correct |
27 ms |
5408 KB |
Output is correct |
32 |
Correct |
26 ms |
5468 KB |
Output is correct |
33 |
Correct |
224 ms |
73144 KB |
Output is correct |
34 |
Correct |
223 ms |
67412 KB |
Output is correct |
35 |
Correct |
215 ms |
67408 KB |
Output is correct |
36 |
Correct |
197 ms |
61780 KB |
Output is correct |
37 |
Correct |
264 ms |
78672 KB |
Output is correct |
38 |
Correct |
255 ms |
78932 KB |
Output is correct |
39 |
Correct |
253 ms |
79036 KB |
Output is correct |
40 |
Correct |
247 ms |
74508 KB |
Output is correct |
41 |
Correct |
175 ms |
48976 KB |
Output is correct |
42 |
Correct |
241 ms |
52564 KB |
Output is correct |
43 |
Correct |
464 ms |
63148 KB |
Output is correct |
44 |
Correct |
479 ms |
65192 KB |
Output is correct |
45 |
Correct |
183 ms |
34388 KB |
Output is correct |
46 |
Correct |
189 ms |
34256 KB |
Output is correct |
47 |
Correct |
421 ms |
62208 KB |
Output is correct |
48 |
Correct |
403 ms |
63568 KB |
Output is correct |
49 |
Correct |
38 ms |
75096 KB |
Output is correct |
50 |
Correct |
28 ms |
54108 KB |
Output is correct |
51 |
Correct |
38 ms |
74328 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
43 ms |
74836 KB |
Output is correct |
54 |
Correct |
41 ms |
74840 KB |
Output is correct |
55 |
Correct |
42 ms |
74840 KB |
Output is correct |
56 |
Correct |
39 ms |
74832 KB |
Output is correct |
57 |
Correct |
40 ms |
74740 KB |
Output is correct |
58 |
Correct |
39 ms |
74256 KB |
Output is correct |
59 |
Correct |
41 ms |
74584 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
1150 ms |
299432 KB |
Output is correct |
62 |
Correct |
2548 ms |
618200 KB |
Output is correct |
63 |
Correct |
2549 ms |
620808 KB |
Output is correct |
64 |
Correct |
2557 ms |
621184 KB |
Output is correct |
65 |
Correct |
265 ms |
267116 KB |
Output is correct |
66 |
Correct |
485 ms |
471632 KB |
Output is correct |
67 |
Correct |
501 ms |
502352 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
1 ms |
344 KB |
Output is correct |
71 |
Correct |
0 ms |
436 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
3618 ms |
952872 KB |
Output is correct |
74 |
Correct |
3723 ms |
879580 KB |
Output is correct |
75 |
Correct |
3291 ms |
879656 KB |
Output is correct |
76 |
Correct |
2565 ms |
805540 KB |
Output is correct |
77 |
Correct |
4950 ms |
1035780 KB |
Output is correct |
78 |
Correct |
4165 ms |
491868 KB |
Output is correct |
79 |
Correct |
4551 ms |
532316 KB |
Output is correct |
80 |
Execution timed out |
5044 ms |
797012 KB |
Time limit exceeded |
81 |
Halted |
0 ms |
0 KB |
- |