#pragma GCC optimize ("O3")
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#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);
}
int 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={max(dsu[i].y.x,dsu[j].y.x),min(dsu[i].y.y,dsu[j].y.y)};
sz[i]+=sz[j];
return i;
}
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]++;
int par=getp(vp[j].y);
if(vp[j].y!=m-1)
if(tmp[vp[j].y+1]==i)
par=connect(par, vp[j].y+1);
if(vp[j].y!=0)
if(tmp[vp[j].y-1]==i)
par=connect(par, vp[j].y-1);
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]++;
int par=getp(vp[j].y);
if(vp[j].y!=n-1)
if(tmp[vp[j].y+1]==i)
par=connect(par, vp[j].y+1);
if(vp[j].y!=0)
if(tmp[vp[j].y-1]==i)
par=connect(par, vp[j].y-1);
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 |
34 ms |
49376 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
33 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
33 ms |
49404 KB |
Output is correct |
6 |
Correct |
34 ms |
49400 KB |
Output is correct |
7 |
Correct |
31 ms |
49400 KB |
Output is correct |
8 |
Correct |
34 ms |
49376 KB |
Output is correct |
9 |
Correct |
35 ms |
49400 KB |
Output is correct |
10 |
Correct |
38 ms |
49400 KB |
Output is correct |
11 |
Correct |
32 ms |
49400 KB |
Output is correct |
12 |
Correct |
38 ms |
49532 KB |
Output is correct |
13 |
Correct |
32 ms |
49400 KB |
Output is correct |
14 |
Correct |
33 ms |
49400 KB |
Output is correct |
15 |
Correct |
33 ms |
49400 KB |
Output is correct |
16 |
Correct |
33 ms |
49272 KB |
Output is correct |
17 |
Correct |
34 ms |
49400 KB |
Output is correct |
18 |
Correct |
32 ms |
49380 KB |
Output is correct |
19 |
Correct |
36 ms |
49404 KB |
Output is correct |
20 |
Correct |
33 ms |
49272 KB |
Output is correct |
21 |
Correct |
37 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49376 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
33 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
33 ms |
49404 KB |
Output is correct |
6 |
Correct |
34 ms |
49400 KB |
Output is correct |
7 |
Correct |
31 ms |
49400 KB |
Output is correct |
8 |
Correct |
34 ms |
49376 KB |
Output is correct |
9 |
Correct |
35 ms |
49400 KB |
Output is correct |
10 |
Correct |
38 ms |
49400 KB |
Output is correct |
11 |
Correct |
32 ms |
49400 KB |
Output is correct |
12 |
Correct |
38 ms |
49532 KB |
Output is correct |
13 |
Correct |
32 ms |
49400 KB |
Output is correct |
14 |
Correct |
33 ms |
49400 KB |
Output is correct |
15 |
Correct |
33 ms |
49400 KB |
Output is correct |
16 |
Correct |
33 ms |
49272 KB |
Output is correct |
17 |
Correct |
38 ms |
50240 KB |
Output is correct |
18 |
Correct |
41 ms |
50296 KB |
Output is correct |
19 |
Correct |
35 ms |
50168 KB |
Output is correct |
20 |
Correct |
35 ms |
49784 KB |
Output is correct |
21 |
Correct |
36 ms |
49888 KB |
Output is correct |
22 |
Correct |
39 ms |
49912 KB |
Output is correct |
23 |
Correct |
37 ms |
49912 KB |
Output is correct |
24 |
Correct |
37 ms |
49528 KB |
Output is correct |
25 |
Correct |
34 ms |
49400 KB |
Output is correct |
26 |
Correct |
32 ms |
49380 KB |
Output is correct |
27 |
Correct |
36 ms |
49404 KB |
Output is correct |
28 |
Correct |
33 ms |
49272 KB |
Output is correct |
29 |
Correct |
37 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49376 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
33 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
33 ms |
49404 KB |
Output is correct |
6 |
Correct |
34 ms |
49400 KB |
Output is correct |
7 |
Correct |
31 ms |
49400 KB |
Output is correct |
8 |
Correct |
34 ms |
49376 KB |
Output is correct |
9 |
Correct |
35 ms |
49400 KB |
Output is correct |
10 |
Correct |
38 ms |
49400 KB |
Output is correct |
11 |
Correct |
32 ms |
49400 KB |
Output is correct |
12 |
Correct |
38 ms |
49532 KB |
Output is correct |
13 |
Correct |
32 ms |
49400 KB |
Output is correct |
14 |
Correct |
33 ms |
49400 KB |
Output is correct |
15 |
Correct |
33 ms |
49400 KB |
Output is correct |
16 |
Correct |
33 ms |
49272 KB |
Output is correct |
17 |
Correct |
38 ms |
50240 KB |
Output is correct |
18 |
Correct |
41 ms |
50296 KB |
Output is correct |
19 |
Correct |
35 ms |
50168 KB |
Output is correct |
20 |
Correct |
35 ms |
49784 KB |
Output is correct |
21 |
Correct |
36 ms |
49888 KB |
Output is correct |
22 |
Correct |
39 ms |
49912 KB |
Output is correct |
23 |
Correct |
37 ms |
49912 KB |
Output is correct |
24 |
Correct |
37 ms |
49528 KB |
Output is correct |
25 |
Correct |
57 ms |
55340 KB |
Output is correct |
26 |
Correct |
66 ms |
55336 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
55 ms |
52140 KB |
Output is correct |
29 |
Correct |
75 ms |
53324 KB |
Output is correct |
30 |
Correct |
67 ms |
53300 KB |
Output is correct |
31 |
Correct |
63 ms |
53040 KB |
Output is correct |
32 |
Correct |
67 ms |
53040 KB |
Output is correct |
33 |
Correct |
34 ms |
49400 KB |
Output is correct |
34 |
Correct |
32 ms |
49380 KB |
Output is correct |
35 |
Correct |
36 ms |
49404 KB |
Output is correct |
36 |
Correct |
33 ms |
49272 KB |
Output is correct |
37 |
Correct |
37 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49376 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
33 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
33 ms |
49404 KB |
Output is correct |
6 |
Correct |
34 ms |
49400 KB |
Output is correct |
7 |
Correct |
31 ms |
49400 KB |
Output is correct |
8 |
Correct |
34 ms |
49376 KB |
Output is correct |
9 |
Correct |
35 ms |
49400 KB |
Output is correct |
10 |
Correct |
38 ms |
49400 KB |
Output is correct |
11 |
Correct |
32 ms |
49400 KB |
Output is correct |
12 |
Correct |
38 ms |
49532 KB |
Output is correct |
13 |
Correct |
32 ms |
49400 KB |
Output is correct |
14 |
Correct |
33 ms |
49400 KB |
Output is correct |
15 |
Correct |
33 ms |
49400 KB |
Output is correct |
16 |
Correct |
33 ms |
49272 KB |
Output is correct |
17 |
Correct |
38 ms |
50240 KB |
Output is correct |
18 |
Correct |
41 ms |
50296 KB |
Output is correct |
19 |
Correct |
35 ms |
50168 KB |
Output is correct |
20 |
Correct |
35 ms |
49784 KB |
Output is correct |
21 |
Correct |
36 ms |
49888 KB |
Output is correct |
22 |
Correct |
39 ms |
49912 KB |
Output is correct |
23 |
Correct |
37 ms |
49912 KB |
Output is correct |
24 |
Correct |
37 ms |
49528 KB |
Output is correct |
25 |
Correct |
57 ms |
55340 KB |
Output is correct |
26 |
Correct |
66 ms |
55336 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
55 ms |
52140 KB |
Output is correct |
29 |
Correct |
75 ms |
53324 KB |
Output is correct |
30 |
Correct |
67 ms |
53300 KB |
Output is correct |
31 |
Correct |
63 ms |
53040 KB |
Output is correct |
32 |
Correct |
67 ms |
53040 KB |
Output is correct |
33 |
Correct |
226 ms |
86040 KB |
Output is correct |
34 |
Correct |
219 ms |
85912 KB |
Output is correct |
35 |
Correct |
206 ms |
85912 KB |
Output is correct |
36 |
Correct |
216 ms |
85960 KB |
Output is correct |
37 |
Correct |
379 ms |
118048 KB |
Output is correct |
38 |
Correct |
364 ms |
117968 KB |
Output is correct |
39 |
Correct |
372 ms |
118024 KB |
Output is correct |
40 |
Correct |
366 ms |
113928 KB |
Output is correct |
41 |
Correct |
237 ms |
74776 KB |
Output is correct |
42 |
Correct |
259 ms |
79896 KB |
Output is correct |
43 |
Correct |
575 ms |
101900 KB |
Output is correct |
44 |
Correct |
540 ms |
102152 KB |
Output is correct |
45 |
Correct |
341 ms |
75812 KB |
Output is correct |
46 |
Correct |
286 ms |
75676 KB |
Output is correct |
47 |
Correct |
531 ms |
99464 KB |
Output is correct |
48 |
Correct |
568 ms |
99592 KB |
Output is correct |
49 |
Correct |
34 ms |
49400 KB |
Output is correct |
50 |
Correct |
32 ms |
49380 KB |
Output is correct |
51 |
Correct |
36 ms |
49404 KB |
Output is correct |
52 |
Correct |
33 ms |
49272 KB |
Output is correct |
53 |
Correct |
37 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
49912 KB |
Output is correct |
2 |
Correct |
35 ms |
49912 KB |
Output is correct |
3 |
Correct |
37 ms |
49400 KB |
Output is correct |
4 |
Correct |
32 ms |
49272 KB |
Output is correct |
5 |
Correct |
40 ms |
49656 KB |
Output is correct |
6 |
Correct |
40 ms |
49656 KB |
Output is correct |
7 |
Correct |
38 ms |
49656 KB |
Output is correct |
8 |
Correct |
40 ms |
49528 KB |
Output is correct |
9 |
Correct |
37 ms |
49656 KB |
Output is correct |
10 |
Correct |
68 ms |
49400 KB |
Output is correct |
11 |
Correct |
39 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
49400 KB |
Output is correct |
2 |
Correct |
1335 ms |
213448 KB |
Output is correct |
3 |
Correct |
3002 ms |
390208 KB |
Output is correct |
4 |
Correct |
2805 ms |
390936 KB |
Output is correct |
5 |
Correct |
2813 ms |
390956 KB |
Output is correct |
6 |
Correct |
422 ms |
73592 KB |
Output is correct |
7 |
Correct |
823 ms |
95608 KB |
Output is correct |
8 |
Correct |
888 ms |
98552 KB |
Output is correct |
9 |
Correct |
34 ms |
49400 KB |
Output is correct |
10 |
Correct |
32 ms |
49380 KB |
Output is correct |
11 |
Correct |
36 ms |
49404 KB |
Output is correct |
12 |
Correct |
33 ms |
49272 KB |
Output is correct |
13 |
Correct |
37 ms |
49400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
49376 KB |
Output is correct |
2 |
Correct |
33 ms |
49400 KB |
Output is correct |
3 |
Correct |
33 ms |
49400 KB |
Output is correct |
4 |
Correct |
35 ms |
49400 KB |
Output is correct |
5 |
Correct |
33 ms |
49404 KB |
Output is correct |
6 |
Correct |
34 ms |
49400 KB |
Output is correct |
7 |
Correct |
31 ms |
49400 KB |
Output is correct |
8 |
Correct |
34 ms |
49376 KB |
Output is correct |
9 |
Correct |
35 ms |
49400 KB |
Output is correct |
10 |
Correct |
38 ms |
49400 KB |
Output is correct |
11 |
Correct |
32 ms |
49400 KB |
Output is correct |
12 |
Correct |
38 ms |
49532 KB |
Output is correct |
13 |
Correct |
32 ms |
49400 KB |
Output is correct |
14 |
Correct |
33 ms |
49400 KB |
Output is correct |
15 |
Correct |
33 ms |
49400 KB |
Output is correct |
16 |
Correct |
33 ms |
49272 KB |
Output is correct |
17 |
Correct |
38 ms |
50240 KB |
Output is correct |
18 |
Correct |
41 ms |
50296 KB |
Output is correct |
19 |
Correct |
35 ms |
50168 KB |
Output is correct |
20 |
Correct |
35 ms |
49784 KB |
Output is correct |
21 |
Correct |
36 ms |
49888 KB |
Output is correct |
22 |
Correct |
39 ms |
49912 KB |
Output is correct |
23 |
Correct |
37 ms |
49912 KB |
Output is correct |
24 |
Correct |
37 ms |
49528 KB |
Output is correct |
25 |
Correct |
57 ms |
55340 KB |
Output is correct |
26 |
Correct |
66 ms |
55336 KB |
Output is correct |
27 |
Correct |
60 ms |
55340 KB |
Output is correct |
28 |
Correct |
55 ms |
52140 KB |
Output is correct |
29 |
Correct |
75 ms |
53324 KB |
Output is correct |
30 |
Correct |
67 ms |
53300 KB |
Output is correct |
31 |
Correct |
63 ms |
53040 KB |
Output is correct |
32 |
Correct |
67 ms |
53040 KB |
Output is correct |
33 |
Correct |
226 ms |
86040 KB |
Output is correct |
34 |
Correct |
219 ms |
85912 KB |
Output is correct |
35 |
Correct |
206 ms |
85912 KB |
Output is correct |
36 |
Correct |
216 ms |
85960 KB |
Output is correct |
37 |
Correct |
379 ms |
118048 KB |
Output is correct |
38 |
Correct |
364 ms |
117968 KB |
Output is correct |
39 |
Correct |
372 ms |
118024 KB |
Output is correct |
40 |
Correct |
366 ms |
113928 KB |
Output is correct |
41 |
Correct |
237 ms |
74776 KB |
Output is correct |
42 |
Correct |
259 ms |
79896 KB |
Output is correct |
43 |
Correct |
575 ms |
101900 KB |
Output is correct |
44 |
Correct |
540 ms |
102152 KB |
Output is correct |
45 |
Correct |
341 ms |
75812 KB |
Output is correct |
46 |
Correct |
286 ms |
75676 KB |
Output is correct |
47 |
Correct |
531 ms |
99464 KB |
Output is correct |
48 |
Correct |
568 ms |
99592 KB |
Output is correct |
49 |
Correct |
36 ms |
49912 KB |
Output is correct |
50 |
Correct |
35 ms |
49912 KB |
Output is correct |
51 |
Correct |
37 ms |
49400 KB |
Output is correct |
52 |
Correct |
32 ms |
49272 KB |
Output is correct |
53 |
Correct |
40 ms |
49656 KB |
Output is correct |
54 |
Correct |
40 ms |
49656 KB |
Output is correct |
55 |
Correct |
38 ms |
49656 KB |
Output is correct |
56 |
Correct |
40 ms |
49528 KB |
Output is correct |
57 |
Correct |
37 ms |
49656 KB |
Output is correct |
58 |
Correct |
68 ms |
49400 KB |
Output is correct |
59 |
Correct |
39 ms |
49400 KB |
Output is correct |
60 |
Correct |
47 ms |
49400 KB |
Output is correct |
61 |
Correct |
1335 ms |
213448 KB |
Output is correct |
62 |
Correct |
3002 ms |
390208 KB |
Output is correct |
63 |
Correct |
2805 ms |
390936 KB |
Output is correct |
64 |
Correct |
2813 ms |
390956 KB |
Output is correct |
65 |
Correct |
422 ms |
73592 KB |
Output is correct |
66 |
Correct |
823 ms |
95608 KB |
Output is correct |
67 |
Correct |
888 ms |
98552 KB |
Output is correct |
68 |
Correct |
2541 ms |
503724 KB |
Output is correct |
69 |
Correct |
2291 ms |
505404 KB |
Output is correct |
70 |
Correct |
2382 ms |
503716 KB |
Output is correct |
71 |
Correct |
2187 ms |
505660 KB |
Output is correct |
72 |
Correct |
4712 ms |
905180 KB |
Output is correct |
73 |
Correct |
4320 ms |
450012 KB |
Output is correct |
74 |
Execution timed out |
5071 ms |
462772 KB |
Time limit exceeded |
75 |
Halted |
0 ms |
0 KB |
- |