#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
set<int>pos[2501][2501],pos2[2501][2501],stb[2501][20];
set<int> operator & (const set<int>&a,const set<int>&b){
set<int>c;
if(a.size()<b.size()){
for(auto i:a)
if(b.count(i))
c.insert(i);
} else for(auto i:b)
if(a.count(i))
c.insert(i);
return c;
}
vector<array<int,4>> ops;
int CC;
vector<int>res;
int calc(int r1,int r2,int beg){
/*for(int i=20;i--;)
if(stb[beg][i].count(r2))
beg+=1<<i;
return beg;*/
while(stb[++beg][0].count(r2));
return beg-1;
}
int cnt(int l,int r,int col){
/*int x=31-__builtin_clz(r-l+1);
set<int>k=stb[l][x]&stb[r-(1<<x)+1][x];*/
set<int>k=pos[l][col];
for(int i=l;i++<r;)
k=k&pos[i][col];
return distance(k.begin(),k.upper_bound(res[CC++]));
}
long long count_rectangles(std::vector<std::vector<int> > a) {
int n=a.size(),m=a[0].size();
int ans=0;
for(int i=1;i<n-1;i++){
stack<int>stk;
stk.push(m-1);
for(int j=m-1;j--;){
while(stk.size()&&a[i][j]>a[i][stk.top()])
pos[i][j+1].insert(stk.top()-1),stk.pop();
if(stk.size()){
pos[i][j+1].insert(stk.top()-1);
if(a[i][stk.top()]==a[i][j])
stk.pop();
}
stk.push(j);
pos[i][j+1].erase(j);
}
}
for(int j=1;j<m-1;j++){
stack<int>stk;
stk.push(n-1);
for(int i=n-1;i--;){
while(stk.size()&&a[i][j]>a[stk.top()][j])
pos2[i+1][j].insert(stk.top()-1),stk.pop();
if(stk.size()){
pos2[i+1][j].insert(stk.top()-1);
if(a[stk.top()][j]==a[i][j])
stk.pop();
}
stk.push(i);
pos2[i+1][j].erase(i);
}
}
for(int j=1;j<m-1;j++)
for(int i=1;i<n-1;i++)
for(auto x:pos2[i][j])
ops.push_back({i,x,j,CC++});
res.resize(CC);
CC=0;
sort(ops.begin(),ops.end());
int currr=0;
for(auto[r1,r2,col,id]:ops){
if(r1-currr) {
currr=r1;
for(int i=1;i<m-1;i++)
stb[i][0]=pos2[r1][i];
for(int j=1;j<20;j++) for(int i=1;i+(1<<j)<m;i++)
stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
}
res[id]=calc(r1,r2,col);
}
ops.clear();
for(int col=1;col<m-1;col++){
for(int i=1;i<n-1;i++)
stb[i][0]=pos[i][col];
for(int j=1;j<20;j++) for(int i=1;i+(1<<j)<n;i++)
stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
for(int row1=1;row1<n-1;row1++)
for(auto row2:pos2[row1][col])
ans+=cnt(row1,row2,col);
}
return ans;
}
Compilation message
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:82:50: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
82 | stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
| ~^~
rect.cpp:91:46: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
91 | stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
590160 KB |
Output is correct |
2 |
Correct |
162 ms |
590404 KB |
Output is correct |
3 |
Correct |
158 ms |
590252 KB |
Output is correct |
4 |
Correct |
174 ms |
590372 KB |
Output is correct |
5 |
Correct |
166 ms |
590160 KB |
Output is correct |
6 |
Correct |
174 ms |
590420 KB |
Output is correct |
7 |
Correct |
156 ms |
590160 KB |
Output is correct |
8 |
Correct |
158 ms |
590112 KB |
Output is correct |
9 |
Correct |
162 ms |
590352 KB |
Output is correct |
10 |
Correct |
162 ms |
590232 KB |
Output is correct |
11 |
Correct |
167 ms |
590252 KB |
Output is correct |
12 |
Correct |
157 ms |
590456 KB |
Output is correct |
13 |
Correct |
203 ms |
590144 KB |
Output is correct |
14 |
Correct |
172 ms |
590164 KB |
Output is correct |
15 |
Correct |
163 ms |
590164 KB |
Output is correct |
16 |
Correct |
157 ms |
590128 KB |
Output is correct |
17 |
Correct |
159 ms |
590240 KB |
Output is correct |
18 |
Correct |
154 ms |
590312 KB |
Output is correct |
19 |
Correct |
161 ms |
590260 KB |
Output is correct |
20 |
Correct |
155 ms |
590212 KB |
Output is correct |
21 |
Correct |
156 ms |
590164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
590160 KB |
Output is correct |
2 |
Correct |
162 ms |
590404 KB |
Output is correct |
3 |
Correct |
158 ms |
590252 KB |
Output is correct |
4 |
Correct |
174 ms |
590372 KB |
Output is correct |
5 |
Correct |
166 ms |
590160 KB |
Output is correct |
6 |
Correct |
174 ms |
590420 KB |
Output is correct |
7 |
Correct |
156 ms |
590160 KB |
Output is correct |
8 |
Correct |
158 ms |
590112 KB |
Output is correct |
9 |
Correct |
162 ms |
590352 KB |
Output is correct |
10 |
Correct |
162 ms |
590232 KB |
Output is correct |
11 |
Correct |
167 ms |
590252 KB |
Output is correct |
12 |
Correct |
157 ms |
590456 KB |
Output is correct |
13 |
Correct |
203 ms |
590144 KB |
Output is correct |
14 |
Correct |
172 ms |
590164 KB |
Output is correct |
15 |
Correct |
163 ms |
590164 KB |
Output is correct |
16 |
Correct |
157 ms |
590128 KB |
Output is correct |
17 |
Correct |
159 ms |
590240 KB |
Output is correct |
18 |
Correct |
154 ms |
590312 KB |
Output is correct |
19 |
Correct |
161 ms |
590260 KB |
Output is correct |
20 |
Correct |
155 ms |
590212 KB |
Output is correct |
21 |
Correct |
156 ms |
590164 KB |
Output is correct |
22 |
Correct |
164 ms |
591188 KB |
Output is correct |
23 |
Correct |
191 ms |
591124 KB |
Output is correct |
24 |
Correct |
162 ms |
591188 KB |
Output is correct |
25 |
Correct |
161 ms |
590672 KB |
Output is correct |
26 |
Correct |
162 ms |
590972 KB |
Output is correct |
27 |
Correct |
156 ms |
591048 KB |
Output is correct |
28 |
Correct |
157 ms |
590928 KB |
Output is correct |
29 |
Correct |
161 ms |
590504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
590160 KB |
Output is correct |
2 |
Correct |
162 ms |
590404 KB |
Output is correct |
3 |
Correct |
158 ms |
590252 KB |
Output is correct |
4 |
Correct |
174 ms |
590372 KB |
Output is correct |
5 |
Correct |
166 ms |
590160 KB |
Output is correct |
6 |
Correct |
174 ms |
590420 KB |
Output is correct |
7 |
Correct |
156 ms |
590160 KB |
Output is correct |
8 |
Correct |
158 ms |
590112 KB |
Output is correct |
9 |
Correct |
162 ms |
590352 KB |
Output is correct |
10 |
Correct |
162 ms |
590232 KB |
Output is correct |
11 |
Correct |
167 ms |
590252 KB |
Output is correct |
12 |
Correct |
157 ms |
590456 KB |
Output is correct |
13 |
Correct |
203 ms |
590144 KB |
Output is correct |
14 |
Correct |
172 ms |
590164 KB |
Output is correct |
15 |
Correct |
163 ms |
590164 KB |
Output is correct |
16 |
Correct |
157 ms |
590128 KB |
Output is correct |
17 |
Correct |
164 ms |
591188 KB |
Output is correct |
18 |
Correct |
191 ms |
591124 KB |
Output is correct |
19 |
Correct |
162 ms |
591188 KB |
Output is correct |
20 |
Correct |
161 ms |
590672 KB |
Output is correct |
21 |
Correct |
162 ms |
590972 KB |
Output is correct |
22 |
Correct |
156 ms |
591048 KB |
Output is correct |
23 |
Correct |
157 ms |
590928 KB |
Output is correct |
24 |
Correct |
161 ms |
590504 KB |
Output is correct |
25 |
Correct |
159 ms |
590240 KB |
Output is correct |
26 |
Correct |
154 ms |
590312 KB |
Output is correct |
27 |
Correct |
161 ms |
590260 KB |
Output is correct |
28 |
Correct |
155 ms |
590212 KB |
Output is correct |
29 |
Correct |
156 ms |
590164 KB |
Output is correct |
30 |
Correct |
286 ms |
595656 KB |
Output is correct |
31 |
Correct |
268 ms |
595676 KB |
Output is correct |
32 |
Correct |
271 ms |
595656 KB |
Output is correct |
33 |
Correct |
189 ms |
592464 KB |
Output is correct |
34 |
Correct |
181 ms |
595408 KB |
Output is correct |
35 |
Correct |
190 ms |
595508 KB |
Output is correct |
36 |
Correct |
176 ms |
595168 KB |
Output is correct |
37 |
Correct |
220 ms |
595192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
590160 KB |
Output is correct |
2 |
Correct |
162 ms |
590404 KB |
Output is correct |
3 |
Correct |
158 ms |
590252 KB |
Output is correct |
4 |
Correct |
174 ms |
590372 KB |
Output is correct |
5 |
Correct |
166 ms |
590160 KB |
Output is correct |
6 |
Correct |
174 ms |
590420 KB |
Output is correct |
7 |
Correct |
156 ms |
590160 KB |
Output is correct |
8 |
Correct |
158 ms |
590112 KB |
Output is correct |
9 |
Correct |
162 ms |
590352 KB |
Output is correct |
10 |
Correct |
162 ms |
590232 KB |
Output is correct |
11 |
Correct |
167 ms |
590252 KB |
Output is correct |
12 |
Correct |
157 ms |
590456 KB |
Output is correct |
13 |
Correct |
203 ms |
590144 KB |
Output is correct |
14 |
Correct |
172 ms |
590164 KB |
Output is correct |
15 |
Correct |
163 ms |
590164 KB |
Output is correct |
16 |
Correct |
157 ms |
590128 KB |
Output is correct |
17 |
Correct |
164 ms |
591188 KB |
Output is correct |
18 |
Correct |
191 ms |
591124 KB |
Output is correct |
19 |
Correct |
162 ms |
591188 KB |
Output is correct |
20 |
Correct |
161 ms |
590672 KB |
Output is correct |
21 |
Correct |
162 ms |
590972 KB |
Output is correct |
22 |
Correct |
156 ms |
591048 KB |
Output is correct |
23 |
Correct |
157 ms |
590928 KB |
Output is correct |
24 |
Correct |
161 ms |
590504 KB |
Output is correct |
25 |
Correct |
286 ms |
595656 KB |
Output is correct |
26 |
Correct |
268 ms |
595676 KB |
Output is correct |
27 |
Correct |
271 ms |
595656 KB |
Output is correct |
28 |
Correct |
189 ms |
592464 KB |
Output is correct |
29 |
Correct |
181 ms |
595408 KB |
Output is correct |
30 |
Correct |
190 ms |
595508 KB |
Output is correct |
31 |
Correct |
176 ms |
595168 KB |
Output is correct |
32 |
Correct |
220 ms |
595192 KB |
Output is correct |
33 |
Correct |
159 ms |
590240 KB |
Output is correct |
34 |
Correct |
154 ms |
590312 KB |
Output is correct |
35 |
Correct |
161 ms |
590260 KB |
Output is correct |
36 |
Correct |
155 ms |
590212 KB |
Output is correct |
37 |
Correct |
156 ms |
590164 KB |
Output is correct |
38 |
Correct |
572 ms |
623896 KB |
Output is correct |
39 |
Correct |
533 ms |
623768 KB |
Output is correct |
40 |
Correct |
526 ms |
623556 KB |
Output is correct |
41 |
Correct |
2288 ms |
623764 KB |
Output is correct |
42 |
Correct |
4712 ms |
651432 KB |
Output is correct |
43 |
Correct |
4629 ms |
651512 KB |
Output is correct |
44 |
Correct |
4510 ms |
651852 KB |
Output is correct |
45 |
Correct |
4147 ms |
647940 KB |
Output is correct |
46 |
Correct |
298 ms |
608716 KB |
Output is correct |
47 |
Correct |
322 ms |
616928 KB |
Output is correct |
48 |
Correct |
476 ms |
649044 KB |
Output is correct |
49 |
Correct |
489 ms |
650680 KB |
Output is correct |
50 |
Correct |
348 ms |
620600 KB |
Output is correct |
51 |
Correct |
349 ms |
620488 KB |
Output is correct |
52 |
Correct |
532 ms |
647104 KB |
Output is correct |
53 |
Correct |
562 ms |
647144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
591696 KB |
Output is correct |
2 |
Correct |
171 ms |
591444 KB |
Output is correct |
3 |
Correct |
165 ms |
590360 KB |
Output is correct |
4 |
Correct |
159 ms |
590252 KB |
Output is correct |
5 |
Correct |
159 ms |
590416 KB |
Output is correct |
6 |
Correct |
205 ms |
590504 KB |
Output is correct |
7 |
Correct |
177 ms |
590672 KB |
Output is correct |
8 |
Correct |
158 ms |
590416 KB |
Output is correct |
9 |
Correct |
180 ms |
590640 KB |
Output is correct |
10 |
Correct |
165 ms |
590144 KB |
Output is correct |
11 |
Correct |
158 ms |
590244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
590240 KB |
Output is correct |
2 |
Correct |
154 ms |
590312 KB |
Output is correct |
3 |
Correct |
161 ms |
590260 KB |
Output is correct |
4 |
Correct |
155 ms |
590212 KB |
Output is correct |
5 |
Correct |
156 ms |
590164 KB |
Output is correct |
6 |
Correct |
159 ms |
590320 KB |
Output is correct |
7 |
Correct |
1037 ms |
696196 KB |
Output is correct |
8 |
Correct |
2189 ms |
823720 KB |
Output is correct |
9 |
Correct |
2196 ms |
824904 KB |
Output is correct |
10 |
Correct |
2242 ms |
824816 KB |
Output is correct |
11 |
Correct |
502 ms |
617812 KB |
Output is correct |
12 |
Correct |
903 ms |
642132 KB |
Output is correct |
13 |
Correct |
990 ms |
645456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
590160 KB |
Output is correct |
2 |
Correct |
162 ms |
590404 KB |
Output is correct |
3 |
Correct |
158 ms |
590252 KB |
Output is correct |
4 |
Correct |
174 ms |
590372 KB |
Output is correct |
5 |
Correct |
166 ms |
590160 KB |
Output is correct |
6 |
Correct |
174 ms |
590420 KB |
Output is correct |
7 |
Correct |
156 ms |
590160 KB |
Output is correct |
8 |
Correct |
158 ms |
590112 KB |
Output is correct |
9 |
Correct |
162 ms |
590352 KB |
Output is correct |
10 |
Correct |
162 ms |
590232 KB |
Output is correct |
11 |
Correct |
167 ms |
590252 KB |
Output is correct |
12 |
Correct |
157 ms |
590456 KB |
Output is correct |
13 |
Correct |
203 ms |
590144 KB |
Output is correct |
14 |
Correct |
172 ms |
590164 KB |
Output is correct |
15 |
Correct |
163 ms |
590164 KB |
Output is correct |
16 |
Correct |
157 ms |
590128 KB |
Output is correct |
17 |
Correct |
164 ms |
591188 KB |
Output is correct |
18 |
Correct |
191 ms |
591124 KB |
Output is correct |
19 |
Correct |
162 ms |
591188 KB |
Output is correct |
20 |
Correct |
161 ms |
590672 KB |
Output is correct |
21 |
Correct |
162 ms |
590972 KB |
Output is correct |
22 |
Correct |
156 ms |
591048 KB |
Output is correct |
23 |
Correct |
157 ms |
590928 KB |
Output is correct |
24 |
Correct |
161 ms |
590504 KB |
Output is correct |
25 |
Correct |
286 ms |
595656 KB |
Output is correct |
26 |
Correct |
268 ms |
595676 KB |
Output is correct |
27 |
Correct |
271 ms |
595656 KB |
Output is correct |
28 |
Correct |
189 ms |
592464 KB |
Output is correct |
29 |
Correct |
181 ms |
595408 KB |
Output is correct |
30 |
Correct |
190 ms |
595508 KB |
Output is correct |
31 |
Correct |
176 ms |
595168 KB |
Output is correct |
32 |
Correct |
220 ms |
595192 KB |
Output is correct |
33 |
Correct |
572 ms |
623896 KB |
Output is correct |
34 |
Correct |
533 ms |
623768 KB |
Output is correct |
35 |
Correct |
526 ms |
623556 KB |
Output is correct |
36 |
Correct |
2288 ms |
623764 KB |
Output is correct |
37 |
Correct |
4712 ms |
651432 KB |
Output is correct |
38 |
Correct |
4629 ms |
651512 KB |
Output is correct |
39 |
Correct |
4510 ms |
651852 KB |
Output is correct |
40 |
Correct |
4147 ms |
647940 KB |
Output is correct |
41 |
Correct |
298 ms |
608716 KB |
Output is correct |
42 |
Correct |
322 ms |
616928 KB |
Output is correct |
43 |
Correct |
476 ms |
649044 KB |
Output is correct |
44 |
Correct |
489 ms |
650680 KB |
Output is correct |
45 |
Correct |
348 ms |
620600 KB |
Output is correct |
46 |
Correct |
349 ms |
620488 KB |
Output is correct |
47 |
Correct |
532 ms |
647104 KB |
Output is correct |
48 |
Correct |
562 ms |
647144 KB |
Output is correct |
49 |
Correct |
167 ms |
591696 KB |
Output is correct |
50 |
Correct |
171 ms |
591444 KB |
Output is correct |
51 |
Correct |
165 ms |
590360 KB |
Output is correct |
52 |
Correct |
159 ms |
590252 KB |
Output is correct |
53 |
Correct |
159 ms |
590416 KB |
Output is correct |
54 |
Correct |
205 ms |
590504 KB |
Output is correct |
55 |
Correct |
177 ms |
590672 KB |
Output is correct |
56 |
Correct |
158 ms |
590416 KB |
Output is correct |
57 |
Correct |
180 ms |
590640 KB |
Output is correct |
58 |
Correct |
165 ms |
590144 KB |
Output is correct |
59 |
Correct |
158 ms |
590244 KB |
Output is correct |
60 |
Correct |
159 ms |
590320 KB |
Output is correct |
61 |
Correct |
1037 ms |
696196 KB |
Output is correct |
62 |
Correct |
2189 ms |
823720 KB |
Output is correct |
63 |
Correct |
2196 ms |
824904 KB |
Output is correct |
64 |
Correct |
2242 ms |
824816 KB |
Output is correct |
65 |
Correct |
502 ms |
617812 KB |
Output is correct |
66 |
Correct |
903 ms |
642132 KB |
Output is correct |
67 |
Correct |
990 ms |
645456 KB |
Output is correct |
68 |
Correct |
159 ms |
590240 KB |
Output is correct |
69 |
Correct |
154 ms |
590312 KB |
Output is correct |
70 |
Correct |
161 ms |
590260 KB |
Output is correct |
71 |
Correct |
155 ms |
590212 KB |
Output is correct |
72 |
Correct |
156 ms |
590164 KB |
Output is correct |
73 |
Execution timed out |
5111 ms |
998404 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |