#include "rect.h"
#include <bits/stdc++.h>
#define pb push_back
// #define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;
struct DSU{
int n, t;
vector<int> p;
DSU(int n, int t) : n(n), t(t), p(n) {
for(int i = 0; i < n; i++) p[i] = i;
}
int find(int u){
if(p[u] == u) return u;
return p[u] = find(p[u]);
}
void merge(int u, int v){
u = find(u);
v = find(v);
if(u == v) return;
if(u > v) swap(u, v);
if(t)
p[u] = v;
else
p[v] = u;
}
};
struct fenwick{
int n;
vector<int> fen;
fenwick(int n) : n(n), fen(n){}
void upd(int pos, int val){
for(; pos < n; pos |= pos + 1)
fen[pos] += val;
}
int query(int pos){
int ret = 0;
for(; pos >= 0; pos = (pos & (pos + 1)) - 1)
ret += fen[pos];
return ret;
}
int query(int ql, int qr){
return query(qr) - query(ql - 1);
}
};
ll count_rectangles(vector<vector<int>> a){
int n = a.size(), m = a[0].size();
ll ans = 0;
vector<vector<pair<int, int>>> rows(n);
for(int i = 0; i < n; i++){
vector<pair<int, int>> vals;
vector<int> act(m);
DSU left(m, 0), right(m, 1);
for(int j = 0; j < m; j++)
vals.pb({a[i][j], j});
sort(all(vals));
vector<vector<int>> same_idx(m);
int c = -1;
for(int k = 0, lst = -1; k < m; k++){
c += (lst != vals[k].first);
same_idx[c].pb(vals[k].second);
lst = vals[k].first;
}
same_idx.resize(c);
for(int j = 0; j < c; j++){
for(int ind : same_idx[j]){
act[ind] = 1;
if(ind && act[ind - 1])
left.merge(ind - 1, ind), right.merge(ind - 1, ind);
if(ind < m - 1 && act[ind + 1])
left.merge(ind, ind + 1), right.merge(ind, ind + 1);
}
set<pair<int, int>> interval;
for(int ind : same_idx[j])
interval.insert({left.find(ind), right.find(ind)});
for(auto [tl, tr] : interval)
if(tl != 0 && tr != m - 1)
rows[i].pb({tl, tr});
}
}
vector<vector<vector<pair<int, int>>>> down_extend(n, vector<vector<pair<int, int>>>(m));
vector<vector<int>> dp(m, vector<int>(m, -1));
vector<vector<int>> dp2(m, vector<int>(m, -1));
for(int i = n - 1; i >= 0; i--){
for(auto [tl, tr] : rows[i]){
int cev = (dp[tl][tr] == i + 1 ? dp2[tl][tr] + 1 : 1);
down_extend[i][tl].pb({tr, cev});
dp[tl][tr] = i;
dp2[tl][tr] = cev;
}
}
vector<vector<pair<int, int>>> cols(m);
for(int i = 0; i < m; i++){
vector<pair<int, int>> vals;
vector<int> act(n);
DSU up(n, 0), down(n, 1);
for(int j = 0; j < n; j++)
vals.pb({a[j][i], j});
sort(all(vals));
vector<vector<int>> same_idx(n);
int c = -1;
for(int k = 0, lst = - 1; k < n; k++){
c += (vals[k].first != lst);
same_idx[c].pb(vals[k].second);
lst = vals[k].first;
}
same_idx.resize(c);
for(int j = 0; j < c; j++){
for(int ind : same_idx[j]){
act[ind] = 1;
if(ind && act[ind - 1])
up.merge(ind - 1, ind), down.merge(ind - 1, ind);
if(ind < n - 1 && act[ind + 1])
up.merge(ind, ind + 1), down.merge(ind, ind + 1);
}
set<pair<int, int>> interval;
for(int ind : same_idx[j])
interval.insert({up.find(ind), down.find(ind)});
for(auto [tl, tr] : interval)
if(tl != 0 && tr != n - 1)
cols[i].pb({tl, tr});
}
}
vector<vector<vector<pair<int, int>>>> right_extend(n, vector<vector<pair<int, int>>>(m));
vector<vector<int>> DP(n, vector<int>(n));
vector<vector<int>> DP2(n, vector<int>(n));
for(int i = m - 1; i >= 0; i--){
for(auto [tl, tr] : cols[i]){
int cev = (DP[tl][tr] == i + 1 ? DP2[tl][tr] + 1 : 1);
right_extend[tl][i].pb({cev, tr});
DP[tl][tr] = i;
DP2[tl][tr] = cev;
}
}
fenwick fen(n);
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
sort(all(right_extend[i][j]));
sort(all(down_extend[i][j])); reverse(all(down_extend[i][j]));
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
int c = right_extend[i][j].size() - 1;
vector<int> rollback;
for(auto [tr, ext] : down_extend[i][j]){
while(c >= 0 && j + right_extend[i][j][c].first - 1 >= tr){
fen.upd(right_extend[i][j][c].second , 1);
rollback.pb(right_extend[i][j][c].second);
c--;
}
ans += fen.query(i, i + ext - 1);
}
for(int x : rollback)
fen.upd(x, -1);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
0 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
1384 KB |
Output is correct |
23 |
Correct |
2 ms |
1372 KB |
Output is correct |
24 |
Correct |
2 ms |
1372 KB |
Output is correct |
25 |
Correct |
2 ms |
1112 KB |
Output is correct |
26 |
Correct |
3 ms |
1116 KB |
Output is correct |
27 |
Correct |
3 ms |
1116 KB |
Output is correct |
28 |
Correct |
3 ms |
1156 KB |
Output is correct |
29 |
Correct |
2 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1384 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
1156 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 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 |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
14 ms |
6744 KB |
Output is correct |
31 |
Correct |
14 ms |
6744 KB |
Output is correct |
32 |
Correct |
14 ms |
6848 KB |
Output is correct |
33 |
Correct |
10 ms |
4480 KB |
Output is correct |
34 |
Correct |
19 ms |
5468 KB |
Output is correct |
35 |
Correct |
19 ms |
5872 KB |
Output is correct |
36 |
Correct |
18 ms |
5468 KB |
Output is correct |
37 |
Correct |
17 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1384 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
1156 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
14 ms |
6744 KB |
Output is correct |
26 |
Correct |
14 ms |
6744 KB |
Output is correct |
27 |
Correct |
14 ms |
6848 KB |
Output is correct |
28 |
Correct |
10 ms |
4480 KB |
Output is correct |
29 |
Correct |
19 ms |
5468 KB |
Output is correct |
30 |
Correct |
19 ms |
5872 KB |
Output is correct |
31 |
Correct |
18 ms |
5468 KB |
Output is correct |
32 |
Correct |
17 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 |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
150 ms |
58000 KB |
Output is correct |
39 |
Correct |
140 ms |
53140 KB |
Output is correct |
40 |
Correct |
128 ms |
53220 KB |
Output is correct |
41 |
Correct |
120 ms |
48264 KB |
Output is correct |
42 |
Correct |
175 ms |
78672 KB |
Output is correct |
43 |
Correct |
173 ms |
78588 KB |
Output is correct |
44 |
Correct |
175 ms |
78928 KB |
Output is correct |
45 |
Correct |
164 ms |
74320 KB |
Output is correct |
46 |
Correct |
129 ms |
46164 KB |
Output is correct |
47 |
Correct |
162 ms |
51416 KB |
Output is correct |
48 |
Correct |
294 ms |
65364 KB |
Output is correct |
49 |
Correct |
291 ms |
66576 KB |
Output is correct |
50 |
Correct |
113 ms |
34660 KB |
Output is correct |
51 |
Correct |
126 ms |
34508 KB |
Output is correct |
52 |
Correct |
247 ms |
64116 KB |
Output is correct |
53 |
Correct |
246 ms |
64772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
50268 KB |
Output is correct |
2 |
Correct |
14 ms |
36696 KB |
Output is correct |
3 |
Correct |
20 ms |
49960 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
20 ms |
50304 KB |
Output is correct |
6 |
Correct |
19 ms |
50216 KB |
Output is correct |
7 |
Correct |
20 ms |
50268 KB |
Output is correct |
8 |
Correct |
20 ms |
50264 KB |
Output is correct |
9 |
Correct |
20 ms |
50268 KB |
Output is correct |
10 |
Correct |
17 ms |
49756 KB |
Output is correct |
11 |
Correct |
18 ms |
50012 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
803 ms |
279164 KB |
Output is correct |
8 |
Correct |
1720 ms |
582048 KB |
Output is correct |
9 |
Correct |
1783 ms |
584852 KB |
Output is correct |
10 |
Correct |
1745 ms |
585024 KB |
Output is correct |
11 |
Correct |
312 ms |
233760 KB |
Output is correct |
12 |
Correct |
574 ms |
419672 KB |
Output is correct |
13 |
Correct |
585 ms |
447040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
452 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
544 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 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 |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
3 ms |
1384 KB |
Output is correct |
18 |
Correct |
2 ms |
1372 KB |
Output is correct |
19 |
Correct |
2 ms |
1372 KB |
Output is correct |
20 |
Correct |
2 ms |
1112 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
3 ms |
1116 KB |
Output is correct |
23 |
Correct |
3 ms |
1156 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
14 ms |
6744 KB |
Output is correct |
26 |
Correct |
14 ms |
6744 KB |
Output is correct |
27 |
Correct |
14 ms |
6848 KB |
Output is correct |
28 |
Correct |
10 ms |
4480 KB |
Output is correct |
29 |
Correct |
19 ms |
5468 KB |
Output is correct |
30 |
Correct |
19 ms |
5872 KB |
Output is correct |
31 |
Correct |
18 ms |
5468 KB |
Output is correct |
32 |
Correct |
17 ms |
5468 KB |
Output is correct |
33 |
Correct |
150 ms |
58000 KB |
Output is correct |
34 |
Correct |
140 ms |
53140 KB |
Output is correct |
35 |
Correct |
128 ms |
53220 KB |
Output is correct |
36 |
Correct |
120 ms |
48264 KB |
Output is correct |
37 |
Correct |
175 ms |
78672 KB |
Output is correct |
38 |
Correct |
173 ms |
78588 KB |
Output is correct |
39 |
Correct |
175 ms |
78928 KB |
Output is correct |
40 |
Correct |
164 ms |
74320 KB |
Output is correct |
41 |
Correct |
129 ms |
46164 KB |
Output is correct |
42 |
Correct |
162 ms |
51416 KB |
Output is correct |
43 |
Correct |
294 ms |
65364 KB |
Output is correct |
44 |
Correct |
291 ms |
66576 KB |
Output is correct |
45 |
Correct |
113 ms |
34660 KB |
Output is correct |
46 |
Correct |
126 ms |
34508 KB |
Output is correct |
47 |
Correct |
247 ms |
64116 KB |
Output is correct |
48 |
Correct |
246 ms |
64772 KB |
Output is correct |
49 |
Correct |
25 ms |
50268 KB |
Output is correct |
50 |
Correct |
14 ms |
36696 KB |
Output is correct |
51 |
Correct |
20 ms |
49960 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
20 ms |
50304 KB |
Output is correct |
54 |
Correct |
19 ms |
50216 KB |
Output is correct |
55 |
Correct |
20 ms |
50268 KB |
Output is correct |
56 |
Correct |
20 ms |
50264 KB |
Output is correct |
57 |
Correct |
20 ms |
50268 KB |
Output is correct |
58 |
Correct |
17 ms |
49756 KB |
Output is correct |
59 |
Correct |
18 ms |
50012 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
803 ms |
279164 KB |
Output is correct |
62 |
Correct |
1720 ms |
582048 KB |
Output is correct |
63 |
Correct |
1783 ms |
584852 KB |
Output is correct |
64 |
Correct |
1745 ms |
585024 KB |
Output is correct |
65 |
Correct |
312 ms |
233760 KB |
Output is correct |
66 |
Correct |
574 ms |
419672 KB |
Output is correct |
67 |
Correct |
585 ms |
447040 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 |
348 KB |
Output is correct |
71 |
Correct |
1 ms |
348 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
2023 ms |
722044 KB |
Output is correct |
74 |
Correct |
1786 ms |
654952 KB |
Output is correct |
75 |
Correct |
1672 ms |
655044 KB |
Output is correct |
76 |
Correct |
1665 ms |
588008 KB |
Output is correct |
77 |
Correct |
2680 ms |
974228 KB |
Output is correct |
78 |
Correct |
2294 ms |
491940 KB |
Output is correct |
79 |
Correct |
2474 ms |
520944 KB |
Output is correct |
80 |
Correct |
4176 ms |
799860 KB |
Output is correct |
81 |
Correct |
2275 ms |
501600 KB |
Output is correct |
82 |
Correct |
3114 ms |
648036 KB |
Output is correct |
83 |
Correct |
3950 ms |
815972 KB |
Output is correct |
84 |
Correct |
2144 ms |
482352 KB |
Output is correct |
85 |
Correct |
3791 ms |
793524 KB |
Output is correct |
86 |
Correct |
3626 ms |
770792 KB |
Output is correct |
87 |
Correct |
1425 ms |
597072 KB |
Output is correct |
88 |
Correct |
2543 ms |
973700 KB |
Output is correct |
89 |
Correct |
2600 ms |
950616 KB |
Output is correct |
90 |
Correct |
2686 ms |
950340 KB |
Output is correct |