#include "rect.h"
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#define sz(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
const int MAXN = 2550;
class Fenwick {
private:
vector <long long> f;
public:
void init(int n) {
f.assign(n, 0);
}
void update(int pos, int delta) {
for (; pos < sz(f); pos = (pos | (pos + 1))) {
f[pos] += delta;
}
}
long long sum(int pos) {
long long res = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
res += f[pos];
}
return res;
}
long long sum(int l, int r) {
return sum(r) - sum(l - 1);
}
};
struct Segment {
int r, from, to, len;
};
bool cmpFromToR(Segment &a, Segment &b) {
pair<pair<int, int>, int> ap = {{a.from, a.to}, a.r};
pair<pair<int, int>, int> bp = {{b.from, b.to}, b.r};
return ap < bp;
}
bool cmpRToLen(Segment &a, Segment &b) {
pair<pair<int, int>, int> ap = {{a.r, a.to}, a.len};
pair<pair<int, int>, int> bp = {{b.r, b.to}, b.len};
return ap < bp;
}
bool cmpToRFrom(Segment &a, Segment &b) {
pair<pair<int, int>, int> ap = {{a.to, a.r}, -a.from};
pair<pair<int, int>, int> bp = {{b.to, b.r}, -b.from};
return ap < bp;
}
int n, m;
int a[MAXN][MAXN];
vector<Segment> rows, cols;
Fenwick f;
void initRowsAndCols() {
for (int i = 1; i + 1 < n; i++) {
stack <int> st;
for (int j = 0; j < m; j++) {
while (!st.empty() && a[i][st.top()] < a[i][j]) {
st.pop();
}
if (!st.empty() && st.top() + 1 < j) {
rows.push_back({i, st.top(), j, 1});
}
st.push(j);
}
while (!st.empty()) {
st.pop();
}
for (int j = m - 1; j >= 0; j--) {
while (!st.empty() && a[i][st.top()] < a[i][j]) {
st.pop();
}
if (!st.empty() && a[i][st.top()] != a[i][j] && j + 1 < st.top()) {
rows.push_back({i, j, st.top(), 1});
}
st.push(j);
}
}
for (int j = 1; j + 1 < m; j++) {
stack <int> st;
for (int i = 0; i < n; i++) {
while (!st.empty() && a[st.top()][j] < a[i][j]) {
st.pop();
}
if (!st.empty() && st.top() + 1 < i) {
cols.push_back({j, st.top(), i, 1});
}
st.push(i);
}
while (!st.empty()) {
st.pop();
}
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && a[st.top()][j] < a[i][j]) {
st.pop();
}
if (!st.empty() && a[st.top()][j] != a[i][j] && i + 1 < st.top()) {
cols.push_back({j, i, st.top(), 1});
}
st.push(i);
}
}
}
void calcLen() {
sort(all(rows), cmpFromToR);
for (int i = 0; i < sz(rows); i++) {
if (i > 0 && rows[i - 1].from == rows[i].from && rows[i - 1].to == rows[i].to
&& rows[i - 1].r + 1 == rows[i].r) {
rows[i].len = rows[i - 1].len + 1;
}
}
sort(all(cols), cmpFromToR);
for (int i = 0; i < sz(cols); i++) {
if (i > 0 && cols[i - 1].from == cols[i].from && cols[i - 1].to == cols[i].to
&& cols[i - 1].r + 1 == cols[i].r) {
cols[i].len = cols[i - 1].len + 1;
}
}
}
void printSegment(Segment &a) {
cerr << a.r << " " << a.from << " " << a.to << " " << a.len << endl;
}
long long count_rectangles(std::vector<std::vector<int> > A) {
n = sz(A); m = sz(A[0]);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = A[i][j];
}
}
initRowsAndCols();
calcLen();
f.init(m);
sort(all(rows), cmpRToLen);
sort(all(cols), cmpToRFrom);
/* for (int i = 0; i < sz(rows); i++) {
printSegment(rows[i]);
}
cerr << "Cols:" << endl;
for (int i = 0; i < sz(cols); i++) {
printSegment(cols[i]);
}
cerr << endl; */
long long ans = 0;
vector<int> curRows;
vector<int> curCols;
int pRows = 0, pCols = 0;
for (int i = 1; i + 1 < n; i++) {
for (int j = 1; j + 1 < m; j++) {
while (pRows < sz(rows) && rows[pRows].r == i && rows[pRows].to == j + 1) {
curRows.push_back(pRows);
pRows++;
}
while (pCols < sz(cols) && cols[pCols].to == i + 1 && cols[pCols].r == j) {
curCols.push_back(pCols);
pCols++;
}
// cerr << i << " " << j << " " << sz(curRows) << " " << sz(curCols) << endl;
int curColsP = 0;
for (auto &row : curRows) {
while (curColsP < sz(curCols) && cols[curCols[curColsP]].from >= i - rows[row].len) {
f.update(j - cols[curCols[curColsP]].len + 1, 1);
curColsP++;
}
ans += f.sum(0, rows[row].from + 1);
}
for (int k = 0; k < curColsP; k++) {
f.update(j - cols[curCols[k]].len + 1, -1);
}
curRows.clear();
curCols.clear();
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
380 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
5 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
380 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
8 ms |
1016 KB |
Output is correct |
18 |
Correct |
8 ms |
1144 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
888 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1144 KB |
Output is correct |
23 |
Correct |
9 ms |
1016 KB |
Output is correct |
24 |
Correct |
8 ms |
888 KB |
Output is correct |
25 |
Correct |
5 ms |
256 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
5 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
380 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
8 ms |
1016 KB |
Output is correct |
18 |
Correct |
8 ms |
1144 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
888 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1144 KB |
Output is correct |
23 |
Correct |
9 ms |
1016 KB |
Output is correct |
24 |
Correct |
8 ms |
888 KB |
Output is correct |
25 |
Correct |
28 ms |
3948 KB |
Output is correct |
26 |
Correct |
29 ms |
3952 KB |
Output is correct |
27 |
Correct |
29 ms |
4080 KB |
Output is correct |
28 |
Correct |
18 ms |
2296 KB |
Output is correct |
29 |
Correct |
34 ms |
3824 KB |
Output is correct |
30 |
Correct |
35 ms |
4080 KB |
Output is correct |
31 |
Correct |
32 ms |
3952 KB |
Output is correct |
32 |
Correct |
35 ms |
3948 KB |
Output is correct |
33 |
Correct |
5 ms |
256 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
5 ms |
504 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
380 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
8 ms |
1016 KB |
Output is correct |
18 |
Correct |
8 ms |
1144 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
888 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1144 KB |
Output is correct |
23 |
Correct |
9 ms |
1016 KB |
Output is correct |
24 |
Correct |
8 ms |
888 KB |
Output is correct |
25 |
Correct |
28 ms |
3948 KB |
Output is correct |
26 |
Correct |
29 ms |
3952 KB |
Output is correct |
27 |
Correct |
29 ms |
4080 KB |
Output is correct |
28 |
Correct |
18 ms |
2296 KB |
Output is correct |
29 |
Correct |
34 ms |
3824 KB |
Output is correct |
30 |
Correct |
35 ms |
4080 KB |
Output is correct |
31 |
Correct |
32 ms |
3952 KB |
Output is correct |
32 |
Correct |
35 ms |
3948 KB |
Output is correct |
33 |
Correct |
186 ms |
22084 KB |
Output is correct |
34 |
Correct |
180 ms |
21980 KB |
Output is correct |
35 |
Correct |
172 ms |
21980 KB |
Output is correct |
36 |
Correct |
147 ms |
21980 KB |
Output is correct |
37 |
Correct |
333 ms |
32080 KB |
Output is correct |
38 |
Correct |
351 ms |
32208 KB |
Output is correct |
39 |
Correct |
335 ms |
32464 KB |
Output is correct |
40 |
Correct |
311 ms |
31316 KB |
Output is correct |
41 |
Correct |
127 ms |
14948 KB |
Output is correct |
42 |
Correct |
191 ms |
19164 KB |
Output is correct |
43 |
Correct |
430 ms |
30416 KB |
Output is correct |
44 |
Correct |
430 ms |
32468 KB |
Output is correct |
45 |
Correct |
204 ms |
17888 KB |
Output is correct |
46 |
Correct |
208 ms |
16476 KB |
Output is correct |
47 |
Correct |
382 ms |
30928 KB |
Output is correct |
48 |
Correct |
382 ms |
32080 KB |
Output is correct |
49 |
Correct |
5 ms |
256 KB |
Output is correct |
50 |
Correct |
5 ms |
376 KB |
Output is correct |
51 |
Correct |
5 ms |
504 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
680 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
632 KB |
Output is correct |
6 |
Correct |
7 ms |
504 KB |
Output is correct |
7 |
Correct |
6 ms |
504 KB |
Output is correct |
8 |
Correct |
7 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
753 ms |
76616 KB |
Output is correct |
3 |
Correct |
1698 ms |
159732 KB |
Output is correct |
4 |
Correct |
1674 ms |
160248 KB |
Output is correct |
5 |
Correct |
1704 ms |
160376 KB |
Output is correct |
6 |
Correct |
158 ms |
43000 KB |
Output is correct |
7 |
Correct |
416 ms |
82876 KB |
Output is correct |
8 |
Correct |
401 ms |
86660 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
504 KB |
Output is correct |
3 |
Correct |
5 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
504 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
11 |
Correct |
7 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
504 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
380 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
8 ms |
1016 KB |
Output is correct |
18 |
Correct |
8 ms |
1144 KB |
Output is correct |
19 |
Correct |
8 ms |
1016 KB |
Output is correct |
20 |
Correct |
7 ms |
888 KB |
Output is correct |
21 |
Correct |
9 ms |
1016 KB |
Output is correct |
22 |
Correct |
9 ms |
1144 KB |
Output is correct |
23 |
Correct |
9 ms |
1016 KB |
Output is correct |
24 |
Correct |
8 ms |
888 KB |
Output is correct |
25 |
Correct |
28 ms |
3948 KB |
Output is correct |
26 |
Correct |
29 ms |
3952 KB |
Output is correct |
27 |
Correct |
29 ms |
4080 KB |
Output is correct |
28 |
Correct |
18 ms |
2296 KB |
Output is correct |
29 |
Correct |
34 ms |
3824 KB |
Output is correct |
30 |
Correct |
35 ms |
4080 KB |
Output is correct |
31 |
Correct |
32 ms |
3952 KB |
Output is correct |
32 |
Correct |
35 ms |
3948 KB |
Output is correct |
33 |
Correct |
186 ms |
22084 KB |
Output is correct |
34 |
Correct |
180 ms |
21980 KB |
Output is correct |
35 |
Correct |
172 ms |
21980 KB |
Output is correct |
36 |
Correct |
147 ms |
21980 KB |
Output is correct |
37 |
Correct |
333 ms |
32080 KB |
Output is correct |
38 |
Correct |
351 ms |
32208 KB |
Output is correct |
39 |
Correct |
335 ms |
32464 KB |
Output is correct |
40 |
Correct |
311 ms |
31316 KB |
Output is correct |
41 |
Correct |
127 ms |
14948 KB |
Output is correct |
42 |
Correct |
191 ms |
19164 KB |
Output is correct |
43 |
Correct |
430 ms |
30416 KB |
Output is correct |
44 |
Correct |
430 ms |
32468 KB |
Output is correct |
45 |
Correct |
204 ms |
17888 KB |
Output is correct |
46 |
Correct |
208 ms |
16476 KB |
Output is correct |
47 |
Correct |
382 ms |
30928 KB |
Output is correct |
48 |
Correct |
382 ms |
32080 KB |
Output is correct |
49 |
Correct |
7 ms |
680 KB |
Output is correct |
50 |
Correct |
6 ms |
632 KB |
Output is correct |
51 |
Correct |
6 ms |
376 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
6 ms |
632 KB |
Output is correct |
54 |
Correct |
7 ms |
504 KB |
Output is correct |
55 |
Correct |
6 ms |
504 KB |
Output is correct |
56 |
Correct |
7 ms |
504 KB |
Output is correct |
57 |
Correct |
6 ms |
504 KB |
Output is correct |
58 |
Correct |
5 ms |
376 KB |
Output is correct |
59 |
Correct |
5 ms |
376 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
753 ms |
76616 KB |
Output is correct |
62 |
Correct |
1698 ms |
159732 KB |
Output is correct |
63 |
Correct |
1674 ms |
160248 KB |
Output is correct |
64 |
Correct |
1704 ms |
160376 KB |
Output is correct |
65 |
Correct |
158 ms |
43000 KB |
Output is correct |
66 |
Correct |
416 ms |
82876 KB |
Output is correct |
67 |
Correct |
401 ms |
86660 KB |
Output is correct |
68 |
Correct |
2747 ms |
217216 KB |
Output is correct |
69 |
Correct |
2780 ms |
217164 KB |
Output is correct |
70 |
Correct |
2644 ms |
217224 KB |
Output is correct |
71 |
Correct |
2220 ms |
217260 KB |
Output is correct |
72 |
Correct |
4984 ms |
332364 KB |
Output is correct |
73 |
Correct |
3702 ms |
181656 KB |
Output is correct |
74 |
Correct |
3926 ms |
205460 KB |
Output is correct |
75 |
Execution timed out |
5054 ms |
324740 KB |
Time limit exceeded |
76 |
Halted |
0 ms |
0 KB |
- |