#include "rect.h"
#include <bits/stdc++.h>
//#include "grader.cpp"
using namespace std;
typedef long long ll;
int v[2505][2505],n,m,up[2505][2505],down[2505][2505],rmq[15][2505],loga[2505],dlin[2505],ulin[2505],lins[2505],valmax[2505];
vector<int> who[2505];
int good[2505],last[2505];
int aib[2505];
int getmax(int l,int r)
{
int lg=loga[r-l+1];
return max(rmq[lg][l],rmq[lg][r-(1<<lg)+1]);
}
int lsb(int x)
{
return x&(-x);
}
void update(int poz,int val)
{
for(int i=poz;i<=n;i+=lsb(i))
aib[i]+=val;
}
int suma(int poz)
{
int rez=0;
for(int i=poz;i>=1;i-=lsb(i))
rez+=aib[i];
return rez;
}
long long count_rectangles(vector<vector<int>> A)
{
ll ans=0;
n=A.size();
m=A[0].size();
for(int i=1;i<=2500;i++)
{
for(int bit=0;bit<=13;bit++)
if((1<<bit)<=i)
loga[i]=bit;
}
if(n<=2||m<=2)
return 0;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
v[i][j]=A[i-1][j-1];
for(int j=1;j<=m;j++)
{
for(int i=n;i>=1;i--)
{
rmq[0][i]=v[i][j];
for(int k=1;k<=13;k++)
{
rmq[k][i]=rmq[k-1][i];
int poz=i+(1<<(k-1));
if(poz<=n)
rmq[k][i]=max(rmq[k][i],rmq[k-1][poz]);
}
}
for(int i=2;i<n;i++)
{
up[i][j]=1e9;
down[i][j]=0;
int st=i;
int dr=n-1;
while(st<=dr)
{
int mij=(st+dr)/2;
if(getmax(i,mij)<v[i-1][j])
{
down[i][j]=mij;
st=mij+1;
}
else
dr=mij-1;
}
st=2;
dr=i;
while(st<=dr)
{
int mij=(st+dr)/2;
if(getmax(mij,i)<v[i+1][j])
{
up[i][j]=mij;
dr=mij-1;
}
else
st=mij+1;
}
}
}
for(int c1=2;c1<m;c1++)
{
for(int i=2;i<n;i++)
{
dlin[i]=1e9;
ulin[i]=0;
valmax[i]=-1;
}
for(int c2=c1;c2<m;c2++)
{
for(int i=2;i<n;i++)
who[i].clear();
for(int i=2;i<n;i++)
{
valmax[i]=max(valmax[i],v[i][c2]);
dlin[i]=min(dlin[i],down[i][c2]);
ulin[i]=max(ulin[i],up[i][c2]);
if(valmax[i]<v[i][c1-1]&&valmax[i]<v[i][c2+1])
good[i]=1;
else
good[i]=0;
if(ulin[i]>=2&&ulin[i]<n&&good[i])
who[ulin[i]].push_back(i);
}
for(int i=n-1;i>=2;i--)
{
if(!good[i])
{
last[i]=0;
continue;
}
if(good[i+1])
last[i]=last[i+1];
else
last[i]=i;
}
for(int i=1;i<=n;i++)
aib[i]=0;
for(int i=2;i<n;i++)
{
for(int j:who[i])
update(j,1);
if(good[i])
{
int st=i;
int dr=min(dlin[i],last[i]);
if(st<=dr)
{
int val=suma(dr)-suma(st-1);
ans+=val;
}
}
}
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
2548 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8692 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
1 ms |
2548 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
8540 KB |
Output is correct |
20 |
Correct |
1 ms |
8692 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
3 ms |
8792 KB |
Output is correct |
23 |
Correct |
5 ms |
8796 KB |
Output is correct |
24 |
Correct |
4 ms |
8840 KB |
Output is correct |
25 |
Correct |
5 ms |
8792 KB |
Output is correct |
26 |
Correct |
5 ms |
8796 KB |
Output is correct |
27 |
Correct |
5 ms |
8792 KB |
Output is correct |
28 |
Correct |
5 ms |
8796 KB |
Output is correct |
29 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8792 KB |
Output is correct |
18 |
Correct |
5 ms |
8796 KB |
Output is correct |
19 |
Correct |
4 ms |
8840 KB |
Output is correct |
20 |
Correct |
5 ms |
8792 KB |
Output is correct |
21 |
Correct |
5 ms |
8796 KB |
Output is correct |
22 |
Correct |
5 ms |
8792 KB |
Output is correct |
23 |
Correct |
5 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
1 ms |
2548 KB |
Output is correct |
26 |
Correct |
0 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
8540 KB |
Output is correct |
28 |
Correct |
1 ms |
8692 KB |
Output is correct |
29 |
Correct |
0 ms |
2396 KB |
Output is correct |
30 |
Correct |
36 ms |
11332 KB |
Output is correct |
31 |
Correct |
38 ms |
11336 KB |
Output is correct |
32 |
Correct |
32 ms |
11396 KB |
Output is correct |
33 |
Correct |
32 ms |
11100 KB |
Output is correct |
34 |
Correct |
34 ms |
11096 KB |
Output is correct |
35 |
Correct |
33 ms |
11352 KB |
Output is correct |
36 |
Correct |
35 ms |
11384 KB |
Output is correct |
37 |
Correct |
48 ms |
9380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8792 KB |
Output is correct |
18 |
Correct |
5 ms |
8796 KB |
Output is correct |
19 |
Correct |
4 ms |
8840 KB |
Output is correct |
20 |
Correct |
5 ms |
8792 KB |
Output is correct |
21 |
Correct |
5 ms |
8796 KB |
Output is correct |
22 |
Correct |
5 ms |
8792 KB |
Output is correct |
23 |
Correct |
5 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
36 ms |
11332 KB |
Output is correct |
26 |
Correct |
38 ms |
11336 KB |
Output is correct |
27 |
Correct |
32 ms |
11396 KB |
Output is correct |
28 |
Correct |
32 ms |
11100 KB |
Output is correct |
29 |
Correct |
34 ms |
11096 KB |
Output is correct |
30 |
Correct |
33 ms |
11352 KB |
Output is correct |
31 |
Correct |
35 ms |
11384 KB |
Output is correct |
32 |
Correct |
48 ms |
9380 KB |
Output is correct |
33 |
Correct |
1 ms |
2548 KB |
Output is correct |
34 |
Correct |
0 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
8540 KB |
Output is correct |
36 |
Correct |
1 ms |
8692 KB |
Output is correct |
37 |
Correct |
0 ms |
2396 KB |
Output is correct |
38 |
Correct |
1274 ms |
34252 KB |
Output is correct |
39 |
Correct |
1189 ms |
34248 KB |
Output is correct |
40 |
Correct |
1256 ms |
34268 KB |
Output is correct |
41 |
Correct |
1441 ms |
34256 KB |
Output is correct |
42 |
Correct |
1390 ms |
34352 KB |
Output is correct |
43 |
Correct |
1320 ms |
34404 KB |
Output is correct |
44 |
Correct |
1312 ms |
34668 KB |
Output is correct |
45 |
Correct |
1192 ms |
34352 KB |
Output is correct |
46 |
Correct |
1263 ms |
32088 KB |
Output is correct |
47 |
Correct |
1294 ms |
32092 KB |
Output is correct |
48 |
Correct |
1470 ms |
33180 KB |
Output is correct |
49 |
Correct |
1255 ms |
35084 KB |
Output is correct |
50 |
Correct |
345 ms |
31056 KB |
Output is correct |
51 |
Correct |
646 ms |
18520 KB |
Output is correct |
52 |
Correct |
1217 ms |
33900 KB |
Output is correct |
53 |
Correct |
1195 ms |
34640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
8796 KB |
Output is correct |
2 |
Correct |
22 ms |
8796 KB |
Output is correct |
3 |
Correct |
29 ms |
8792 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
29 ms |
8796 KB |
Output is correct |
6 |
Correct |
37 ms |
8796 KB |
Output is correct |
7 |
Correct |
29 ms |
8796 KB |
Output is correct |
8 |
Correct |
29 ms |
8796 KB |
Output is correct |
9 |
Correct |
29 ms |
8876 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2548 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8692 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Execution timed out |
5054 ms |
67700 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8536 KB |
Output is correct |
2 |
Correct |
4 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
2 ms |
8540 KB |
Output is correct |
6 |
Correct |
1 ms |
8536 KB |
Output is correct |
7 |
Correct |
1 ms |
8540 KB |
Output is correct |
8 |
Correct |
2 ms |
8540 KB |
Output is correct |
9 |
Correct |
2 ms |
8540 KB |
Output is correct |
10 |
Correct |
1 ms |
8540 KB |
Output is correct |
11 |
Correct |
3 ms |
8540 KB |
Output is correct |
12 |
Correct |
2 ms |
8540 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
2 ms |
8540 KB |
Output is correct |
16 |
Correct |
2 ms |
8540 KB |
Output is correct |
17 |
Correct |
3 ms |
8792 KB |
Output is correct |
18 |
Correct |
5 ms |
8796 KB |
Output is correct |
19 |
Correct |
4 ms |
8840 KB |
Output is correct |
20 |
Correct |
5 ms |
8792 KB |
Output is correct |
21 |
Correct |
5 ms |
8796 KB |
Output is correct |
22 |
Correct |
5 ms |
8792 KB |
Output is correct |
23 |
Correct |
5 ms |
8796 KB |
Output is correct |
24 |
Correct |
2 ms |
8796 KB |
Output is correct |
25 |
Correct |
36 ms |
11332 KB |
Output is correct |
26 |
Correct |
38 ms |
11336 KB |
Output is correct |
27 |
Correct |
32 ms |
11396 KB |
Output is correct |
28 |
Correct |
32 ms |
11100 KB |
Output is correct |
29 |
Correct |
34 ms |
11096 KB |
Output is correct |
30 |
Correct |
33 ms |
11352 KB |
Output is correct |
31 |
Correct |
35 ms |
11384 KB |
Output is correct |
32 |
Correct |
48 ms |
9380 KB |
Output is correct |
33 |
Correct |
1274 ms |
34252 KB |
Output is correct |
34 |
Correct |
1189 ms |
34248 KB |
Output is correct |
35 |
Correct |
1256 ms |
34268 KB |
Output is correct |
36 |
Correct |
1441 ms |
34256 KB |
Output is correct |
37 |
Correct |
1390 ms |
34352 KB |
Output is correct |
38 |
Correct |
1320 ms |
34404 KB |
Output is correct |
39 |
Correct |
1312 ms |
34668 KB |
Output is correct |
40 |
Correct |
1192 ms |
34352 KB |
Output is correct |
41 |
Correct |
1263 ms |
32088 KB |
Output is correct |
42 |
Correct |
1294 ms |
32092 KB |
Output is correct |
43 |
Correct |
1470 ms |
33180 KB |
Output is correct |
44 |
Correct |
1255 ms |
35084 KB |
Output is correct |
45 |
Correct |
345 ms |
31056 KB |
Output is correct |
46 |
Correct |
646 ms |
18520 KB |
Output is correct |
47 |
Correct |
1217 ms |
33900 KB |
Output is correct |
48 |
Correct |
1195 ms |
34640 KB |
Output is correct |
49 |
Correct |
30 ms |
8796 KB |
Output is correct |
50 |
Correct |
22 ms |
8796 KB |
Output is correct |
51 |
Correct |
29 ms |
8792 KB |
Output is correct |
52 |
Correct |
1 ms |
2396 KB |
Output is correct |
53 |
Correct |
29 ms |
8796 KB |
Output is correct |
54 |
Correct |
37 ms |
8796 KB |
Output is correct |
55 |
Correct |
29 ms |
8796 KB |
Output is correct |
56 |
Correct |
29 ms |
8796 KB |
Output is correct |
57 |
Correct |
29 ms |
8876 KB |
Output is correct |
58 |
Correct |
1 ms |
2392 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
8536 KB |
Output is correct |
61 |
Execution timed out |
5054 ms |
67700 KB |
Time limit exceeded |
62 |
Halted |
0 ms |
0 KB |
- |