# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
479589 | peti1234 | Rectangles (IOI19_rect) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int c=2550;
bool v[c][c], t[c][c];
int n, m, ans, ks, ko, ns, no, mini, db, fel[c][c], le[c][c], bal[c][c], jobb[c][c], cfel[c][c], cle[c][c], cbal[c][c], cjobb[c][c];
unordered_set<pair<pair<int, int>, pair<int, int> > > sol;
void add(int ks, int ko, int ns, int no) {
int mini=min({ks, ko, ns, no});
if (mini!=-1 && ks && ko && ns<n-1 && no<m-1 && ks<=ns && ko<=no) {
if (ks==3) {
//cout << "proba " << ks << " " << ko << " " << ns << " " << no << "\n";
}
int sdb=ns-ks+1, odb=no-ko+1;
bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
bool jooszlop=(le[ks][no]==ns && cle[ks][no]>=odb || fel[ns][no]==ks && cfel[ns][no]>=odb);
//cout << "darab " << sdb << " " << odb << "\n";
//cout << "ertek " << jobb[ns][ko] << " " << bal[ns][no] << "\n";
//cout << "darab " << cjobb[ns][ko] << "\n";
if (josor && jooszlop) {
//cout << "siker " << ks << " " << ko << " " << ns << " " << no << "\n";
/*for (int i=ks; i<=ns; i++) {
for (int j=ko; j<=no; j++) {
if (sz[i][j]>=min({sz[ks-1][j], sz[ns+1][j], sz[i][ko-1], sz[i][no+1]})) {
cout << "...\n";
//cout << i << " " << j << "\n";
//cout << "ertek " << sz[i][j] << "\n";
return 0;
}
}
}*/
pair<pair<int, int>, pair<int, int> > p={{ks, ko}, {ns, no}};
if (sol.find(p)==sol.end()) {
ans++;
sol.insert(p);
}
}
}
}
vector<vector<int>> sz;
bool brute_force(int ks, int ko, int ns, int no) {
for (int i=ks; i<=ns; i++) {
for (int j=ko; j<=no; j++) {
if (sz[i][j]>=min({sz[ks-1][j], sz[ns+1][j], sz[i][ko-1], sz[i][no+1]})) {
//cout << i << " " << j << "\n";
//cout << "ertek " << sz[i][j] << "\n";
return 0;
}
}
}
return 1;
}
bool gyors(int ks, int ko, int ns, int no) {
//cout << "proba " << ks << " " << ko << " " << ns << " " << no << "\n";
int sdb=ns-ks+1, odb=no-ko+1;
//cout << "darab " << sdb << " " << odb << "\n";
bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
bool jooszlop=(le[ks][no]==ns && cle[ks][no]>=odb || fel[ns][no]==ks && cfel[ns][no]>=odb);
//cout << "ertekek " << le[ks][no] << " " << fel[ns][no] << "\n";
//cout << "elromlik " << cle[ks][no] << "\n";
//cout << josor << " " << jooszlop << "\n";
return (josor && jooszlop);
}
/*
5 5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
*/
/*
8 8
240802 1587995 1427863 188743 2698756 1850992 49687 1404055
1902879 1925306 2563972 2903664 525375 2264944 2140527 1162814
2481424 719088 1064586 496976 720141 3125893 2276444 1741080
546865 2422130 2000584 2534743 3820322 3836881 936460 905455
542176 1788941 1454467 3488899 3166602 806200 349711 2467429
2283572 3167542 1022001 2893598 2541467 1310733 311674 3751564
3161793 2034612 614776 3670183 2325816 1707268 3862740 2715886
2310628 2838931 3439134 526672 3878034 3828531 678835 1270375
*/
long long count_rectangles(vector<vector<int>> sz2) {
n=sz2.size(), m=sz2[0].size();
/*sz.resize(m);
for (int i=0; i<m; i++) {
sz[i].resize(n);
}
for (int i=0; i<m; i++) {;
for (int j=0; j<n; j++) {
sz[i][j]=sz2[j][i];
}
}
swap(n, m);*/
sz=sz2;
/*cout << "vege " << n << " " << m;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
cout << sz[i][j] << " ";
}
cout << "\n";
}*/
vector<int> s;
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
while(s.size()>0 && sz[i][j]>=sz[i][s.back()]) jobb[i][1+s.back()]=j-1, s.pop_back();
s.push_back(j);
}
while(s.size()>0) jobb[i][1+s.back()]=-1, s.pop_back();
for (int j=m-1; j>=0; j--) {
while(s.size()>0 && sz[i][j]>=sz[i][s.back()]) bal[i][-1+s.back()]=j+1, s.pop_back();
if (j) s.push_back(j);
}
while(s.size()>0) bal[i][-1+s.back()]=-1, s.pop_back();
}
for (int j=0; j<m; j++) {
for (int i=0; i<n; i++) {
while(s.size()>0 && sz[i][j]>=sz[s.back()][j]) le[1+s.back()][j]=i-1, s.pop_back();
s.push_back(i);
}
while(s.size()>0) le[1+s.back()][j]=-1, s.pop_back();
for (int i=n-1; i>=0; i--) {
while(s.size()>0 && sz[i][j]>=sz[s.back()][j]) fel[-1+s.back()][j]=i+1, s.pop_back();
if (i) s.push_back(i);
}
while(s.size()>0) fel[-1+s.back()][j]=-1, s.pop_back();
}
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (i==0 || j==0 || i==n-1 || j==m-1) {
bal[i][j]=jobb[i][j]=le[i][j]=fel[i][j]=-c;
}
}
}
for (int i=1; i<n-1; i++) {
for (int j=1; j<m-1; j++) {
int pos;
pos=jobb[i][j];
if (pos!=-1) {
if (jobb[i-1][j]==pos) cjobb[i][j]=cjobb[i-1][j];
if (bal[i-1][pos]==j) cjobb[i][j]=cbal[i-1][pos];
cjobb[i][j]++;
}
pos=bal[i][j];
if (pos!=-1) {
if (bal[i-1][j]==pos) cbal[i][j]=cbal[i-1][j];
if (jobb[i-1][pos]==j) cbal[i][j]=cjobb[i-1][pos];
cbal[i][j]++;
}
}
}
for (int j=1; j<m-1; j++) {
for (int i=1; i<n-1; i++) {
int pos;
pos=le[i][j];
if (pos!=-1) {
//if (i==1 && j==3) cout << "nagyonfontos " << pos << "\n";
//if (i==1 && j==3) cout << fel[pos][j-1] << " " << cfel[pos][j-1] << "\n";
if (le[i][j-1]==pos) cle[i][j]=cle[i][j-1];
if (fel[pos][j-1]==i) cle[i][j]=cfel[pos][j-1];
cle[i][j]++;
}
pos=fel[i][j];
if (pos!=-1) {
if (fel[i][j-1]==pos) cfel[i][j]=cfel[i][j-1];
if (le[pos][j-1]==i) cfel[i][j]=cle[pos][j-1];
cfel[i][j]++;
}
}
}
//cout << "kezd " << fel[2][2] << " " << cfel[2][2] << "\n";
//cout << gyors(1, 2, 2, 3) << "\n";
//return 0;
/*for (int ks=1; ks<n-1; ks++) {
for (int ns=ks; ns<n-1; ns++) {
for (int ko=1; ko<m-1; ko++) {
for (int no=ko; no<m-1; no++) {
if (brute_force(ks, ko, ns, no)!=gyors(ks, ko, ns, no)) {
cout << "....\n";
cout << ks << " " << ko << " " << ns << " " << no << "\n";
cout << sz[ks][ko] << endl;
return 0;
}
}
}
}
}*/
//return 0;
for (int i=1; i<n-1; i++) {
for (int j=1; j<m-1; j++) {
int ba=bal[i][j], jo=jobb[i][j], fe=fel[i][j], l=le[i][j];
if (i==3 && j==3) {
//cout << "fontos " << i << " " << j << "\n" << "iranyok " << ba << " " << jo << " " << fe << " " << l << "\n";
}
add(i, j, l, jo), add(i, ba, l, j), add(fe, j, i, jo), add(fe, ba, i, j);
if (jo<-1 || jo>=2500) {
cout << "....\n";
return 0;
}
if (l<-1 || l>=2500) {
cout << "rossz l\n";
}
if (jo!=-1) add(i, j, le[i][jo], jo);
if (l!=-1) add(i, j, l, jobb[l][j]);
}
}
/*sort(ch.begin(), ch.end());
for (int i=0; i<ch.size(); i++) if (i==0 || ch[i]!=ch[i-1]) {
ks=ch[i].first.first, ko=ch[i].first.second, ns=ch[i].second.first, no=ch[i].second.second, mini=min({ks, ko, ns, no});
//if (ks!=1 || ns!=3 || ko!=3 || no!=3) continue;
if (mini!=-1 && ks && ko && ns<n-1 && no<m-1 && ks<=ns && ko<=no) {
if (ks==3) {
//cout << "proba " << ks << " " << ko << " " << ns << " " << no << "\n";
}
int sdb=ns-ks+1, odb=no-ko+1;
bool josor=(jobb[ns][ko]==no && cjobb[ns][ko]>=sdb || bal[ns][no]==ko && cbal[ns][no]>=sdb);
bool jooszlop=(le[ks][no]==ns && cle[ks][no]>=odb || fel[ns][no]==ks && cfel[ns][no]>=odb);
//cout << "darab " << sdb << " " << odb << "\n";
//cout << "ertek " << jobb[ns][ko] << " " << bal[ns][no] << "\n";
//cout << "darab " << cjobb[ns][ko] << "\n";
if (josor && jooszlop) {
//cout << "siker " << ks << " " << ko << " " << ns << " " << no << "\n";
for (int i=ks; i<=ns; i++) {
for (int j=ko; j<=no; j++) {
if (sz[i][j]>=min({sz[ks-1][j], sz[ns+1][j], sz[i][ko-1], sz[i][no+1]})) {
cout << "...\n";
//cout << i << " " << j << "\n";
//cout << "ertek " << sz[i][j] << "\n";
return 0;
}
}
}
ans++;
}
}
}*/
return ans;
}
/*
int b1, b2;
vector<vector<int>> b3;
int main()
{
cin >> b1 >> b2;
//b1=b2=2500;
b3.resize(b1);
for (int i=0; i<b1; i++) for (int j=0; j<b2; j++) {
int x; cin >> x;
//int x=0;
b3[i].push_back(x);
}
cout << count_rectangles(b3);
return 0;
}
*/
/*
30 13
2314857 2951714 2551799 1262649 877317 2582030 1583139 3582015 1970170 2496877 252584 1959948 809239
1363336 486953 3562759 2227001 1996347 1994014 2317663 3232136 648728 2110306 2235717 2759784 2530855
258050 2824581 1243255 3198783 565865 231889 3727287 818081 2547404 3225075 3106983 1408265 2995366
1583118 3603184 3629350 727853 1632409 2090115 1933387 83572 2600107 3982487 2531332 427817 3615973
3440551 482330 832180 2483232 1194814 1255098 3085141 3601105 2004042 2837153 2452976 2727248 874027
1258869 875364 1785054 825988 2603807 616044 1136447 2155243 2614282 71407 1427398 536403 3683916
1342075 3590476 2175341 451150 616801 2691611 3044834 1464107 2158164 1823214 2852005 3843741 3348365
3275922 56468 169199 1391242 3905121 2188314 2243511 542710 891329 1856147 1366638 3533582 2388423
414756 228308 1784355 2073402 285249 2482853 950126 2479306 3261135 2401265 1132321 2475699 3500210
3639115 932461 412166 3184319 2485044 3895091 2327009 850550 583679 1610883 1517452 756022 1390820
2087853 1101237 515757 1513050 2877836 2764525 2414358 1468916 2674305 2446104 295599 841409 3215968
3119789 2516751 1471712 2600507 1997824 1353835 3994432 2006376 1507633 1140685 2177209 2927071 3424545
1674397 2008442 3544943 2924176 935153 2249156 3802239 1864176 1019795 1979123 1766617 694370 1733081
3151503 2112818 3513114 3136472 1994096 3379937 3919391 2547259 2798496 1308083 3156335 3855647 1504459
1023261 3017057 2016620 1439369 14943 1514149 1117474 1422903 1921583 2637168 1213085 185921 774705
932281 1562426 2076680 2283504 608334 3233863 1378085 2061191 1317930 3481445 2062302 1298203 2405802
2744660 2002176 1689886 947679 682094 2670377 1742765 2807882 2301713 2735402 1556615 2514209 1582309
1972102 1431422 2324079 2151483 2611035 2163733 709144 2889064 2205017 2880931 52320 1498527 2933993
1209767 439593 3020508 3721396 1036704 142701 3927559 1608169 1743546 1625063 46581 2017411 808968
263329 3136612 293841 2117846 1207981 247502 2293469 2198968 2702265 1642631 637723 3263373 3860851
240802 1587995 1427863 188743 2698756 1850992 49687 1404055 830973 560046 1793635 882126 1947220
1902879 1925306 2563972 2903664 525375 2264944 2140527 1162814 1977719 1265162 1808117 2896293 956308
2481424 719088 1064586 496976 720141 3125893 2276444 1741080 881467 2570549 3588324 2535402 1767516
546865 2422130 2000584 2534743 3820322 3836881 936460 905455 2021753 279793 2611693 3353033 2670364
542176 1788941 1454467 3488899 3166602 806200 349711 2467429 3374884 2852311 1490036 223291 1904067
2283572 3167542 1022001 2893598 2541467 1310733 311674 3751564 762621 971844 1004691 2594274 3728923
3161793 2034612 614776 3670183 2325816 1707268 3862740 2715886 3833150 1644459 358172 2247126 3008803
2310628 2838931 3439134 526672 3878034 3828531 678835 1270375 1931564 574357 2934272 3603169 2515791
213795 737446 3581205 1683399 3125307 140091 3694357 3619274 1694258 356442 2055920 3991030 445263
2124254 193277 3567661 3047095 2066338 1717492 1375193 2159796 1294986 1069001 1972558 1182016 1628337
*/
/*
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 3 6
5 7 5 2 7
4 5 13 5 6
*/