#include <bits/stdc++.h>
#include "rect.h"
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
const int nmax = 25e2 + 5;
#define lsb(x) (x & -x)
struct AIB {
vector<int> v;
vector<pii> st;
void init(int n) {
v.assign(n + 3, 0);
st.clear();
}
template<int reverser = 0> void upd(int p, int x) {
if(!reverser) st.emplace_back(p, x);
++p;
while(p < sz(v)) v[p] += x, p += lsb(p);
}
int query(int p) {
++p;
int sum = 0;
while(p > 0) sum += v[p], p -= lsb(p);
return sum;
}
int gettime() { return sz(st); }
void revert() {
if(!sz(st)) return;
auto [a, b] = st.back();
upd<1>(a, -b);
st.pop_back();
}
void revert(int btime) {
while(gettime() > btime) revert();
}
};
AIB aib;
struct RangeCounter {
pii mat[nmax][nmax];
void init(int n) { for(int i = 0; i < n; i++) for(int j = i; j < n; j++) mat[i][j].second = -100; }
void addright(int l, int r, int R) {
if(r - l <= 1) return;
//cerr << "\n(******)\nadding " << l << ' ' << r << '\t' << R << "\n(******)\n";
if(mat[l][r].second != R - 1) mat[l][r].first = R;
mat[l][r].second = R;
}
int qleft(int l, int r) { return mat[l][r].first - 1; }
};
RangeCounter hori, verti;
long long count_rectangles(std::vector<std::vector<int>> mat) {
ll rez = 0;
int n = sz(mat), m = sz(mat[0]);
if(n < 3 || m < 3) return 0ll;
hori.init(m + 1);
verti.init(n + 1);
aib.init(max(n, m) + 2);
vector<vector<int>> colst(m, vector<int>());
auto addcolcell = [&](vector<int>& leftcol, int i, int j) {
//cerr << "Pentru verticale::\n";
int lst = -12918;
while(1) {
if(colst[j].empty()) break;
if(lst != mat[i][j]) {
if(colst[j].back() < i - 1) leftcol.emplace_back(colst[j].back());
verti.addright(colst[j].back(), i, j);
}
if(!(mat[colst[j].back()][j] <= mat[i][j])) break;
lst = mat[colst[j].back()][j];
colst[j].pop_back();
}
colst[j].emplace_back(i);
};
vector<int> sdfiasfifas_tmp;
for(int i = 0; i < 2; i++)
for(int j = 0; j < m; j++)
addcolcell(sdfiasfifas_tmp, i, j);
for(int i = 1; i < n - 1; i++) {
vector<int> rowst;
for(int j = 0; j < m; j++) {
vector<int> leftcol, leftrow; // left pe row sau pe col
{
int lst = -129423;
while(1) {
if(rowst.empty()) break;
if(lst != mat[i][j]) {
if(rowst.back() < j - 1) leftrow.emplace_back(rowst.back());
hori.addright(rowst.back(), j, i);
}
if(!(mat[i][rowst.back()] <= mat[i][j])) break;
lst = mat[i][rowst.back()];
rowst.pop_back();
}
rowst.emplace_back(j);
}
if(j >= 2) {
addcolcell(leftcol, i + 1, j - 1);
sort(all(leftrow), [&](int a, int b) { return hori.qleft(a, j) > hori.qleft(b, j); });
int p = 0;
aib.revert(0);
for(auto x : leftrow) {
while(p < sz(leftcol) && hori.qleft(x, j) <= leftcol[p])
aib.upd(verti.qleft(leftcol[p], i + 1), 1), ++p;
rez += aib.query(x);
}
}
}
}
return rez;
}
/**
Istenem! Nu poate fi real.
-- Surse verificate
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
692 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
692 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
1112 KB |
Output is correct |
23 |
Correct |
1 ms |
1284 KB |
Output is correct |
24 |
Correct |
1 ms |
1112 KB |
Output is correct |
25 |
Correct |
1 ms |
1116 KB |
Output is correct |
26 |
Correct |
1 ms |
1116 KB |
Output is correct |
27 |
Correct |
1 ms |
1236 KB |
Output is correct |
28 |
Correct |
2 ms |
1116 KB |
Output is correct |
29 |
Correct |
1 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1284 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 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 |
0 ms |
692 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
4 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
3152 KB |
Output is correct |
32 |
Correct |
4 ms |
3020 KB |
Output is correct |
33 |
Correct |
4 ms |
2652 KB |
Output is correct |
34 |
Correct |
6 ms |
2652 KB |
Output is correct |
35 |
Correct |
7 ms |
2980 KB |
Output is correct |
36 |
Correct |
7 ms |
2912 KB |
Output is correct |
37 |
Correct |
6 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1284 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
3152 KB |
Output is correct |
27 |
Correct |
4 ms |
3020 KB |
Output is correct |
28 |
Correct |
4 ms |
2652 KB |
Output is correct |
29 |
Correct |
6 ms |
2652 KB |
Output is correct |
30 |
Correct |
7 ms |
2980 KB |
Output is correct |
31 |
Correct |
7 ms |
2912 KB |
Output is correct |
32 |
Correct |
6 ms |
2908 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
692 KB |
Output is correct |
36 |
Correct |
1 ms |
604 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
36 ms |
18232 KB |
Output is correct |
39 |
Correct |
32 ms |
18360 KB |
Output is correct |
40 |
Correct |
44 ms |
18248 KB |
Output is correct |
41 |
Correct |
44 ms |
18196 KB |
Output is correct |
42 |
Correct |
47 ms |
19884 KB |
Output is correct |
43 |
Correct |
37 ms |
19776 KB |
Output is correct |
44 |
Correct |
38 ms |
20048 KB |
Output is correct |
45 |
Correct |
36 ms |
19284 KB |
Output is correct |
46 |
Correct |
38 ms |
14684 KB |
Output is correct |
47 |
Correct |
37 ms |
14680 KB |
Output is correct |
48 |
Correct |
75 ms |
15940 KB |
Output is correct |
49 |
Correct |
81 ms |
17580 KB |
Output is correct |
50 |
Correct |
38 ms |
10840 KB |
Output is correct |
51 |
Correct |
39 ms |
10836 KB |
Output is correct |
52 |
Correct |
74 ms |
16664 KB |
Output is correct |
53 |
Correct |
81 ms |
17748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
34136 KB |
Output is correct |
2 |
Correct |
13 ms |
26972 KB |
Output is correct |
3 |
Correct |
14 ms |
34140 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
13 ms |
34140 KB |
Output is correct |
6 |
Correct |
13 ms |
34204 KB |
Output is correct |
7 |
Correct |
14 ms |
34140 KB |
Output is correct |
8 |
Correct |
13 ms |
34136 KB |
Output is correct |
9 |
Correct |
15 ms |
34392 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
692 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
171 ms |
69412 KB |
Output is correct |
8 |
Correct |
390 ms |
128336 KB |
Output is correct |
9 |
Correct |
410 ms |
128776 KB |
Output is correct |
10 |
Correct |
379 ms |
129028 KB |
Output is correct |
11 |
Correct |
85 ms |
75132 KB |
Output is correct |
12 |
Correct |
159 ms |
121936 KB |
Output is correct |
13 |
Correct |
176 ms |
128852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
1112 KB |
Output is correct |
18 |
Correct |
1 ms |
1284 KB |
Output is correct |
19 |
Correct |
1 ms |
1112 KB |
Output is correct |
20 |
Correct |
1 ms |
1116 KB |
Output is correct |
21 |
Correct |
1 ms |
1116 KB |
Output is correct |
22 |
Correct |
1 ms |
1236 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
1 ms |
860 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
3 ms |
3152 KB |
Output is correct |
27 |
Correct |
4 ms |
3020 KB |
Output is correct |
28 |
Correct |
4 ms |
2652 KB |
Output is correct |
29 |
Correct |
6 ms |
2652 KB |
Output is correct |
30 |
Correct |
7 ms |
2980 KB |
Output is correct |
31 |
Correct |
7 ms |
2912 KB |
Output is correct |
32 |
Correct |
6 ms |
2908 KB |
Output is correct |
33 |
Correct |
36 ms |
18232 KB |
Output is correct |
34 |
Correct |
32 ms |
18360 KB |
Output is correct |
35 |
Correct |
44 ms |
18248 KB |
Output is correct |
36 |
Correct |
44 ms |
18196 KB |
Output is correct |
37 |
Correct |
47 ms |
19884 KB |
Output is correct |
38 |
Correct |
37 ms |
19776 KB |
Output is correct |
39 |
Correct |
38 ms |
20048 KB |
Output is correct |
40 |
Correct |
36 ms |
19284 KB |
Output is correct |
41 |
Correct |
38 ms |
14684 KB |
Output is correct |
42 |
Correct |
37 ms |
14680 KB |
Output is correct |
43 |
Correct |
75 ms |
15940 KB |
Output is correct |
44 |
Correct |
81 ms |
17580 KB |
Output is correct |
45 |
Correct |
38 ms |
10840 KB |
Output is correct |
46 |
Correct |
39 ms |
10836 KB |
Output is correct |
47 |
Correct |
74 ms |
16664 KB |
Output is correct |
48 |
Correct |
81 ms |
17748 KB |
Output is correct |
49 |
Correct |
14 ms |
34136 KB |
Output is correct |
50 |
Correct |
13 ms |
26972 KB |
Output is correct |
51 |
Correct |
14 ms |
34140 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
13 ms |
34140 KB |
Output is correct |
54 |
Correct |
13 ms |
34204 KB |
Output is correct |
55 |
Correct |
14 ms |
34140 KB |
Output is correct |
56 |
Correct |
13 ms |
34136 KB |
Output is correct |
57 |
Correct |
15 ms |
34392 KB |
Output is correct |
58 |
Correct |
0 ms |
344 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
344 KB |
Output is correct |
61 |
Correct |
171 ms |
69412 KB |
Output is correct |
62 |
Correct |
390 ms |
128336 KB |
Output is correct |
63 |
Correct |
410 ms |
128776 KB |
Output is correct |
64 |
Correct |
379 ms |
129028 KB |
Output is correct |
65 |
Correct |
85 ms |
75132 KB |
Output is correct |
66 |
Correct |
159 ms |
121936 KB |
Output is correct |
67 |
Correct |
176 ms |
128852 KB |
Output is correct |
68 |
Correct |
0 ms |
348 KB |
Output is correct |
69 |
Correct |
0 ms |
348 KB |
Output is correct |
70 |
Correct |
0 ms |
692 KB |
Output is correct |
71 |
Correct |
1 ms |
604 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
512 ms |
180456 KB |
Output is correct |
74 |
Correct |
505 ms |
180860 KB |
Output is correct |
75 |
Correct |
573 ms |
181084 KB |
Output is correct |
76 |
Correct |
529 ms |
181032 KB |
Output is correct |
77 |
Correct |
667 ms |
202088 KB |
Output is correct |
78 |
Correct |
579 ms |
92220 KB |
Output is correct |
79 |
Correct |
595 ms |
103248 KB |
Output is correct |
80 |
Correct |
957 ms |
139580 KB |
Output is correct |
81 |
Correct |
624 ms |
107192 KB |
Output is correct |
82 |
Correct |
829 ms |
135564 KB |
Output is correct |
83 |
Correct |
1108 ms |
164768 KB |
Output is correct |
84 |
Correct |
638 ms |
100028 KB |
Output is correct |
85 |
Correct |
1111 ms |
165260 KB |
Output is correct |
86 |
Correct |
973 ms |
161000 KB |
Output is correct |
87 |
Correct |
372 ms |
129612 KB |
Output is correct |
88 |
Correct |
753 ms |
201960 KB |
Output is correct |
89 |
Correct |
674 ms |
202132 KB |
Output is correct |
90 |
Correct |
709 ms |
201964 KB |
Output is correct |