#include <bits/stdc++.h>
#include "rect.h"
#define ll long long
#define pb push_back
#define x first
#define y second
using namespace std;
int where[2500][2500],where2[2500][2500];
vector<vector<pair<int,int>>> mp,mp2;
vector<pair<int,pair<int,int>>> dsu;
vector<int> sz;
int getp(int x)
{
if (x==dsu[x].x)
return x;
return dsu[x].x=getp(dsu[x].x);
}
void connect(int i, int j)
{
i=getp(i), j=getp(j);
if(sz[i]<sz[j]) swap(i,j);
dsu[j].x=i;
dsu[i].y.x=max(dsu[i].y.x,dsu[j].y.x);
dsu[i].y.y=min(dsu[i].y.y,dsu[j].y.y);
sz[i]+=sz[j];
}
ll count_rectangles(vector<vector<int>> a)
{
for(int i=0;i<2500;i++) dsu.pb({i,{i,i}}), sz.pb(1);
for(int i=0;i<2500;i++) for(int j=0;j<2500;j++) where[i][j]=-1,where2[i][j]=-1;
vector<pair<int,pair<int,int>>> dsuin=dsu;
int n=a.size(), m=a[0].size();
vector<int> tmp(2500,0);
for(int i=1;i<n-1;i++)
{
dsu=dsuin;
fill(sz.begin(),sz.end(),1);
vector<pair<int,int>> vp;
for(int j=0;j<m;j++) vp.pb({a[i][j],j});
sort(vp.begin(),vp.end());
int startedwith=mp.size();
for(int j=0;j<m;j++)
{
tmp[vp[j].y]++;
if(vp[j].y!=m-1)
if(tmp[vp[j].y+1]==i)
connect(vp[j].y, vp[j].y+1);
if(vp[j].y!=0)
if(tmp[vp[j].y-1]==i)
connect(vp[j].y, vp[j].y-1);
int par=getp(vp[j].y);
int l=dsu[par].y.x, l2=dsu[par].y.y;
if(l2==0 || l==m-1) continue;
if(a[i][l+1]==vp[j].x || a[i][l2-1]==vp[j].x) continue;
int num=1;
if(where[i-1][l2-1]!=-1)
{
auto it = lower_bound(mp[where[i-1][l2-1]].begin(),mp[where[i-1][l2-1]].end(),pair<int,int>{l+1,0});
if(it!=mp[where[i-1][l2-1]].end()) if((*it).x==l+1) num+=(*it).y;
}
if(where[i][l2-1]==-1) where[i][l2-1]=mp.size(), mp.pb(vector<pair<int,int>>());
mp[where[i][l2-1]].pb({l+1,num});
num=1;
if(where[i-1][l+1]!=-1)
{
auto it = lower_bound(mp[where[i-1][l+1]].begin(),mp[where[i-1][l+1]].end(),pair<int,int>{l2-1,0});
if(it!=mp[where[i-1][l+1]].end()) if((*it).x==l2-1) num+=(*it).y;
}
if(where[i][l+1]==-1) where[i][l+1]=mp.size(), mp.pb(vector<pair<int,int>>());
mp[where[i][l+1]].pb({l2-1,num});
}
for(int z=startedwith;z<(int)mp.size();z++) sort(mp[z].begin(),mp[z].end());
}
ll ans=0;
for(int i=0;i<2500;i++) tmp[i]=0;
for(int i=1;i<m-1;i++)
{
dsu=dsuin;
fill(sz.begin(),sz.end(),1);
vector<pair<int,int>> vp;
for(int j=0;j<n;j++) vp.pb({a[j][i],j});
sort(vp.begin(),vp.end());
int startedwith=mp2.size();
for(int j=0;j<n;j++)
{
tmp[vp[j].y]++;
if(vp[j].y!=n-1)
if(tmp[vp[j].y+1]==i)
connect(vp[j].y, vp[j].y+1);
if(vp[j].y!=0)
if(tmp[vp[j].y-1]==i)
connect(vp[j].y, vp[j].y-1);
int par=getp(vp[j].y);
int l=dsu[par].y.x, l2=dsu[par].y.y;
if(l2==0 || l==n-1) continue;
if(a[l+1][i]==vp[j].x || a[l2-1][i]==vp[j].x) continue;
int num=1;
if(where2[l2-1][i-1]!=-1)
{
auto it = lower_bound(mp2[where2[l2-1][i-1]].begin(),mp2[where2[l2-1][i-1]].end(),pair<int,int>{l+1,0});
if(it!=mp2[where2[l2-1][i-1]].end()) if((*it).x==l+1) num+=(*it).y;
}
if(where2[l2-1][i]==-1) where2[l2-1][i]=mp2.size(), mp2.pb(vector<pair<int,int>>());
mp2[where2[l2-1][i]].pb({l+1,num});
num=1;
if(where2[l+1][i-1]!=-1)
{
auto it = lower_bound(mp2[where2[l+1][i-1]].begin(),mp2[where2[l+1][i-1]].end(),pair<int,int>{l2-1,0});
if(it!=mp2[where2[l+1][i-1]].end()) if((*it).x==l2-1) num+=(*it).y;
}
if(where2[l+1][i]==-1) where2[l+1][i]=mp2.size(), mp2.pb(vector<pair<int,int>>());
mp2[where2[l+1][i]].pb({l2-1,num});
if(where[l][i+1]!=-1)
{
auto it = lower_bound(mp[where[l][i+1]].begin(),mp[where[l][i+1]].end(),pair<int,int>{i-num,l-l2+1});
for(;it!=mp[where[l][i+1]].end();++it)
{
if((*it).x>i+1) break;
if((*it).y>=l-l2+1) ans++;
}
}
}
for(int z=startedwith;z<(int)mp2.size();z++) sort(mp2[z].begin(),mp2[z].end());
}
return ans;
}
/*
int main()
{
freopen("test.in","r",stdin);
int n,m; cin>>n>>m;
vector<vector<int>> a(n);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
int k; cin>>k;
a[i].pb(k);
}
cout<<count_rectangles(a);
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49272 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
34 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
37 ms |
49400 KB |
Output is correct |
6 |
Correct |
34 ms |
49404 KB |
Output is correct |
7 |
Correct |
35 ms |
49404 KB |
Output is correct |
8 |
Correct |
36 ms |
49400 KB |
Output is correct |
9 |
Correct |
38 ms |
49400 KB |
Output is correct |
10 |
Correct |
37 ms |
49400 KB |
Output is correct |
11 |
Correct |
38 ms |
49400 KB |
Output is correct |
12 |
Correct |
35 ms |
49400 KB |
Output is correct |
13 |
Correct |
34 ms |
49272 KB |
Output is correct |
14 |
Correct |
34 ms |
49272 KB |
Output is correct |
15 |
Correct |
35 ms |
49276 KB |
Output is correct |
16 |
Correct |
37 ms |
49272 KB |
Output is correct |
17 |
Correct |
34 ms |
49272 KB |
Output is correct |
18 |
Correct |
34 ms |
49272 KB |
Output is correct |
19 |
Correct |
37 ms |
49528 KB |
Output is correct |
20 |
Correct |
35 ms |
49276 KB |
Output is correct |
21 |
Correct |
36 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49272 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
34 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
37 ms |
49400 KB |
Output is correct |
6 |
Correct |
34 ms |
49404 KB |
Output is correct |
7 |
Correct |
35 ms |
49404 KB |
Output is correct |
8 |
Correct |
36 ms |
49400 KB |
Output is correct |
9 |
Correct |
38 ms |
49400 KB |
Output is correct |
10 |
Correct |
37 ms |
49400 KB |
Output is correct |
11 |
Correct |
38 ms |
49400 KB |
Output is correct |
12 |
Correct |
35 ms |
49400 KB |
Output is correct |
13 |
Correct |
34 ms |
49272 KB |
Output is correct |
14 |
Correct |
34 ms |
49272 KB |
Output is correct |
15 |
Correct |
35 ms |
49276 KB |
Output is correct |
16 |
Correct |
37 ms |
49272 KB |
Output is correct |
17 |
Correct |
41 ms |
50424 KB |
Output is correct |
18 |
Correct |
39 ms |
50172 KB |
Output is correct |
19 |
Correct |
40 ms |
50296 KB |
Output is correct |
20 |
Correct |
38 ms |
49784 KB |
Output is correct |
21 |
Correct |
42 ms |
49912 KB |
Output is correct |
22 |
Correct |
40 ms |
49912 KB |
Output is correct |
23 |
Correct |
40 ms |
50040 KB |
Output is correct |
24 |
Correct |
36 ms |
49528 KB |
Output is correct |
25 |
Correct |
34 ms |
49272 KB |
Output is correct |
26 |
Correct |
34 ms |
49272 KB |
Output is correct |
27 |
Correct |
37 ms |
49528 KB |
Output is correct |
28 |
Correct |
35 ms |
49276 KB |
Output is correct |
29 |
Correct |
36 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49272 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
34 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
37 ms |
49400 KB |
Output is correct |
6 |
Correct |
34 ms |
49404 KB |
Output is correct |
7 |
Correct |
35 ms |
49404 KB |
Output is correct |
8 |
Correct |
36 ms |
49400 KB |
Output is correct |
9 |
Correct |
38 ms |
49400 KB |
Output is correct |
10 |
Correct |
37 ms |
49400 KB |
Output is correct |
11 |
Correct |
38 ms |
49400 KB |
Output is correct |
12 |
Correct |
35 ms |
49400 KB |
Output is correct |
13 |
Correct |
34 ms |
49272 KB |
Output is correct |
14 |
Correct |
34 ms |
49272 KB |
Output is correct |
15 |
Correct |
35 ms |
49276 KB |
Output is correct |
16 |
Correct |
37 ms |
49272 KB |
Output is correct |
17 |
Correct |
41 ms |
50424 KB |
Output is correct |
18 |
Correct |
39 ms |
50172 KB |
Output is correct |
19 |
Correct |
40 ms |
50296 KB |
Output is correct |
20 |
Correct |
38 ms |
49784 KB |
Output is correct |
21 |
Correct |
42 ms |
49912 KB |
Output is correct |
22 |
Correct |
40 ms |
49912 KB |
Output is correct |
23 |
Correct |
40 ms |
50040 KB |
Output is correct |
24 |
Correct |
36 ms |
49528 KB |
Output is correct |
25 |
Correct |
65 ms |
55312 KB |
Output is correct |
26 |
Correct |
62 ms |
55340 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
54 ms |
52140 KB |
Output is correct |
29 |
Correct |
68 ms |
53296 KB |
Output is correct |
30 |
Correct |
72 ms |
53292 KB |
Output is correct |
31 |
Correct |
67 ms |
53036 KB |
Output is correct |
32 |
Correct |
68 ms |
53040 KB |
Output is correct |
33 |
Correct |
34 ms |
49272 KB |
Output is correct |
34 |
Correct |
34 ms |
49272 KB |
Output is correct |
35 |
Correct |
37 ms |
49528 KB |
Output is correct |
36 |
Correct |
35 ms |
49276 KB |
Output is correct |
37 |
Correct |
36 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49272 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
34 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
37 ms |
49400 KB |
Output is correct |
6 |
Correct |
34 ms |
49404 KB |
Output is correct |
7 |
Correct |
35 ms |
49404 KB |
Output is correct |
8 |
Correct |
36 ms |
49400 KB |
Output is correct |
9 |
Correct |
38 ms |
49400 KB |
Output is correct |
10 |
Correct |
37 ms |
49400 KB |
Output is correct |
11 |
Correct |
38 ms |
49400 KB |
Output is correct |
12 |
Correct |
35 ms |
49400 KB |
Output is correct |
13 |
Correct |
34 ms |
49272 KB |
Output is correct |
14 |
Correct |
34 ms |
49272 KB |
Output is correct |
15 |
Correct |
35 ms |
49276 KB |
Output is correct |
16 |
Correct |
37 ms |
49272 KB |
Output is correct |
17 |
Correct |
41 ms |
50424 KB |
Output is correct |
18 |
Correct |
39 ms |
50172 KB |
Output is correct |
19 |
Correct |
40 ms |
50296 KB |
Output is correct |
20 |
Correct |
38 ms |
49784 KB |
Output is correct |
21 |
Correct |
42 ms |
49912 KB |
Output is correct |
22 |
Correct |
40 ms |
49912 KB |
Output is correct |
23 |
Correct |
40 ms |
50040 KB |
Output is correct |
24 |
Correct |
36 ms |
49528 KB |
Output is correct |
25 |
Correct |
65 ms |
55312 KB |
Output is correct |
26 |
Correct |
62 ms |
55340 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
54 ms |
52140 KB |
Output is correct |
29 |
Correct |
68 ms |
53296 KB |
Output is correct |
30 |
Correct |
72 ms |
53292 KB |
Output is correct |
31 |
Correct |
67 ms |
53036 KB |
Output is correct |
32 |
Correct |
68 ms |
53040 KB |
Output is correct |
33 |
Correct |
216 ms |
86040 KB |
Output is correct |
34 |
Correct |
210 ms |
86040 KB |
Output is correct |
35 |
Correct |
208 ms |
85916 KB |
Output is correct |
36 |
Correct |
202 ms |
85912 KB |
Output is correct |
37 |
Correct |
368 ms |
118152 KB |
Output is correct |
38 |
Correct |
372 ms |
118076 KB |
Output is correct |
39 |
Correct |
372 ms |
118288 KB |
Output is correct |
40 |
Correct |
347 ms |
114076 KB |
Output is correct |
41 |
Correct |
241 ms |
74768 KB |
Output is correct |
42 |
Correct |
290 ms |
80024 KB |
Output is correct |
43 |
Correct |
561 ms |
101900 KB |
Output is correct |
44 |
Correct |
570 ms |
102152 KB |
Output is correct |
45 |
Correct |
285 ms |
75716 KB |
Output is correct |
46 |
Correct |
281 ms |
75672 KB |
Output is correct |
47 |
Correct |
536 ms |
99720 KB |
Output is correct |
48 |
Correct |
529 ms |
99720 KB |
Output is correct |
49 |
Correct |
34 ms |
49272 KB |
Output is correct |
50 |
Correct |
34 ms |
49272 KB |
Output is correct |
51 |
Correct |
37 ms |
49528 KB |
Output is correct |
52 |
Correct |
35 ms |
49276 KB |
Output is correct |
53 |
Correct |
36 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
49916 KB |
Output is correct |
2 |
Correct |
49 ms |
49784 KB |
Output is correct |
3 |
Correct |
46 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49272 KB |
Output is correct |
5 |
Correct |
54 ms |
49656 KB |
Output is correct |
6 |
Correct |
45 ms |
49656 KB |
Output is correct |
7 |
Correct |
46 ms |
49656 KB |
Output is correct |
8 |
Correct |
51 ms |
49528 KB |
Output is correct |
9 |
Correct |
48 ms |
49528 KB |
Output is correct |
10 |
Correct |
50 ms |
49400 KB |
Output is correct |
11 |
Correct |
46 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49400 KB |
Output is correct |
2 |
Correct |
1268 ms |
213304 KB |
Output is correct |
3 |
Correct |
2923 ms |
390096 KB |
Output is correct |
4 |
Correct |
2958 ms |
390996 KB |
Output is correct |
5 |
Correct |
2912 ms |
391152 KB |
Output is correct |
6 |
Correct |
446 ms |
73800 KB |
Output is correct |
7 |
Correct |
854 ms |
95536 KB |
Output is correct |
8 |
Correct |
912 ms |
98552 KB |
Output is correct |
9 |
Correct |
34 ms |
49272 KB |
Output is correct |
10 |
Correct |
34 ms |
49272 KB |
Output is correct |
11 |
Correct |
37 ms |
49528 KB |
Output is correct |
12 |
Correct |
35 ms |
49276 KB |
Output is correct |
13 |
Correct |
36 ms |
49332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
49272 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
34 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
37 ms |
49400 KB |
Output is correct |
6 |
Correct |
34 ms |
49404 KB |
Output is correct |
7 |
Correct |
35 ms |
49404 KB |
Output is correct |
8 |
Correct |
36 ms |
49400 KB |
Output is correct |
9 |
Correct |
38 ms |
49400 KB |
Output is correct |
10 |
Correct |
37 ms |
49400 KB |
Output is correct |
11 |
Correct |
38 ms |
49400 KB |
Output is correct |
12 |
Correct |
35 ms |
49400 KB |
Output is correct |
13 |
Correct |
34 ms |
49272 KB |
Output is correct |
14 |
Correct |
34 ms |
49272 KB |
Output is correct |
15 |
Correct |
35 ms |
49276 KB |
Output is correct |
16 |
Correct |
37 ms |
49272 KB |
Output is correct |
17 |
Correct |
41 ms |
50424 KB |
Output is correct |
18 |
Correct |
39 ms |
50172 KB |
Output is correct |
19 |
Correct |
40 ms |
50296 KB |
Output is correct |
20 |
Correct |
38 ms |
49784 KB |
Output is correct |
21 |
Correct |
42 ms |
49912 KB |
Output is correct |
22 |
Correct |
40 ms |
49912 KB |
Output is correct |
23 |
Correct |
40 ms |
50040 KB |
Output is correct |
24 |
Correct |
36 ms |
49528 KB |
Output is correct |
25 |
Correct |
65 ms |
55312 KB |
Output is correct |
26 |
Correct |
62 ms |
55340 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
54 ms |
52140 KB |
Output is correct |
29 |
Correct |
68 ms |
53296 KB |
Output is correct |
30 |
Correct |
72 ms |
53292 KB |
Output is correct |
31 |
Correct |
67 ms |
53036 KB |
Output is correct |
32 |
Correct |
68 ms |
53040 KB |
Output is correct |
33 |
Correct |
216 ms |
86040 KB |
Output is correct |
34 |
Correct |
210 ms |
86040 KB |
Output is correct |
35 |
Correct |
208 ms |
85916 KB |
Output is correct |
36 |
Correct |
202 ms |
85912 KB |
Output is correct |
37 |
Correct |
368 ms |
118152 KB |
Output is correct |
38 |
Correct |
372 ms |
118076 KB |
Output is correct |
39 |
Correct |
372 ms |
118288 KB |
Output is correct |
40 |
Correct |
347 ms |
114076 KB |
Output is correct |
41 |
Correct |
241 ms |
74768 KB |
Output is correct |
42 |
Correct |
290 ms |
80024 KB |
Output is correct |
43 |
Correct |
561 ms |
101900 KB |
Output is correct |
44 |
Correct |
570 ms |
102152 KB |
Output is correct |
45 |
Correct |
285 ms |
75716 KB |
Output is correct |
46 |
Correct |
281 ms |
75672 KB |
Output is correct |
47 |
Correct |
536 ms |
99720 KB |
Output is correct |
48 |
Correct |
529 ms |
99720 KB |
Output is correct |
49 |
Correct |
47 ms |
49916 KB |
Output is correct |
50 |
Correct |
49 ms |
49784 KB |
Output is correct |
51 |
Correct |
46 ms |
49400 KB |
Output is correct |
52 |
Correct |
35 ms |
49272 KB |
Output is correct |
53 |
Correct |
54 ms |
49656 KB |
Output is correct |
54 |
Correct |
45 ms |
49656 KB |
Output is correct |
55 |
Correct |
46 ms |
49656 KB |
Output is correct |
56 |
Correct |
51 ms |
49528 KB |
Output is correct |
57 |
Correct |
48 ms |
49528 KB |
Output is correct |
58 |
Correct |
50 ms |
49400 KB |
Output is correct |
59 |
Correct |
46 ms |
49400 KB |
Output is correct |
60 |
Correct |
35 ms |
49400 KB |
Output is correct |
61 |
Correct |
1268 ms |
213304 KB |
Output is correct |
62 |
Correct |
2923 ms |
390096 KB |
Output is correct |
63 |
Correct |
2958 ms |
390996 KB |
Output is correct |
64 |
Correct |
2912 ms |
391152 KB |
Output is correct |
65 |
Correct |
446 ms |
73800 KB |
Output is correct |
66 |
Correct |
854 ms |
95536 KB |
Output is correct |
67 |
Correct |
912 ms |
98552 KB |
Output is correct |
68 |
Correct |
2541 ms |
503524 KB |
Output is correct |
69 |
Correct |
2295 ms |
505344 KB |
Output is correct |
70 |
Correct |
2375 ms |
503964 KB |
Output is correct |
71 |
Correct |
2261 ms |
505604 KB |
Output is correct |
72 |
Correct |
4838 ms |
934040 KB |
Output is correct |
73 |
Correct |
4292 ms |
463856 KB |
Output is correct |
74 |
Execution timed out |
5026 ms |
485204 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |