답안 #281675

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
281675 2020-08-23T09:52:08 Z peti1234 Rectangles (IOI19_rect) C++17
72 / 100
1621 ms 499192 KB
#include <bits/stdc++.h>

using namespace std;
const int c=2502;
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];
vector<pair<pair<int, int>, pair<int, int> > > ch;
void add(int a, int b, int c, int d) {ch.push_back({{a, b}, {c, d}});}

void dfs(int a, int b) {
    v[a][b]=true, db++;
    ks=min(ks, a), ko=min(ko, b), ns=max(ns, a), no=max(no, b);
    if (a>0 && !v[a-1][b] && !t[a-1][b]) dfs(a-1, b);
    if (b>0 && !v[a][b-1] && !t[a][b-1]) dfs(a, b-1);
    if (a+1<n && !v[a+1][b] && !t[a+1][b]) dfs(a+1, b);
    if (b+1<m && !v[a][b+1] && !t[a][b+1]) dfs(a, b+1);
}

long long count_rectangles(vector<vector<int>> sz) {
    n=sz.size(), m=sz[0].size();

    if (n<=3) {
        if (n<3) return 0;
        for (int i=0; i<m; i++) {
            int kezd=sz[1][i], maxi=sz[1][i+1];
            for (int j=i+1; j<m-1; j++) {
                int ert=sz[1][j];
                maxi=max(maxi, ert);
                if (maxi>=kezd || ert>=min(sz[0][j], sz[2][j])) break;
                ans+=(maxi<sz[1][j+1]);
            }
        }
        return ans;
    }

    if (n<=700 && m<=700) {
        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();
                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();
                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++) cout << jobb[i][j] << " ";
            cout << "\n";
        }*/
        for (int i=0; i<n; i++) {
            for (int j=0; j<m; j++) {
                int ba=bal[i][j], jo=jobb[i][j], fe=fel[i][j], l=le[i][j];
                add(i, j, l, jo), add(i, ba, l, j), add(fe, j, i, jo), add(fe, ba, i, j);
                if (jo) add(i, j, le[i][jo], jo);
                if (l) add(i, j, l, jobb[l][j]);
            }
        }
        sort(ch.begin(), ch.end());
        for (int i=0; i<ch.size(); i++) if (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 (mini!=-1 && ks && ko && ns<n-1 && no<m-1 && ks<=ns && ko<=no) {
                int r=0;
                for (int j=ko; j<=no; j++) {
                    if (le[ks][j]!=-1 && le[ks][j]<ns) r++;
                    if (fel[ns][j]>ks) r++;
                }
                for (int i=ks; i<=ns; i++) {
                    if (jobb[i][ko]!=-1 && jobb[i][ko]<no) r++;
                    if (bal[i][no]>ko) r++;
                }
                if (!r) ans++;
            }
        }
        return ans;
    }

    for (int i=0; i<n; i++) for (int j=0; j<m; j++) t[i][j]=sz[i][j];
    for (int i=0; i<n; i++) for (int j=0; j<m; j++) {
        if (!v[i][j] && !sz[i][j]) {
            ns=0, no=0, ks=c, ko=c, db=0;
            dfs(i, j);
            if (ks>0 && ko>0 && ns<n-1 && no<m-1 && (ns-ks+1)*(no-ko+1)==db) ans++;
        }
    }
    return ans;

}




/*
int b1, b2;
vector<vector<int>> b3;
int main()
{
    cin >> b1 >> b2;
    b3.resize(b1);
    for (int i=0; i<b1; i++) for (int j=0; j<b2; j++) {
        int x; cin >> x;
        b3[i].push_back(x);
    }
    cout << count_rectangles(b3);
    return 0;
}
*/
/*
6 5
4 8 7 5 6
7 4 10 3 5
9 7 20 14 2
9 14 7 5 6
5 7 5 2 7
4 5 13 5 6
*/

Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:75:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   75 |         for (int i=0; i<ch.size(); i++) if (ch[i]!=ch[i-1]) {
      |                       ~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 1 ms 896 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 1024 KB Output is correct
11 Correct 2 ms 1024 KB Output is correct
12 Correct 2 ms 1024 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 0 ms 256 KB Output is correct
18 Correct 0 ms 384 KB Output is correct
19 Correct 1 ms 1024 KB Output is correct
20 Correct 1 ms 896 KB Output is correct
21 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 1 ms 896 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 1024 KB Output is correct
11 Correct 2 ms 1024 KB Output is correct
12 Correct 2 ms 1024 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 7 ms 2936 KB Output is correct
18 Correct 9 ms 2936 KB Output is correct
19 Correct 8 ms 2936 KB Output is correct
20 Correct 7 ms 2936 KB Output is correct
21 Correct 8 ms 2936 KB Output is correct
22 Correct 10 ms 2936 KB Output is correct
23 Correct 8 ms 2936 KB Output is correct
24 Correct 5 ms 2300 KB Output is correct
25 Correct 0 ms 256 KB Output is correct
26 Correct 0 ms 384 KB Output is correct
27 Correct 1 ms 1024 KB Output is correct
28 Correct 1 ms 896 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 1 ms 896 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 1024 KB Output is correct
11 Correct 2 ms 1024 KB Output is correct
12 Correct 2 ms 1024 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 7 ms 2936 KB Output is correct
18 Correct 9 ms 2936 KB Output is correct
19 Correct 8 ms 2936 KB Output is correct
20 Correct 7 ms 2936 KB Output is correct
21 Correct 8 ms 2936 KB Output is correct
22 Correct 10 ms 2936 KB Output is correct
23 Correct 8 ms 2936 KB Output is correct
24 Correct 5 ms 2300 KB Output is correct
25 Correct 47 ms 8696 KB Output is correct
26 Correct 49 ms 8824 KB Output is correct
27 Correct 48 ms 8696 KB Output is correct
28 Correct 38 ms 8804 KB Output is correct
29 Correct 51 ms 8692 KB Output is correct
30 Correct 48 ms 8692 KB Output is correct
31 Correct 53 ms 8692 KB Output is correct
32 Correct 50 ms 8688 KB Output is correct
33 Correct 0 ms 256 KB Output is correct
34 Correct 0 ms 384 KB Output is correct
35 Correct 1 ms 1024 KB Output is correct
36 Correct 1 ms 896 KB Output is correct
37 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 1 ms 896 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 1024 KB Output is correct
11 Correct 2 ms 1024 KB Output is correct
12 Correct 2 ms 1024 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 7 ms 2936 KB Output is correct
18 Correct 9 ms 2936 KB Output is correct
19 Correct 8 ms 2936 KB Output is correct
20 Correct 7 ms 2936 KB Output is correct
21 Correct 8 ms 2936 KB Output is correct
22 Correct 10 ms 2936 KB Output is correct
23 Correct 8 ms 2936 KB Output is correct
24 Correct 5 ms 2300 KB Output is correct
25 Correct 47 ms 8696 KB Output is correct
26 Correct 49 ms 8824 KB Output is correct
27 Correct 48 ms 8696 KB Output is correct
28 Correct 38 ms 8804 KB Output is correct
29 Correct 51 ms 8692 KB Output is correct
30 Correct 48 ms 8692 KB Output is correct
31 Correct 53 ms 8692 KB Output is correct
32 Correct 50 ms 8688 KB Output is correct
33 Correct 738 ms 88988 KB Output is correct
34 Correct 483 ms 89016 KB Output is correct
35 Correct 510 ms 88984 KB Output is correct
36 Correct 501 ms 89000 KB Output is correct
37 Correct 1550 ms 89016 KB Output is correct
38 Correct 1621 ms 88924 KB Output is correct
39 Correct 1574 ms 88984 KB Output is correct
40 Correct 1328 ms 87456 KB Output is correct
41 Correct 440 ms 88908 KB Output is correct
42 Correct 465 ms 88984 KB Output is correct
43 Correct 611 ms 88984 KB Output is correct
44 Correct 623 ms 88984 KB Output is correct
45 Correct 315 ms 50384 KB Output is correct
46 Correct 305 ms 44752 KB Output is correct
47 Correct 582 ms 88984 KB Output is correct
48 Correct 578 ms 88984 KB Output is correct
49 Correct 0 ms 256 KB Output is correct
50 Correct 0 ms 384 KB Output is correct
51 Correct 1 ms 1024 KB Output is correct
52 Correct 1 ms 896 KB Output is correct
53 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 384 KB Output is correct
2 Correct 6 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 512 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 512 KB Output is correct
2 Correct 102 ms 28920 KB Output is correct
3 Correct 204 ms 61480 KB Output is correct
4 Correct 203 ms 61816 KB Output is correct
5 Correct 206 ms 61816 KB Output is correct
6 Correct 241 ms 171256 KB Output is correct
7 Correct 519 ms 417912 KB Output is correct
8 Correct 594 ms 499192 KB Output is correct
9 Correct 0 ms 256 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 1024 KB Output is correct
12 Correct 1 ms 896 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 1024 KB Output is correct
3 Correct 2 ms 1024 KB Output is correct
4 Correct 2 ms 1024 KB Output is correct
5 Correct 1 ms 1024 KB Output is correct
6 Correct 2 ms 1024 KB Output is correct
7 Correct 1 ms 896 KB Output is correct
8 Correct 1 ms 640 KB Output is correct
9 Correct 2 ms 1024 KB Output is correct
10 Correct 2 ms 1024 KB Output is correct
11 Correct 2 ms 1024 KB Output is correct
12 Correct 2 ms 1024 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 512 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 0 ms 384 KB Output is correct
17 Correct 7 ms 2936 KB Output is correct
18 Correct 9 ms 2936 KB Output is correct
19 Correct 8 ms 2936 KB Output is correct
20 Correct 7 ms 2936 KB Output is correct
21 Correct 8 ms 2936 KB Output is correct
22 Correct 10 ms 2936 KB Output is correct
23 Correct 8 ms 2936 KB Output is correct
24 Correct 5 ms 2300 KB Output is correct
25 Correct 47 ms 8696 KB Output is correct
26 Correct 49 ms 8824 KB Output is correct
27 Correct 48 ms 8696 KB Output is correct
28 Correct 38 ms 8804 KB Output is correct
29 Correct 51 ms 8692 KB Output is correct
30 Correct 48 ms 8692 KB Output is correct
31 Correct 53 ms 8692 KB Output is correct
32 Correct 50 ms 8688 KB Output is correct
33 Correct 738 ms 88988 KB Output is correct
34 Correct 483 ms 89016 KB Output is correct
35 Correct 510 ms 88984 KB Output is correct
36 Correct 501 ms 89000 KB Output is correct
37 Correct 1550 ms 89016 KB Output is correct
38 Correct 1621 ms 88924 KB Output is correct
39 Correct 1574 ms 88984 KB Output is correct
40 Correct 1328 ms 87456 KB Output is correct
41 Correct 440 ms 88908 KB Output is correct
42 Correct 465 ms 88984 KB Output is correct
43 Correct 611 ms 88984 KB Output is correct
44 Correct 623 ms 88984 KB Output is correct
45 Correct 315 ms 50384 KB Output is correct
46 Correct 305 ms 44752 KB Output is correct
47 Correct 582 ms 88984 KB Output is correct
48 Correct 578 ms 88984 KB Output is correct
49 Correct 7 ms 384 KB Output is correct
50 Correct 6 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 1 ms 384 KB Output is correct
54 Correct 1 ms 384 KB Output is correct
55 Correct 1 ms 512 KB Output is correct
56 Correct 1 ms 384 KB Output is correct
57 Correct 1 ms 384 KB Output is correct
58 Correct 1 ms 384 KB Output is correct
59 Correct 1 ms 384 KB Output is correct
60 Correct 1 ms 512 KB Output is correct
61 Correct 102 ms 28920 KB Output is correct
62 Correct 204 ms 61480 KB Output is correct
63 Correct 203 ms 61816 KB Output is correct
64 Correct 206 ms 61816 KB Output is correct
65 Correct 241 ms 171256 KB Output is correct
66 Correct 519 ms 417912 KB Output is correct
67 Correct 594 ms 499192 KB Output is correct
68 Incorrect 237 ms 67528 KB Output isn't correct
69 Halted 0 ms 0 KB -