#include <bits/stdc++.h>
#include "rect.h"
#define ll long long
#define pb push_back
#define x first
#define y second
using namespace std;
vector<vector<set<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[dsu[x].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);
vector<pair<int,pair<int,int>>> dsuin=dsu;
int n=a.size(), m=a[0].size();
for(int i=0;i<n;i++)
mp.pb(vector<set<pair<int,int>>>(m)), mp2.pb(vector<set<pair<int,int>>>(m));
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());
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(int(mp[i-1][l2-1].size()))
{
auto it = mp[i-1][l2-1].lower_bound({l+1,0});
if(it!=mp[i-1][l2-1].end()) if((*it).x==l+1) num+=(*it).y;
}
mp[i][l2-1].insert({l+1,num});
num=1;
if((int)mp[i-1][l+1].size())
{
auto it = mp[i-1][l+1].lower_bound({l2-1,0});
if(it!=mp[i-1][l+1].end()) if((*it).x==l2-1) num+=(*it).y;
}
mp[i][l+1].insert({l2-1,num});
}
}
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());
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((int)mp2[l2-1][i-1].size())
{
auto it = mp2[l2-1][i-1].lower_bound({l+1,0});
if(it!=mp2[l2-1][i-1].end()) if((*it).x==l+1) num+=(*it).y;
}
mp2[l2-1][i].insert({l+1,num});
num=1;
if((int)mp2[l+1][i-1].size())
{
auto it = mp2[l+1][i-1].lower_bound({l2-1,0});
if(it!=mp2[l+1][i-1].end()) if((*it).x==l2-1) num+=(*it).y;
}
mp2[l+1][i].insert({l2-1,num});
auto it = mp[l][i+1].lower_bound({i-num,l-l2+1});
for(;it!=mp[l][i+1].end();it++)
{
if((*it).x>i+1) break;
if((*it).y>=l-l2+1) ans++;
}
}
}
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 |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
636 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
5 ms |
376 KB |
Output is correct |
18 |
Correct |
5 ms |
376 KB |
Output is correct |
19 |
Correct |
6 ms |
504 KB |
Output is correct |
20 |
Correct |
5 ms |
376 KB |
Output is correct |
21 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
636 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
2168 KB |
Output is correct |
18 |
Correct |
11 ms |
2296 KB |
Output is correct |
19 |
Correct |
13 ms |
2168 KB |
Output is correct |
20 |
Correct |
9 ms |
1528 KB |
Output is correct |
21 |
Correct |
13 ms |
2040 KB |
Output is correct |
22 |
Correct |
11 ms |
2040 KB |
Output is correct |
23 |
Correct |
12 ms |
2040 KB |
Output is correct |
24 |
Correct |
8 ms |
1144 KB |
Output is correct |
25 |
Correct |
5 ms |
376 KB |
Output is correct |
26 |
Correct |
5 ms |
376 KB |
Output is correct |
27 |
Correct |
6 ms |
504 KB |
Output is correct |
28 |
Correct |
5 ms |
376 KB |
Output is correct |
29 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
636 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
2168 KB |
Output is correct |
18 |
Correct |
11 ms |
2296 KB |
Output is correct |
19 |
Correct |
13 ms |
2168 KB |
Output is correct |
20 |
Correct |
9 ms |
1528 KB |
Output is correct |
21 |
Correct |
13 ms |
2040 KB |
Output is correct |
22 |
Correct |
11 ms |
2040 KB |
Output is correct |
23 |
Correct |
12 ms |
2040 KB |
Output is correct |
24 |
Correct |
8 ms |
1144 KB |
Output is correct |
25 |
Correct |
43 ms |
11896 KB |
Output is correct |
26 |
Correct |
40 ms |
11896 KB |
Output is correct |
27 |
Correct |
43 ms |
11896 KB |
Output is correct |
28 |
Correct |
29 ms |
7292 KB |
Output is correct |
29 |
Correct |
63 ms |
11592 KB |
Output is correct |
30 |
Correct |
53 ms |
11512 KB |
Output is correct |
31 |
Correct |
63 ms |
11004 KB |
Output is correct |
32 |
Correct |
50 ms |
10872 KB |
Output is correct |
33 |
Correct |
5 ms |
376 KB |
Output is correct |
34 |
Correct |
5 ms |
376 KB |
Output is correct |
35 |
Correct |
6 ms |
504 KB |
Output is correct |
36 |
Correct |
5 ms |
376 KB |
Output is correct |
37 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
636 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
2168 KB |
Output is correct |
18 |
Correct |
11 ms |
2296 KB |
Output is correct |
19 |
Correct |
13 ms |
2168 KB |
Output is correct |
20 |
Correct |
9 ms |
1528 KB |
Output is correct |
21 |
Correct |
13 ms |
2040 KB |
Output is correct |
22 |
Correct |
11 ms |
2040 KB |
Output is correct |
23 |
Correct |
12 ms |
2040 KB |
Output is correct |
24 |
Correct |
8 ms |
1144 KB |
Output is correct |
25 |
Correct |
43 ms |
11896 KB |
Output is correct |
26 |
Correct |
40 ms |
11896 KB |
Output is correct |
27 |
Correct |
43 ms |
11896 KB |
Output is correct |
28 |
Correct |
29 ms |
7292 KB |
Output is correct |
29 |
Correct |
63 ms |
11592 KB |
Output is correct |
30 |
Correct |
53 ms |
11512 KB |
Output is correct |
31 |
Correct |
63 ms |
11004 KB |
Output is correct |
32 |
Correct |
50 ms |
10872 KB |
Output is correct |
33 |
Correct |
281 ms |
96632 KB |
Output is correct |
34 |
Correct |
276 ms |
96632 KB |
Output is correct |
35 |
Correct |
320 ms |
96932 KB |
Output is correct |
36 |
Correct |
322 ms |
96760 KB |
Output is correct |
37 |
Correct |
513 ms |
142484 KB |
Output is correct |
38 |
Correct |
510 ms |
142328 KB |
Output is correct |
39 |
Correct |
510 ms |
142584 KB |
Output is correct |
40 |
Correct |
490 ms |
133196 KB |
Output is correct |
41 |
Correct |
262 ms |
73208 KB |
Output is correct |
42 |
Correct |
338 ms |
86012 KB |
Output is correct |
43 |
Correct |
835 ms |
139640 KB |
Output is correct |
44 |
Correct |
851 ms |
141304 KB |
Output is correct |
45 |
Correct |
404 ms |
70264 KB |
Output is correct |
46 |
Correct |
411 ms |
70136 KB |
Output is correct |
47 |
Correct |
787 ms |
133496 KB |
Output is correct |
48 |
Correct |
805 ms |
133752 KB |
Output is correct |
49 |
Correct |
5 ms |
376 KB |
Output is correct |
50 |
Correct |
5 ms |
376 KB |
Output is correct |
51 |
Correct |
6 ms |
504 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
1656 KB |
Output is correct |
2 |
Correct |
16 ms |
1400 KB |
Output is correct |
3 |
Correct |
18 ms |
1272 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
19 ms |
1400 KB |
Output is correct |
6 |
Correct |
17 ms |
1400 KB |
Output is correct |
7 |
Correct |
18 ms |
1400 KB |
Output is correct |
8 |
Correct |
20 ms |
1532 KB |
Output is correct |
9 |
Correct |
19 ms |
1400 KB |
Output is correct |
10 |
Correct |
20 ms |
632 KB |
Output is correct |
11 |
Correct |
16 ms |
888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
1569 ms |
426840 KB |
Output is correct |
3 |
Correct |
3616 ms |
925560 KB |
Output is correct |
4 |
Correct |
3651 ms |
929656 KB |
Output is correct |
5 |
Correct |
3699 ms |
929792 KB |
Output is correct |
6 |
Correct |
641 ms |
314744 KB |
Output is correct |
7 |
Correct |
1250 ms |
597624 KB |
Output is correct |
8 |
Correct |
1350 ms |
637048 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
6 ms |
504 KB |
Output is correct |
12 |
Correct |
5 ms |
376 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
632 KB |
Output is correct |
3 |
Correct |
6 ms |
632 KB |
Output is correct |
4 |
Correct |
6 ms |
636 KB |
Output is correct |
5 |
Correct |
5 ms |
504 KB |
Output is correct |
6 |
Correct |
6 ms |
632 KB |
Output is correct |
7 |
Correct |
6 ms |
508 KB |
Output is correct |
8 |
Correct |
6 ms |
504 KB |
Output is correct |
9 |
Correct |
6 ms |
632 KB |
Output is correct |
10 |
Correct |
6 ms |
632 KB |
Output is correct |
11 |
Correct |
6 ms |
632 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
5 ms |
376 KB |
Output is correct |
17 |
Correct |
11 ms |
2168 KB |
Output is correct |
18 |
Correct |
11 ms |
2296 KB |
Output is correct |
19 |
Correct |
13 ms |
2168 KB |
Output is correct |
20 |
Correct |
9 ms |
1528 KB |
Output is correct |
21 |
Correct |
13 ms |
2040 KB |
Output is correct |
22 |
Correct |
11 ms |
2040 KB |
Output is correct |
23 |
Correct |
12 ms |
2040 KB |
Output is correct |
24 |
Correct |
8 ms |
1144 KB |
Output is correct |
25 |
Correct |
43 ms |
11896 KB |
Output is correct |
26 |
Correct |
40 ms |
11896 KB |
Output is correct |
27 |
Correct |
43 ms |
11896 KB |
Output is correct |
28 |
Correct |
29 ms |
7292 KB |
Output is correct |
29 |
Correct |
63 ms |
11592 KB |
Output is correct |
30 |
Correct |
53 ms |
11512 KB |
Output is correct |
31 |
Correct |
63 ms |
11004 KB |
Output is correct |
32 |
Correct |
50 ms |
10872 KB |
Output is correct |
33 |
Correct |
281 ms |
96632 KB |
Output is correct |
34 |
Correct |
276 ms |
96632 KB |
Output is correct |
35 |
Correct |
320 ms |
96932 KB |
Output is correct |
36 |
Correct |
322 ms |
96760 KB |
Output is correct |
37 |
Correct |
513 ms |
142484 KB |
Output is correct |
38 |
Correct |
510 ms |
142328 KB |
Output is correct |
39 |
Correct |
510 ms |
142584 KB |
Output is correct |
40 |
Correct |
490 ms |
133196 KB |
Output is correct |
41 |
Correct |
262 ms |
73208 KB |
Output is correct |
42 |
Correct |
338 ms |
86012 KB |
Output is correct |
43 |
Correct |
835 ms |
139640 KB |
Output is correct |
44 |
Correct |
851 ms |
141304 KB |
Output is correct |
45 |
Correct |
404 ms |
70264 KB |
Output is correct |
46 |
Correct |
411 ms |
70136 KB |
Output is correct |
47 |
Correct |
787 ms |
133496 KB |
Output is correct |
48 |
Correct |
805 ms |
133752 KB |
Output is correct |
49 |
Correct |
17 ms |
1656 KB |
Output is correct |
50 |
Correct |
16 ms |
1400 KB |
Output is correct |
51 |
Correct |
18 ms |
1272 KB |
Output is correct |
52 |
Correct |
5 ms |
376 KB |
Output is correct |
53 |
Correct |
19 ms |
1400 KB |
Output is correct |
54 |
Correct |
17 ms |
1400 KB |
Output is correct |
55 |
Correct |
18 ms |
1400 KB |
Output is correct |
56 |
Correct |
20 ms |
1532 KB |
Output is correct |
57 |
Correct |
19 ms |
1400 KB |
Output is correct |
58 |
Correct |
20 ms |
632 KB |
Output is correct |
59 |
Correct |
16 ms |
888 KB |
Output is correct |
60 |
Correct |
5 ms |
376 KB |
Output is correct |
61 |
Correct |
1569 ms |
426840 KB |
Output is correct |
62 |
Correct |
3616 ms |
925560 KB |
Output is correct |
63 |
Correct |
3651 ms |
929656 KB |
Output is correct |
64 |
Correct |
3699 ms |
929792 KB |
Output is correct |
65 |
Correct |
641 ms |
314744 KB |
Output is correct |
66 |
Correct |
1250 ms |
597624 KB |
Output is correct |
67 |
Correct |
1350 ms |
637048 KB |
Output is correct |
68 |
Runtime error |
2358 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
69 |
Halted |
0 ms |
0 KB |
- |