#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include "rect.h"
#define ll long long
#define ar array
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
using namespace std;
template<typename T> struct fenwick {
int n; vector<T> bit;
fenwick(int a) : n(a), bit(a+1) {}
T sum(int pos) {
T s = 0;
for (; pos; s += bit[pos], pos -= pos&-pos);
return s;
}
T query(int l, int r) {
return sum(r+1) - sum(l);
}
void update(int pos, T x) {
pos++;
for (; pos <= n; bit[pos] += x, pos += pos&-pos);
}
};
const int nax = 2505;
short st[nax], idx[nax], idx2[nax];
vector<short> who[nax][nax], who2[nax][nax], to[nax][nax], to2[nax][nax];
fenwick<int> fw(nax);
long long count_rectangles(std::vector<std::vector<int>> a) {
int n = sz(a), m = sz(a[0]);
vector<vector<short>> u(n, vector<short>(m));
vector<vector<short>> d(n, vector<short>(m));
vector<vector<short>> l(n, vector<short>(m));
vector<vector<short>> r(n, vector<short>(m));
for (int i = 0, tp = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
while (tp && a[i][j] >= a[i][st[tp]]) tp--;
l[i][j] = tp ? st[tp] : m + 1;
st[++tp] = j;
}
tp = 0;
for (int j = m-1; j >= 0; j--) {
while (tp && a[i][j] >= a[i][st[tp]]) tp--;
r[i][j] = tp ? st[tp] : -2;
st[++tp] = j;
}
}
for (int i = 0, tp = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
while (tp && a[j][i] >= a[st[tp]][i]) tp--;
u[j][i] = tp ? st[tp] : n + 1;
st[++tp] = j;
}
tp = 0;
for (int j = n-1; j >= 0; j--) {
while (tp && a[j][i] >= a[st[tp]][i]) tp--;
d[j][i] = tp ? st[tp] : -2;
st[++tp] = j;
}
}
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
int L = l[i][j] + 1, R = r[i][j] - 1;
if (L <= R) who[i][L].emplace_back(R);
int U = u[i][j] + 1, D = d[i][j] - 1;
if (U <= D) who2[U][j].emplace_back(D);
}
u.clear(); u.shrink_to_fit();
r.clear(); r.shrink_to_fit();
l.clear(); l.shrink_to_fit();
d.clear(); d.shrink_to_fit();
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
sort(all(who[i][j]));
sort(all(who2[i][j]));
who[i][j].erase(unique(all(who[i][j])), who[i][j].end());
who2[i][j].erase(unique(all(who2[i][j])), who2[i][j].end());
to[i][j].resize(sz(who[i][j]));
to2[i][j].resize(sz(who2[i][j]));
}
for (int i = n-1; i >= 0; i--) {
for (int j = m-1; j >= 0; j--) {
for (int k = 0; k < sz(who[i][j]); k++) {
int r = who[i][j][k];
if (i+1 < n) {
auto it = lower_bound(all(who[i+1][j]), r);
if (it != who[i+1][j].end() && *it == r) to[i][j][k] = to[i+1][j][it - who[i+1][j].begin()];
else to[i][j][k] = i;
}
else to[i][j][k] = i;
}
for (int k = 0; k < sz(who2[i][j]); k++) {
int d = who2[i][j][k];
if (j+1 < m) {
auto it = lower_bound(all(who2[i][j+1]), d);
if (it != who2[i][j+1].end() && *it == d) to2[i][j][k] = to2[i][j+1][it - who2[i][j+1].begin()];
else to2[i][j][k] = j;
}
else to2[i][j][k] = j;
}
}
}
ll ans = 0;
for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
iota(idx, idx + sz(who[i][j]), 0);
sort(idx, idx + sz(who[i][j]), [&](int x, int y) { return who[i][j][x] < who[i][j][y]; });
iota(idx2, idx2 + sz(who2[i][j]), 0);
sort(idx2, idx2 + sz(who2[i][j]), [&](int x, int y) { return to2[i][j][x] < to2[i][j][y]; });
int t = 0;
for (int k = 0; k < sz(who2[i][j]); k++) {
while (t < sz(who[i][j]) && who[i][j][idx[t]] <= to2[i][j][idx2[k]]) {
fw.update(to[i][j][idx[t++]], 1);
}
ans += fw.query(who2[i][j][idx2[k]], nax-1);
}
for (int k = 0; k < t; k++) fw.update(to[i][j][idx[k]], -1);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
589648 KB |
Output is correct |
2 |
Correct |
172 ms |
589924 KB |
Output is correct |
3 |
Correct |
177 ms |
589908 KB |
Output is correct |
4 |
Correct |
168 ms |
589776 KB |
Output is correct |
5 |
Correct |
165 ms |
589648 KB |
Output is correct |
6 |
Correct |
181 ms |
589840 KB |
Output is correct |
7 |
Correct |
177 ms |
589912 KB |
Output is correct |
8 |
Correct |
172 ms |
589908 KB |
Output is correct |
9 |
Correct |
175 ms |
589708 KB |
Output is correct |
10 |
Correct |
230 ms |
589928 KB |
Output is correct |
11 |
Correct |
196 ms |
589736 KB |
Output is correct |
12 |
Correct |
177 ms |
589884 KB |
Output is correct |
13 |
Correct |
174 ms |
589648 KB |
Output is correct |
14 |
Correct |
190 ms |
589652 KB |
Output is correct |
15 |
Correct |
195 ms |
589648 KB |
Output is correct |
16 |
Correct |
176 ms |
589764 KB |
Output is correct |
17 |
Correct |
196 ms |
589652 KB |
Output is correct |
18 |
Correct |
193 ms |
589784 KB |
Output is correct |
19 |
Correct |
174 ms |
589816 KB |
Output is correct |
20 |
Correct |
184 ms |
589824 KB |
Output is correct |
21 |
Correct |
165 ms |
589848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
589648 KB |
Output is correct |
2 |
Correct |
172 ms |
589924 KB |
Output is correct |
3 |
Correct |
177 ms |
589908 KB |
Output is correct |
4 |
Correct |
168 ms |
589776 KB |
Output is correct |
5 |
Correct |
165 ms |
589648 KB |
Output is correct |
6 |
Correct |
181 ms |
589840 KB |
Output is correct |
7 |
Correct |
177 ms |
589912 KB |
Output is correct |
8 |
Correct |
172 ms |
589908 KB |
Output is correct |
9 |
Correct |
175 ms |
589708 KB |
Output is correct |
10 |
Correct |
230 ms |
589928 KB |
Output is correct |
11 |
Correct |
196 ms |
589736 KB |
Output is correct |
12 |
Correct |
177 ms |
589884 KB |
Output is correct |
13 |
Correct |
174 ms |
589648 KB |
Output is correct |
14 |
Correct |
190 ms |
589652 KB |
Output is correct |
15 |
Correct |
195 ms |
589648 KB |
Output is correct |
16 |
Correct |
176 ms |
589764 KB |
Output is correct |
17 |
Correct |
196 ms |
589652 KB |
Output is correct |
18 |
Correct |
193 ms |
589784 KB |
Output is correct |
19 |
Correct |
174 ms |
589816 KB |
Output is correct |
20 |
Correct |
184 ms |
589824 KB |
Output is correct |
21 |
Correct |
165 ms |
589848 KB |
Output is correct |
22 |
Correct |
167 ms |
590672 KB |
Output is correct |
23 |
Correct |
168 ms |
590672 KB |
Output is correct |
24 |
Correct |
166 ms |
590676 KB |
Output is correct |
25 |
Correct |
173 ms |
590024 KB |
Output is correct |
26 |
Correct |
182 ms |
590156 KB |
Output is correct |
27 |
Correct |
174 ms |
590100 KB |
Output is correct |
28 |
Correct |
181 ms |
590176 KB |
Output is correct |
29 |
Correct |
176 ms |
590048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
589648 KB |
Output is correct |
2 |
Correct |
172 ms |
589924 KB |
Output is correct |
3 |
Correct |
177 ms |
589908 KB |
Output is correct |
4 |
Correct |
168 ms |
589776 KB |
Output is correct |
5 |
Correct |
165 ms |
589648 KB |
Output is correct |
6 |
Correct |
181 ms |
589840 KB |
Output is correct |
7 |
Correct |
177 ms |
589912 KB |
Output is correct |
8 |
Correct |
172 ms |
589908 KB |
Output is correct |
9 |
Correct |
175 ms |
589708 KB |
Output is correct |
10 |
Correct |
230 ms |
589928 KB |
Output is correct |
11 |
Correct |
196 ms |
589736 KB |
Output is correct |
12 |
Correct |
177 ms |
589884 KB |
Output is correct |
13 |
Correct |
174 ms |
589648 KB |
Output is correct |
14 |
Correct |
190 ms |
589652 KB |
Output is correct |
15 |
Correct |
195 ms |
589648 KB |
Output is correct |
16 |
Correct |
176 ms |
589764 KB |
Output is correct |
17 |
Correct |
167 ms |
590672 KB |
Output is correct |
18 |
Correct |
168 ms |
590672 KB |
Output is correct |
19 |
Correct |
166 ms |
590676 KB |
Output is correct |
20 |
Correct |
173 ms |
590024 KB |
Output is correct |
21 |
Correct |
182 ms |
590156 KB |
Output is correct |
22 |
Correct |
174 ms |
590100 KB |
Output is correct |
23 |
Correct |
181 ms |
590176 KB |
Output is correct |
24 |
Correct |
176 ms |
590048 KB |
Output is correct |
25 |
Correct |
196 ms |
589652 KB |
Output is correct |
26 |
Correct |
193 ms |
589784 KB |
Output is correct |
27 |
Correct |
174 ms |
589816 KB |
Output is correct |
28 |
Correct |
184 ms |
589824 KB |
Output is correct |
29 |
Correct |
165 ms |
589848 KB |
Output is correct |
30 |
Correct |
175 ms |
595024 KB |
Output is correct |
31 |
Correct |
191 ms |
594904 KB |
Output is correct |
32 |
Correct |
213 ms |
595024 KB |
Output is correct |
33 |
Correct |
176 ms |
591752 KB |
Output is correct |
34 |
Correct |
235 ms |
592520 KB |
Output is correct |
35 |
Correct |
201 ms |
592472 KB |
Output is correct |
36 |
Correct |
252 ms |
592468 KB |
Output is correct |
37 |
Correct |
204 ms |
592264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
589648 KB |
Output is correct |
2 |
Correct |
172 ms |
589924 KB |
Output is correct |
3 |
Correct |
177 ms |
589908 KB |
Output is correct |
4 |
Correct |
168 ms |
589776 KB |
Output is correct |
5 |
Correct |
165 ms |
589648 KB |
Output is correct |
6 |
Correct |
181 ms |
589840 KB |
Output is correct |
7 |
Correct |
177 ms |
589912 KB |
Output is correct |
8 |
Correct |
172 ms |
589908 KB |
Output is correct |
9 |
Correct |
175 ms |
589708 KB |
Output is correct |
10 |
Correct |
230 ms |
589928 KB |
Output is correct |
11 |
Correct |
196 ms |
589736 KB |
Output is correct |
12 |
Correct |
177 ms |
589884 KB |
Output is correct |
13 |
Correct |
174 ms |
589648 KB |
Output is correct |
14 |
Correct |
190 ms |
589652 KB |
Output is correct |
15 |
Correct |
195 ms |
589648 KB |
Output is correct |
16 |
Correct |
176 ms |
589764 KB |
Output is correct |
17 |
Correct |
167 ms |
590672 KB |
Output is correct |
18 |
Correct |
168 ms |
590672 KB |
Output is correct |
19 |
Correct |
166 ms |
590676 KB |
Output is correct |
20 |
Correct |
173 ms |
590024 KB |
Output is correct |
21 |
Correct |
182 ms |
590156 KB |
Output is correct |
22 |
Correct |
174 ms |
590100 KB |
Output is correct |
23 |
Correct |
181 ms |
590176 KB |
Output is correct |
24 |
Correct |
176 ms |
590048 KB |
Output is correct |
25 |
Correct |
175 ms |
595024 KB |
Output is correct |
26 |
Correct |
191 ms |
594904 KB |
Output is correct |
27 |
Correct |
213 ms |
595024 KB |
Output is correct |
28 |
Correct |
176 ms |
591752 KB |
Output is correct |
29 |
Correct |
235 ms |
592520 KB |
Output is correct |
30 |
Correct |
201 ms |
592472 KB |
Output is correct |
31 |
Correct |
252 ms |
592468 KB |
Output is correct |
32 |
Correct |
204 ms |
592264 KB |
Output is correct |
33 |
Correct |
196 ms |
589652 KB |
Output is correct |
34 |
Correct |
193 ms |
589784 KB |
Output is correct |
35 |
Correct |
174 ms |
589816 KB |
Output is correct |
36 |
Correct |
184 ms |
589824 KB |
Output is correct |
37 |
Correct |
165 ms |
589848 KB |
Output is correct |
38 |
Correct |
258 ms |
624292 KB |
Output is correct |
39 |
Correct |
218 ms |
610132 KB |
Output is correct |
40 |
Correct |
284 ms |
610176 KB |
Output is correct |
41 |
Correct |
224 ms |
599120 KB |
Output is correct |
42 |
Correct |
315 ms |
654860 KB |
Output is correct |
43 |
Correct |
319 ms |
654636 KB |
Output is correct |
44 |
Correct |
298 ms |
654676 KB |
Output is correct |
45 |
Correct |
308 ms |
650580 KB |
Output is correct |
46 |
Correct |
251 ms |
608944 KB |
Output is correct |
47 |
Correct |
295 ms |
613968 KB |
Output is correct |
48 |
Correct |
347 ms |
624208 KB |
Output is correct |
49 |
Correct |
393 ms |
624312 KB |
Output is correct |
50 |
Correct |
268 ms |
606992 KB |
Output is correct |
51 |
Correct |
267 ms |
607020 KB |
Output is correct |
52 |
Correct |
393 ms |
621904 KB |
Output is correct |
53 |
Correct |
342 ms |
621908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
590416 KB |
Output is correct |
2 |
Correct |
171 ms |
590416 KB |
Output is correct |
3 |
Correct |
173 ms |
589948 KB |
Output is correct |
4 |
Correct |
176 ms |
589656 KB |
Output is correct |
5 |
Correct |
163 ms |
589984 KB |
Output is correct |
6 |
Correct |
168 ms |
590124 KB |
Output is correct |
7 |
Correct |
173 ms |
590152 KB |
Output is correct |
8 |
Correct |
191 ms |
590160 KB |
Output is correct |
9 |
Correct |
178 ms |
590020 KB |
Output is correct |
10 |
Correct |
173 ms |
589904 KB |
Output is correct |
11 |
Correct |
172 ms |
589912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
589652 KB |
Output is correct |
2 |
Correct |
193 ms |
589784 KB |
Output is correct |
3 |
Correct |
174 ms |
589816 KB |
Output is correct |
4 |
Correct |
184 ms |
589824 KB |
Output is correct |
5 |
Correct |
165 ms |
589848 KB |
Output is correct |
6 |
Correct |
175 ms |
589652 KB |
Output is correct |
7 |
Correct |
750 ms |
701776 KB |
Output is correct |
8 |
Correct |
1429 ms |
833108 KB |
Output is correct |
9 |
Correct |
1435 ms |
834480 KB |
Output is correct |
10 |
Correct |
1448 ms |
834524 KB |
Output is correct |
11 |
Correct |
320 ms |
638416 KB |
Output is correct |
12 |
Correct |
477 ms |
682068 KB |
Output is correct |
13 |
Correct |
490 ms |
688204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
589648 KB |
Output is correct |
2 |
Correct |
172 ms |
589924 KB |
Output is correct |
3 |
Correct |
177 ms |
589908 KB |
Output is correct |
4 |
Correct |
168 ms |
589776 KB |
Output is correct |
5 |
Correct |
165 ms |
589648 KB |
Output is correct |
6 |
Correct |
181 ms |
589840 KB |
Output is correct |
7 |
Correct |
177 ms |
589912 KB |
Output is correct |
8 |
Correct |
172 ms |
589908 KB |
Output is correct |
9 |
Correct |
175 ms |
589708 KB |
Output is correct |
10 |
Correct |
230 ms |
589928 KB |
Output is correct |
11 |
Correct |
196 ms |
589736 KB |
Output is correct |
12 |
Correct |
177 ms |
589884 KB |
Output is correct |
13 |
Correct |
174 ms |
589648 KB |
Output is correct |
14 |
Correct |
190 ms |
589652 KB |
Output is correct |
15 |
Correct |
195 ms |
589648 KB |
Output is correct |
16 |
Correct |
176 ms |
589764 KB |
Output is correct |
17 |
Correct |
167 ms |
590672 KB |
Output is correct |
18 |
Correct |
168 ms |
590672 KB |
Output is correct |
19 |
Correct |
166 ms |
590676 KB |
Output is correct |
20 |
Correct |
173 ms |
590024 KB |
Output is correct |
21 |
Correct |
182 ms |
590156 KB |
Output is correct |
22 |
Correct |
174 ms |
590100 KB |
Output is correct |
23 |
Correct |
181 ms |
590176 KB |
Output is correct |
24 |
Correct |
176 ms |
590048 KB |
Output is correct |
25 |
Correct |
175 ms |
595024 KB |
Output is correct |
26 |
Correct |
191 ms |
594904 KB |
Output is correct |
27 |
Correct |
213 ms |
595024 KB |
Output is correct |
28 |
Correct |
176 ms |
591752 KB |
Output is correct |
29 |
Correct |
235 ms |
592520 KB |
Output is correct |
30 |
Correct |
201 ms |
592472 KB |
Output is correct |
31 |
Correct |
252 ms |
592468 KB |
Output is correct |
32 |
Correct |
204 ms |
592264 KB |
Output is correct |
33 |
Correct |
258 ms |
624292 KB |
Output is correct |
34 |
Correct |
218 ms |
610132 KB |
Output is correct |
35 |
Correct |
284 ms |
610176 KB |
Output is correct |
36 |
Correct |
224 ms |
599120 KB |
Output is correct |
37 |
Correct |
315 ms |
654860 KB |
Output is correct |
38 |
Correct |
319 ms |
654636 KB |
Output is correct |
39 |
Correct |
298 ms |
654676 KB |
Output is correct |
40 |
Correct |
308 ms |
650580 KB |
Output is correct |
41 |
Correct |
251 ms |
608944 KB |
Output is correct |
42 |
Correct |
295 ms |
613968 KB |
Output is correct |
43 |
Correct |
347 ms |
624208 KB |
Output is correct |
44 |
Correct |
393 ms |
624312 KB |
Output is correct |
45 |
Correct |
268 ms |
606992 KB |
Output is correct |
46 |
Correct |
267 ms |
607020 KB |
Output is correct |
47 |
Correct |
393 ms |
621904 KB |
Output is correct |
48 |
Correct |
342 ms |
621908 KB |
Output is correct |
49 |
Correct |
190 ms |
590416 KB |
Output is correct |
50 |
Correct |
171 ms |
590416 KB |
Output is correct |
51 |
Correct |
173 ms |
589948 KB |
Output is correct |
52 |
Correct |
176 ms |
589656 KB |
Output is correct |
53 |
Correct |
163 ms |
589984 KB |
Output is correct |
54 |
Correct |
168 ms |
590124 KB |
Output is correct |
55 |
Correct |
173 ms |
590152 KB |
Output is correct |
56 |
Correct |
191 ms |
590160 KB |
Output is correct |
57 |
Correct |
178 ms |
590020 KB |
Output is correct |
58 |
Correct |
173 ms |
589904 KB |
Output is correct |
59 |
Correct |
172 ms |
589912 KB |
Output is correct |
60 |
Correct |
175 ms |
589652 KB |
Output is correct |
61 |
Correct |
750 ms |
701776 KB |
Output is correct |
62 |
Correct |
1429 ms |
833108 KB |
Output is correct |
63 |
Correct |
1435 ms |
834480 KB |
Output is correct |
64 |
Correct |
1448 ms |
834524 KB |
Output is correct |
65 |
Correct |
320 ms |
638416 KB |
Output is correct |
66 |
Correct |
477 ms |
682068 KB |
Output is correct |
67 |
Correct |
490 ms |
688204 KB |
Output is correct |
68 |
Correct |
196 ms |
589652 KB |
Output is correct |
69 |
Correct |
193 ms |
589784 KB |
Output is correct |
70 |
Correct |
174 ms |
589816 KB |
Output is correct |
71 |
Correct |
184 ms |
589824 KB |
Output is correct |
72 |
Correct |
165 ms |
589848 KB |
Output is correct |
73 |
Correct |
1208 ms |
1048576 KB |
Output is correct |
74 |
Correct |
1084 ms |
873056 KB |
Output is correct |
75 |
Correct |
1103 ms |
873376 KB |
Output is correct |
76 |
Correct |
889 ms |
729892 KB |
Output is correct |
77 |
Runtime error |
835 ms |
1048576 KB |
Execution killed with signal 9 |
78 |
Halted |
0 ms |
0 KB |
- |