#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=2500+10;
int all[maxn][maxn],n,m,dpp[maxn][maxn];
vector<int>alll[maxn][maxn];
vector<pair<int,int>>allq[maxn][maxn];
vector<pair<int,int>>addy[maxn];
long long mainres=0;
struct fenwick{
int fn[maxn];
vector<int>allind;
void clear(){
for(auto x:allind){
while(x<maxn){
fn[x]=0;
x+=((-x)&x);
}
}
allind.shrink_to_fit();
allind.clear();
}
void add(int i,int w){
i++;
allind.push_back(i);
while(i<maxn){
fn[i]+=w;
i+=((-i)&i);
}
}
int pors(int l,int r){
int ret=0;
r++;
while(l>0){
ret-=fn[l];
l-=((-l)&l);
}
while(r>0){
ret+=fn[r];
r-=((-r)&r);
}
return ret;
}
}fen;
void solveq(int i,int j){
sort(addy[i].begin(),addy[i].end());
sort(allq[i][j].begin(),allq[i][j].end());
while((int)addy[i].size()>0&&(int)allq[i][j].size()>0){
if(addy[i].back().first>=allq[i][j].back().first){
fen.add(addy[i].back().second,1);
addy[i].pop_back();
}else{
mainres+=fen.pors(allq[i][j].back().second,j-2);
allq[i][j].pop_back();
}
}
while((int)allq[i][j].size()>0){
mainres+=fen.pors(allq[i][j].back().second,j-2);
allq[i][j].pop_back();
}
fen.clear();
}
void calq(){
for(int j=1;j<=m;j++){
vector<pair<int,int>>allv;
for(int i=n;i>=1;i--){
while((int)allv.size()>0&&allv.back().first<all[i][j]){
alll[i][j].push_back(allv.back().second);
allv.pop_back();
}
if((int)allv.size()>0){
alll[i][j].push_back(allv.back().second);
}
if((int)allv.size()>0&&allv.back().first==all[i][j]){
allv.pop_back();
}
allv.push_back(make_pair(all[i][j],i));
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
for(auto x:alll[i][j]){
if(x<=i+1){
continue;
}
dpp[x][j]=dpp[x][j-1]+1;
allq[i+1][j+1].push_back(make_pair((x-i-1),j-dpp[x][j]));
}
}
for(int j=1;j<=m;j++){
for(auto x:alll[i][j]){
if(x<=i+1){
continue;
}
dpp[x][j]=0;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
alll[i][j].clear();
}
}
for(int i=1;i<=n;i++){
vector<pair<int,int>>allv;
for(int j=1;j<=m;j++){
while((int)allv.size()>0&&allv.back().first<all[i][j]){
alll[i][j].push_back(allv.back().second);
allv.pop_back();
}
if((int)allv.size()>0){
alll[i][j].push_back(allv.back().second);
}
if((int)allv.size()>0&&allv.back().first==all[i][j]){
allv.pop_back();
}
allv.push_back(make_pair(all[i][j],j));
}
}
for(int j=1;j<=m;j++){
for(int i=n;i>=1;i--){
for(auto x:alll[i][j]){
if(x+1>=j){
continue;
}
dpp[i][x]=dpp[i+1][x]+1;
addy[i].push_back(make_pair(dpp[i][x],x));
}
solveq(i,j);
}
for(int i=n;i>=1;i--){
for(auto x:alll[i][j]){
if(x+1>=j){
continue;
}
dpp[i][x]=0;
}
addy[i].clear();
allq[i][j].clear();
addy[i].shrink_to_fit();
allq[i][j].shrink_to_fit();
}
}
}
void solve(){
calq();
}
long long count_rectangles(std::vector<std::vector<int> > a) {
n=a.size();
m=a[0].size();
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
all[i][j]=a[i-1][j-1];
}
}
solve();
return mainres;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
296272 KB |
Output is correct |
2 |
Correct |
114 ms |
296496 KB |
Output is correct |
3 |
Correct |
114 ms |
296528 KB |
Output is correct |
4 |
Correct |
109 ms |
296540 KB |
Output is correct |
5 |
Correct |
111 ms |
296528 KB |
Output is correct |
6 |
Correct |
112 ms |
296472 KB |
Output is correct |
7 |
Correct |
140 ms |
296532 KB |
Output is correct |
8 |
Correct |
110 ms |
296276 KB |
Output is correct |
9 |
Correct |
112 ms |
296692 KB |
Output is correct |
10 |
Correct |
135 ms |
296564 KB |
Output is correct |
11 |
Correct |
119 ms |
296548 KB |
Output is correct |
12 |
Correct |
110 ms |
296532 KB |
Output is correct |
13 |
Correct |
112 ms |
296200 KB |
Output is correct |
14 |
Correct |
111 ms |
296392 KB |
Output is correct |
15 |
Correct |
109 ms |
296276 KB |
Output is correct |
16 |
Correct |
109 ms |
296276 KB |
Output is correct |
17 |
Correct |
107 ms |
296276 KB |
Output is correct |
18 |
Correct |
109 ms |
296148 KB |
Output is correct |
19 |
Correct |
105 ms |
296528 KB |
Output is correct |
20 |
Correct |
106 ms |
296532 KB |
Output is correct |
21 |
Correct |
116 ms |
296276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
296272 KB |
Output is correct |
2 |
Correct |
114 ms |
296496 KB |
Output is correct |
3 |
Correct |
114 ms |
296528 KB |
Output is correct |
4 |
Correct |
109 ms |
296540 KB |
Output is correct |
5 |
Correct |
111 ms |
296528 KB |
Output is correct |
6 |
Correct |
112 ms |
296472 KB |
Output is correct |
7 |
Correct |
140 ms |
296532 KB |
Output is correct |
8 |
Correct |
110 ms |
296276 KB |
Output is correct |
9 |
Correct |
112 ms |
296692 KB |
Output is correct |
10 |
Correct |
135 ms |
296564 KB |
Output is correct |
11 |
Correct |
119 ms |
296548 KB |
Output is correct |
12 |
Correct |
110 ms |
296532 KB |
Output is correct |
13 |
Correct |
112 ms |
296200 KB |
Output is correct |
14 |
Correct |
111 ms |
296392 KB |
Output is correct |
15 |
Correct |
109 ms |
296276 KB |
Output is correct |
16 |
Correct |
109 ms |
296276 KB |
Output is correct |
17 |
Correct |
107 ms |
296276 KB |
Output is correct |
18 |
Correct |
109 ms |
296148 KB |
Output is correct |
19 |
Correct |
105 ms |
296528 KB |
Output is correct |
20 |
Correct |
106 ms |
296532 KB |
Output is correct |
21 |
Correct |
116 ms |
296276 KB |
Output is correct |
22 |
Correct |
112 ms |
297508 KB |
Output is correct |
23 |
Correct |
131 ms |
297552 KB |
Output is correct |
24 |
Correct |
161 ms |
297560 KB |
Output is correct |
25 |
Correct |
138 ms |
297300 KB |
Output is correct |
26 |
Correct |
108 ms |
297368 KB |
Output is correct |
27 |
Correct |
109 ms |
297444 KB |
Output is correct |
28 |
Correct |
118 ms |
297300 KB |
Output is correct |
29 |
Correct |
182 ms |
297164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
296272 KB |
Output is correct |
2 |
Correct |
114 ms |
296496 KB |
Output is correct |
3 |
Correct |
114 ms |
296528 KB |
Output is correct |
4 |
Correct |
109 ms |
296540 KB |
Output is correct |
5 |
Correct |
111 ms |
296528 KB |
Output is correct |
6 |
Correct |
112 ms |
296472 KB |
Output is correct |
7 |
Correct |
140 ms |
296532 KB |
Output is correct |
8 |
Correct |
110 ms |
296276 KB |
Output is correct |
9 |
Correct |
112 ms |
296692 KB |
Output is correct |
10 |
Correct |
135 ms |
296564 KB |
Output is correct |
11 |
Correct |
119 ms |
296548 KB |
Output is correct |
12 |
Correct |
110 ms |
296532 KB |
Output is correct |
13 |
Correct |
112 ms |
296200 KB |
Output is correct |
14 |
Correct |
111 ms |
296392 KB |
Output is correct |
15 |
Correct |
109 ms |
296276 KB |
Output is correct |
16 |
Correct |
109 ms |
296276 KB |
Output is correct |
17 |
Correct |
112 ms |
297508 KB |
Output is correct |
18 |
Correct |
131 ms |
297552 KB |
Output is correct |
19 |
Correct |
161 ms |
297560 KB |
Output is correct |
20 |
Correct |
138 ms |
297300 KB |
Output is correct |
21 |
Correct |
108 ms |
297368 KB |
Output is correct |
22 |
Correct |
109 ms |
297444 KB |
Output is correct |
23 |
Correct |
118 ms |
297300 KB |
Output is correct |
24 |
Correct |
182 ms |
297164 KB |
Output is correct |
25 |
Correct |
107 ms |
296276 KB |
Output is correct |
26 |
Correct |
109 ms |
296148 KB |
Output is correct |
27 |
Correct |
105 ms |
296528 KB |
Output is correct |
28 |
Correct |
106 ms |
296532 KB |
Output is correct |
29 |
Correct |
116 ms |
296276 KB |
Output is correct |
30 |
Correct |
112 ms |
301136 KB |
Output is correct |
31 |
Correct |
109 ms |
301076 KB |
Output is correct |
32 |
Correct |
118 ms |
301140 KB |
Output is correct |
33 |
Correct |
112 ms |
300116 KB |
Output is correct |
34 |
Correct |
119 ms |
300624 KB |
Output is correct |
35 |
Correct |
127 ms |
300624 KB |
Output is correct |
36 |
Correct |
123 ms |
300572 KB |
Output is correct |
37 |
Correct |
119 ms |
300508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
296272 KB |
Output is correct |
2 |
Correct |
114 ms |
296496 KB |
Output is correct |
3 |
Correct |
114 ms |
296528 KB |
Output is correct |
4 |
Correct |
109 ms |
296540 KB |
Output is correct |
5 |
Correct |
111 ms |
296528 KB |
Output is correct |
6 |
Correct |
112 ms |
296472 KB |
Output is correct |
7 |
Correct |
140 ms |
296532 KB |
Output is correct |
8 |
Correct |
110 ms |
296276 KB |
Output is correct |
9 |
Correct |
112 ms |
296692 KB |
Output is correct |
10 |
Correct |
135 ms |
296564 KB |
Output is correct |
11 |
Correct |
119 ms |
296548 KB |
Output is correct |
12 |
Correct |
110 ms |
296532 KB |
Output is correct |
13 |
Correct |
112 ms |
296200 KB |
Output is correct |
14 |
Correct |
111 ms |
296392 KB |
Output is correct |
15 |
Correct |
109 ms |
296276 KB |
Output is correct |
16 |
Correct |
109 ms |
296276 KB |
Output is correct |
17 |
Correct |
112 ms |
297508 KB |
Output is correct |
18 |
Correct |
131 ms |
297552 KB |
Output is correct |
19 |
Correct |
161 ms |
297560 KB |
Output is correct |
20 |
Correct |
138 ms |
297300 KB |
Output is correct |
21 |
Correct |
108 ms |
297368 KB |
Output is correct |
22 |
Correct |
109 ms |
297444 KB |
Output is correct |
23 |
Correct |
118 ms |
297300 KB |
Output is correct |
24 |
Correct |
182 ms |
297164 KB |
Output is correct |
25 |
Correct |
112 ms |
301136 KB |
Output is correct |
26 |
Correct |
109 ms |
301076 KB |
Output is correct |
27 |
Correct |
118 ms |
301140 KB |
Output is correct |
28 |
Correct |
112 ms |
300116 KB |
Output is correct |
29 |
Correct |
119 ms |
300624 KB |
Output is correct |
30 |
Correct |
127 ms |
300624 KB |
Output is correct |
31 |
Correct |
123 ms |
300572 KB |
Output is correct |
32 |
Correct |
119 ms |
300508 KB |
Output is correct |
33 |
Correct |
107 ms |
296276 KB |
Output is correct |
34 |
Correct |
109 ms |
296148 KB |
Output is correct |
35 |
Correct |
105 ms |
296528 KB |
Output is correct |
36 |
Correct |
106 ms |
296532 KB |
Output is correct |
37 |
Correct |
116 ms |
296276 KB |
Output is correct |
38 |
Correct |
191 ms |
333140 KB |
Output is correct |
39 |
Correct |
192 ms |
330740 KB |
Output is correct |
40 |
Correct |
181 ms |
330580 KB |
Output is correct |
41 |
Correct |
177 ms |
328276 KB |
Output is correct |
42 |
Correct |
209 ms |
343116 KB |
Output is correct |
43 |
Correct |
220 ms |
342964 KB |
Output is correct |
44 |
Correct |
212 ms |
343124 KB |
Output is correct |
45 |
Correct |
210 ms |
340056 KB |
Output is correct |
46 |
Correct |
191 ms |
328788 KB |
Output is correct |
47 |
Correct |
203 ms |
330132 KB |
Output is correct |
48 |
Correct |
255 ms |
335184 KB |
Output is correct |
49 |
Correct |
261 ms |
335440 KB |
Output is correct |
50 |
Correct |
184 ms |
318548 KB |
Output is correct |
51 |
Correct |
188 ms |
315728 KB |
Output is correct |
52 |
Correct |
256 ms |
334672 KB |
Output is correct |
53 |
Correct |
289 ms |
334708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
296856 KB |
Output is correct |
2 |
Correct |
104 ms |
296788 KB |
Output is correct |
3 |
Correct |
106 ms |
296656 KB |
Output is correct |
4 |
Correct |
107 ms |
296348 KB |
Output is correct |
5 |
Correct |
115 ms |
296784 KB |
Output is correct |
6 |
Correct |
106 ms |
296704 KB |
Output is correct |
7 |
Correct |
112 ms |
296784 KB |
Output is correct |
8 |
Correct |
102 ms |
296772 KB |
Output is correct |
9 |
Correct |
104 ms |
296788 KB |
Output is correct |
10 |
Correct |
101 ms |
296452 KB |
Output is correct |
11 |
Correct |
106 ms |
296492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
296276 KB |
Output is correct |
2 |
Correct |
109 ms |
296148 KB |
Output is correct |
3 |
Correct |
105 ms |
296528 KB |
Output is correct |
4 |
Correct |
106 ms |
296532 KB |
Output is correct |
5 |
Correct |
116 ms |
296276 KB |
Output is correct |
6 |
Correct |
102 ms |
296384 KB |
Output is correct |
7 |
Correct |
753 ms |
455252 KB |
Output is correct |
8 |
Correct |
1584 ms |
637364 KB |
Output is correct |
9 |
Correct |
1593 ms |
639056 KB |
Output is correct |
10 |
Correct |
1567 ms |
639060 KB |
Output is correct |
11 |
Correct |
419 ms |
429504 KB |
Output is correct |
12 |
Correct |
737 ms |
550476 KB |
Output is correct |
13 |
Correct |
774 ms |
565620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
116 ms |
296272 KB |
Output is correct |
2 |
Correct |
114 ms |
296496 KB |
Output is correct |
3 |
Correct |
114 ms |
296528 KB |
Output is correct |
4 |
Correct |
109 ms |
296540 KB |
Output is correct |
5 |
Correct |
111 ms |
296528 KB |
Output is correct |
6 |
Correct |
112 ms |
296472 KB |
Output is correct |
7 |
Correct |
140 ms |
296532 KB |
Output is correct |
8 |
Correct |
110 ms |
296276 KB |
Output is correct |
9 |
Correct |
112 ms |
296692 KB |
Output is correct |
10 |
Correct |
135 ms |
296564 KB |
Output is correct |
11 |
Correct |
119 ms |
296548 KB |
Output is correct |
12 |
Correct |
110 ms |
296532 KB |
Output is correct |
13 |
Correct |
112 ms |
296200 KB |
Output is correct |
14 |
Correct |
111 ms |
296392 KB |
Output is correct |
15 |
Correct |
109 ms |
296276 KB |
Output is correct |
16 |
Correct |
109 ms |
296276 KB |
Output is correct |
17 |
Correct |
112 ms |
297508 KB |
Output is correct |
18 |
Correct |
131 ms |
297552 KB |
Output is correct |
19 |
Correct |
161 ms |
297560 KB |
Output is correct |
20 |
Correct |
138 ms |
297300 KB |
Output is correct |
21 |
Correct |
108 ms |
297368 KB |
Output is correct |
22 |
Correct |
109 ms |
297444 KB |
Output is correct |
23 |
Correct |
118 ms |
297300 KB |
Output is correct |
24 |
Correct |
182 ms |
297164 KB |
Output is correct |
25 |
Correct |
112 ms |
301136 KB |
Output is correct |
26 |
Correct |
109 ms |
301076 KB |
Output is correct |
27 |
Correct |
118 ms |
301140 KB |
Output is correct |
28 |
Correct |
112 ms |
300116 KB |
Output is correct |
29 |
Correct |
119 ms |
300624 KB |
Output is correct |
30 |
Correct |
127 ms |
300624 KB |
Output is correct |
31 |
Correct |
123 ms |
300572 KB |
Output is correct |
32 |
Correct |
119 ms |
300508 KB |
Output is correct |
33 |
Correct |
191 ms |
333140 KB |
Output is correct |
34 |
Correct |
192 ms |
330740 KB |
Output is correct |
35 |
Correct |
181 ms |
330580 KB |
Output is correct |
36 |
Correct |
177 ms |
328276 KB |
Output is correct |
37 |
Correct |
209 ms |
343116 KB |
Output is correct |
38 |
Correct |
220 ms |
342964 KB |
Output is correct |
39 |
Correct |
212 ms |
343124 KB |
Output is correct |
40 |
Correct |
210 ms |
340056 KB |
Output is correct |
41 |
Correct |
191 ms |
328788 KB |
Output is correct |
42 |
Correct |
203 ms |
330132 KB |
Output is correct |
43 |
Correct |
255 ms |
335184 KB |
Output is correct |
44 |
Correct |
261 ms |
335440 KB |
Output is correct |
45 |
Correct |
184 ms |
318548 KB |
Output is correct |
46 |
Correct |
188 ms |
315728 KB |
Output is correct |
47 |
Correct |
256 ms |
334672 KB |
Output is correct |
48 |
Correct |
289 ms |
334708 KB |
Output is correct |
49 |
Correct |
108 ms |
296856 KB |
Output is correct |
50 |
Correct |
104 ms |
296788 KB |
Output is correct |
51 |
Correct |
106 ms |
296656 KB |
Output is correct |
52 |
Correct |
107 ms |
296348 KB |
Output is correct |
53 |
Correct |
115 ms |
296784 KB |
Output is correct |
54 |
Correct |
106 ms |
296704 KB |
Output is correct |
55 |
Correct |
112 ms |
296784 KB |
Output is correct |
56 |
Correct |
102 ms |
296772 KB |
Output is correct |
57 |
Correct |
104 ms |
296788 KB |
Output is correct |
58 |
Correct |
101 ms |
296452 KB |
Output is correct |
59 |
Correct |
106 ms |
296492 KB |
Output is correct |
60 |
Correct |
102 ms |
296384 KB |
Output is correct |
61 |
Correct |
753 ms |
455252 KB |
Output is correct |
62 |
Correct |
1584 ms |
637364 KB |
Output is correct |
63 |
Correct |
1593 ms |
639056 KB |
Output is correct |
64 |
Correct |
1567 ms |
639060 KB |
Output is correct |
65 |
Correct |
419 ms |
429504 KB |
Output is correct |
66 |
Correct |
737 ms |
550476 KB |
Output is correct |
67 |
Correct |
774 ms |
565620 KB |
Output is correct |
68 |
Correct |
107 ms |
296276 KB |
Output is correct |
69 |
Correct |
109 ms |
296148 KB |
Output is correct |
70 |
Correct |
105 ms |
296528 KB |
Output is correct |
71 |
Correct |
106 ms |
296532 KB |
Output is correct |
72 |
Correct |
116 ms |
296276 KB |
Output is correct |
73 |
Correct |
1577 ms |
730340 KB |
Output is correct |
74 |
Correct |
1543 ms |
673872 KB |
Output is correct |
75 |
Correct |
1159 ms |
655844 KB |
Output is correct |
76 |
Correct |
1087 ms |
633424 KB |
Output is correct |
77 |
Correct |
1572 ms |
873568 KB |
Output is correct |
78 |
Correct |
1656 ms |
566364 KB |
Output is correct |
79 |
Correct |
1755 ms |
607336 KB |
Output is correct |
80 |
Correct |
2788 ms |
746324 KB |
Output is correct |
81 |
Correct |
1640 ms |
582260 KB |
Output is correct |
82 |
Correct |
2309 ms |
687932 KB |
Output is correct |
83 |
Correct |
2906 ms |
773400 KB |
Output is correct |
84 |
Correct |
1602 ms |
569044 KB |
Output is correct |
85 |
Correct |
2705 ms |
764396 KB |
Output is correct |
86 |
Correct |
2696 ms |
751108 KB |
Output is correct |
87 |
Correct |
981 ms |
658136 KB |
Output is correct |
88 |
Correct |
1562 ms |
873160 KB |
Output is correct |
89 |
Correct |
1564 ms |
873552 KB |
Output is correct |
90 |
Correct |
1586 ms |
873636 KB |
Output is correct |