#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
set<short>pos[2500][2500],pos2[2500][2500],stb[2500][20];
set<short> operator & (const set<short>&a,const set<short>&b){
set<short>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-1;
}
int cnt(int l,int r,int col){
int x=31-__builtin_clz(r-l+1);
set<short>k=stb[l][x]&stb[r-(1<<x)+1][x];
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:78:50: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
78 | stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
| ~^~
rect.cpp:87:46: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
87 | stb[i][j]=stb[i][j-1]&stb[i+(1<<j-1)][j-1];
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
589780 KB |
Output is correct |
2 |
Correct |
167 ms |
589908 KB |
Output is correct |
3 |
Correct |
169 ms |
589796 KB |
Output is correct |
4 |
Correct |
164 ms |
590160 KB |
Output is correct |
5 |
Correct |
163 ms |
589684 KB |
Output is correct |
6 |
Correct |
164 ms |
589908 KB |
Output is correct |
7 |
Correct |
165 ms |
589652 KB |
Output is correct |
8 |
Correct |
175 ms |
589748 KB |
Output is correct |
9 |
Correct |
169 ms |
589908 KB |
Output is correct |
10 |
Correct |
170 ms |
589712 KB |
Output is correct |
11 |
Correct |
182 ms |
589896 KB |
Output is correct |
12 |
Correct |
175 ms |
590012 KB |
Output is correct |
13 |
Correct |
162 ms |
589648 KB |
Output is correct |
14 |
Correct |
164 ms |
589836 KB |
Output is correct |
15 |
Correct |
167 ms |
589788 KB |
Output is correct |
16 |
Correct |
161 ms |
589676 KB |
Output is correct |
17 |
Correct |
203 ms |
589652 KB |
Output is correct |
18 |
Correct |
165 ms |
589648 KB |
Output is correct |
19 |
Correct |
162 ms |
589892 KB |
Output is correct |
20 |
Correct |
162 ms |
589808 KB |
Output is correct |
21 |
Correct |
165 ms |
589652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
589780 KB |
Output is correct |
2 |
Correct |
167 ms |
589908 KB |
Output is correct |
3 |
Correct |
169 ms |
589796 KB |
Output is correct |
4 |
Correct |
164 ms |
590160 KB |
Output is correct |
5 |
Correct |
163 ms |
589684 KB |
Output is correct |
6 |
Correct |
164 ms |
589908 KB |
Output is correct |
7 |
Correct |
165 ms |
589652 KB |
Output is correct |
8 |
Correct |
175 ms |
589748 KB |
Output is correct |
9 |
Correct |
169 ms |
589908 KB |
Output is correct |
10 |
Correct |
170 ms |
589712 KB |
Output is correct |
11 |
Correct |
182 ms |
589896 KB |
Output is correct |
12 |
Correct |
175 ms |
590012 KB |
Output is correct |
13 |
Correct |
162 ms |
589648 KB |
Output is correct |
14 |
Correct |
164 ms |
589836 KB |
Output is correct |
15 |
Correct |
167 ms |
589788 KB |
Output is correct |
16 |
Correct |
161 ms |
589676 KB |
Output is correct |
17 |
Correct |
203 ms |
589652 KB |
Output is correct |
18 |
Correct |
165 ms |
589648 KB |
Output is correct |
19 |
Correct |
162 ms |
589892 KB |
Output is correct |
20 |
Correct |
162 ms |
589808 KB |
Output is correct |
21 |
Correct |
165 ms |
589652 KB |
Output is correct |
22 |
Correct |
173 ms |
590676 KB |
Output is correct |
23 |
Correct |
183 ms |
590672 KB |
Output is correct |
24 |
Correct |
181 ms |
590704 KB |
Output is correct |
25 |
Correct |
176 ms |
590228 KB |
Output is correct |
26 |
Correct |
169 ms |
590576 KB |
Output is correct |
27 |
Correct |
164 ms |
590672 KB |
Output is correct |
28 |
Correct |
169 ms |
590508 KB |
Output is correct |
29 |
Correct |
162 ms |
590160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
589780 KB |
Output is correct |
2 |
Correct |
167 ms |
589908 KB |
Output is correct |
3 |
Correct |
169 ms |
589796 KB |
Output is correct |
4 |
Correct |
164 ms |
590160 KB |
Output is correct |
5 |
Correct |
163 ms |
589684 KB |
Output is correct |
6 |
Correct |
164 ms |
589908 KB |
Output is correct |
7 |
Correct |
165 ms |
589652 KB |
Output is correct |
8 |
Correct |
175 ms |
589748 KB |
Output is correct |
9 |
Correct |
169 ms |
589908 KB |
Output is correct |
10 |
Correct |
170 ms |
589712 KB |
Output is correct |
11 |
Correct |
182 ms |
589896 KB |
Output is correct |
12 |
Correct |
175 ms |
590012 KB |
Output is correct |
13 |
Correct |
162 ms |
589648 KB |
Output is correct |
14 |
Correct |
164 ms |
589836 KB |
Output is correct |
15 |
Correct |
167 ms |
589788 KB |
Output is correct |
16 |
Correct |
161 ms |
589676 KB |
Output is correct |
17 |
Correct |
173 ms |
590676 KB |
Output is correct |
18 |
Correct |
183 ms |
590672 KB |
Output is correct |
19 |
Correct |
181 ms |
590704 KB |
Output is correct |
20 |
Correct |
176 ms |
590228 KB |
Output is correct |
21 |
Correct |
169 ms |
590576 KB |
Output is correct |
22 |
Correct |
164 ms |
590672 KB |
Output is correct |
23 |
Correct |
169 ms |
590508 KB |
Output is correct |
24 |
Correct |
162 ms |
590160 KB |
Output is correct |
25 |
Correct |
203 ms |
589652 KB |
Output is correct |
26 |
Correct |
165 ms |
589648 KB |
Output is correct |
27 |
Correct |
162 ms |
589892 KB |
Output is correct |
28 |
Correct |
162 ms |
589808 KB |
Output is correct |
29 |
Correct |
165 ms |
589652 KB |
Output is correct |
30 |
Correct |
188 ms |
595144 KB |
Output is correct |
31 |
Correct |
198 ms |
595228 KB |
Output is correct |
32 |
Correct |
184 ms |
595148 KB |
Output is correct |
33 |
Correct |
176 ms |
591948 KB |
Output is correct |
34 |
Correct |
197 ms |
594868 KB |
Output is correct |
35 |
Correct |
195 ms |
595004 KB |
Output is correct |
36 |
Correct |
201 ms |
594888 KB |
Output is correct |
37 |
Correct |
206 ms |
594640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
589780 KB |
Output is correct |
2 |
Correct |
167 ms |
589908 KB |
Output is correct |
3 |
Correct |
169 ms |
589796 KB |
Output is correct |
4 |
Correct |
164 ms |
590160 KB |
Output is correct |
5 |
Correct |
163 ms |
589684 KB |
Output is correct |
6 |
Correct |
164 ms |
589908 KB |
Output is correct |
7 |
Correct |
165 ms |
589652 KB |
Output is correct |
8 |
Correct |
175 ms |
589748 KB |
Output is correct |
9 |
Correct |
169 ms |
589908 KB |
Output is correct |
10 |
Correct |
170 ms |
589712 KB |
Output is correct |
11 |
Correct |
182 ms |
589896 KB |
Output is correct |
12 |
Correct |
175 ms |
590012 KB |
Output is correct |
13 |
Correct |
162 ms |
589648 KB |
Output is correct |
14 |
Correct |
164 ms |
589836 KB |
Output is correct |
15 |
Correct |
167 ms |
589788 KB |
Output is correct |
16 |
Correct |
161 ms |
589676 KB |
Output is correct |
17 |
Correct |
173 ms |
590676 KB |
Output is correct |
18 |
Correct |
183 ms |
590672 KB |
Output is correct |
19 |
Correct |
181 ms |
590704 KB |
Output is correct |
20 |
Correct |
176 ms |
590228 KB |
Output is correct |
21 |
Correct |
169 ms |
590576 KB |
Output is correct |
22 |
Correct |
164 ms |
590672 KB |
Output is correct |
23 |
Correct |
169 ms |
590508 KB |
Output is correct |
24 |
Correct |
162 ms |
590160 KB |
Output is correct |
25 |
Correct |
188 ms |
595144 KB |
Output is correct |
26 |
Correct |
198 ms |
595228 KB |
Output is correct |
27 |
Correct |
184 ms |
595148 KB |
Output is correct |
28 |
Correct |
176 ms |
591948 KB |
Output is correct |
29 |
Correct |
197 ms |
594868 KB |
Output is correct |
30 |
Correct |
195 ms |
595004 KB |
Output is correct |
31 |
Correct |
201 ms |
594888 KB |
Output is correct |
32 |
Correct |
206 ms |
594640 KB |
Output is correct |
33 |
Correct |
203 ms |
589652 KB |
Output is correct |
34 |
Correct |
165 ms |
589648 KB |
Output is correct |
35 |
Correct |
162 ms |
589892 KB |
Output is correct |
36 |
Correct |
162 ms |
589808 KB |
Output is correct |
37 |
Correct |
165 ms |
589652 KB |
Output is correct |
38 |
Correct |
323 ms |
624072 KB |
Output is correct |
39 |
Correct |
330 ms |
624316 KB |
Output is correct |
40 |
Correct |
334 ms |
624176 KB |
Output is correct |
41 |
Correct |
359 ms |
624196 KB |
Output is correct |
42 |
Correct |
533 ms |
651940 KB |
Output is correct |
43 |
Correct |
533 ms |
652244 KB |
Output is correct |
44 |
Correct |
567 ms |
652192 KB |
Output is correct |
45 |
Correct |
538 ms |
648304 KB |
Output is correct |
46 |
Correct |
315 ms |
608456 KB |
Output is correct |
47 |
Correct |
348 ms |
616512 KB |
Output is correct |
48 |
Correct |
505 ms |
649024 KB |
Output is correct |
49 |
Correct |
479 ms |
651268 KB |
Output is correct |
50 |
Correct |
346 ms |
620648 KB |
Output is correct |
51 |
Correct |
313 ms |
620348 KB |
Output is correct |
52 |
Correct |
495 ms |
646976 KB |
Output is correct |
53 |
Correct |
642 ms |
647632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
591364 KB |
Output is correct |
2 |
Correct |
185 ms |
591192 KB |
Output is correct |
3 |
Correct |
164 ms |
589756 KB |
Output is correct |
4 |
Correct |
182 ms |
589652 KB |
Output is correct |
5 |
Correct |
190 ms |
590032 KB |
Output is correct |
6 |
Correct |
186 ms |
590180 KB |
Output is correct |
7 |
Correct |
166 ms |
589988 KB |
Output is correct |
8 |
Correct |
176 ms |
590064 KB |
Output is correct |
9 |
Correct |
167 ms |
590164 KB |
Output is correct |
10 |
Correct |
166 ms |
589712 KB |
Output is correct |
11 |
Correct |
166 ms |
589908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
589652 KB |
Output is correct |
2 |
Correct |
165 ms |
589648 KB |
Output is correct |
3 |
Correct |
162 ms |
589892 KB |
Output is correct |
4 |
Correct |
162 ms |
589808 KB |
Output is correct |
5 |
Correct |
165 ms |
589652 KB |
Output is correct |
6 |
Correct |
194 ms |
589648 KB |
Output is correct |
7 |
Correct |
1077 ms |
700348 KB |
Output is correct |
8 |
Correct |
2188 ms |
826824 KB |
Output is correct |
9 |
Correct |
2300 ms |
827616 KB |
Output is correct |
10 |
Correct |
2315 ms |
827784 KB |
Output is correct |
11 |
Correct |
505 ms |
618812 KB |
Output is correct |
12 |
Correct |
974 ms |
644864 KB |
Output is correct |
13 |
Correct |
994 ms |
648432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
589780 KB |
Output is correct |
2 |
Correct |
167 ms |
589908 KB |
Output is correct |
3 |
Correct |
169 ms |
589796 KB |
Output is correct |
4 |
Correct |
164 ms |
590160 KB |
Output is correct |
5 |
Correct |
163 ms |
589684 KB |
Output is correct |
6 |
Correct |
164 ms |
589908 KB |
Output is correct |
7 |
Correct |
165 ms |
589652 KB |
Output is correct |
8 |
Correct |
175 ms |
589748 KB |
Output is correct |
9 |
Correct |
169 ms |
589908 KB |
Output is correct |
10 |
Correct |
170 ms |
589712 KB |
Output is correct |
11 |
Correct |
182 ms |
589896 KB |
Output is correct |
12 |
Correct |
175 ms |
590012 KB |
Output is correct |
13 |
Correct |
162 ms |
589648 KB |
Output is correct |
14 |
Correct |
164 ms |
589836 KB |
Output is correct |
15 |
Correct |
167 ms |
589788 KB |
Output is correct |
16 |
Correct |
161 ms |
589676 KB |
Output is correct |
17 |
Correct |
173 ms |
590676 KB |
Output is correct |
18 |
Correct |
183 ms |
590672 KB |
Output is correct |
19 |
Correct |
181 ms |
590704 KB |
Output is correct |
20 |
Correct |
176 ms |
590228 KB |
Output is correct |
21 |
Correct |
169 ms |
590576 KB |
Output is correct |
22 |
Correct |
164 ms |
590672 KB |
Output is correct |
23 |
Correct |
169 ms |
590508 KB |
Output is correct |
24 |
Correct |
162 ms |
590160 KB |
Output is correct |
25 |
Correct |
188 ms |
595144 KB |
Output is correct |
26 |
Correct |
198 ms |
595228 KB |
Output is correct |
27 |
Correct |
184 ms |
595148 KB |
Output is correct |
28 |
Correct |
176 ms |
591948 KB |
Output is correct |
29 |
Correct |
197 ms |
594868 KB |
Output is correct |
30 |
Correct |
195 ms |
595004 KB |
Output is correct |
31 |
Correct |
201 ms |
594888 KB |
Output is correct |
32 |
Correct |
206 ms |
594640 KB |
Output is correct |
33 |
Correct |
323 ms |
624072 KB |
Output is correct |
34 |
Correct |
330 ms |
624316 KB |
Output is correct |
35 |
Correct |
334 ms |
624176 KB |
Output is correct |
36 |
Correct |
359 ms |
624196 KB |
Output is correct |
37 |
Correct |
533 ms |
651940 KB |
Output is correct |
38 |
Correct |
533 ms |
652244 KB |
Output is correct |
39 |
Correct |
567 ms |
652192 KB |
Output is correct |
40 |
Correct |
538 ms |
648304 KB |
Output is correct |
41 |
Correct |
315 ms |
608456 KB |
Output is correct |
42 |
Correct |
348 ms |
616512 KB |
Output is correct |
43 |
Correct |
505 ms |
649024 KB |
Output is correct |
44 |
Correct |
479 ms |
651268 KB |
Output is correct |
45 |
Correct |
346 ms |
620648 KB |
Output is correct |
46 |
Correct |
313 ms |
620348 KB |
Output is correct |
47 |
Correct |
495 ms |
646976 KB |
Output is correct |
48 |
Correct |
642 ms |
647632 KB |
Output is correct |
49 |
Correct |
204 ms |
591364 KB |
Output is correct |
50 |
Correct |
185 ms |
591192 KB |
Output is correct |
51 |
Correct |
164 ms |
589756 KB |
Output is correct |
52 |
Correct |
182 ms |
589652 KB |
Output is correct |
53 |
Correct |
190 ms |
590032 KB |
Output is correct |
54 |
Correct |
186 ms |
590180 KB |
Output is correct |
55 |
Correct |
166 ms |
589988 KB |
Output is correct |
56 |
Correct |
176 ms |
590064 KB |
Output is correct |
57 |
Correct |
167 ms |
590164 KB |
Output is correct |
58 |
Correct |
166 ms |
589712 KB |
Output is correct |
59 |
Correct |
166 ms |
589908 KB |
Output is correct |
60 |
Correct |
194 ms |
589648 KB |
Output is correct |
61 |
Correct |
1077 ms |
700348 KB |
Output is correct |
62 |
Correct |
2188 ms |
826824 KB |
Output is correct |
63 |
Correct |
2300 ms |
827616 KB |
Output is correct |
64 |
Correct |
2315 ms |
827784 KB |
Output is correct |
65 |
Correct |
505 ms |
618812 KB |
Output is correct |
66 |
Correct |
974 ms |
644864 KB |
Output is correct |
67 |
Correct |
994 ms |
648432 KB |
Output is correct |
68 |
Correct |
203 ms |
589652 KB |
Output is correct |
69 |
Correct |
165 ms |
589648 KB |
Output is correct |
70 |
Correct |
162 ms |
589892 KB |
Output is correct |
71 |
Correct |
162 ms |
589808 KB |
Output is correct |
72 |
Correct |
165 ms |
589652 KB |
Output is correct |
73 |
Correct |
2553 ms |
1033200 KB |
Output is correct |
74 |
Correct |
2875 ms |
1033292 KB |
Output is correct |
75 |
Correct |
2532 ms |
1032488 KB |
Output is correct |
76 |
Correct |
2782 ms |
1029248 KB |
Output is correct |
77 |
Runtime error |
781 ms |
1048576 KB |
Execution killed with signal 9 |
78 |
Halted |
0 ms |
0 KB |
- |