답안 #1066235

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1066235 2024-08-19T16:48:19 Z arbuzick Rectangles (IOI19_rect) C++17
100 / 100
2319 ms 969508 KB
#include "rect.h"

#include <bits/stdc++.h>

using namespace std;

constexpr int inf = 1e9 + 7;

struct SegmentTree {
    int R;

    vector<int> mn, mx;

    SegmentTree(vector<int> vals) {
        R = 1;
        while (R < (int)vals.size()) {
            R *= 2;
        }
        mn.assign(R * 2, inf);
        mx.assign(R * 2, -inf);
        for (int i = 0; i < (int)vals.size(); ++i) {
            mn[i + R] = mx[i + R] = vals[i];
        }
        for (int i = R - 1; i > 0; --i) {
            mn[i] = min(mn[i * 2], mn[i * 2 + 1]);
            mx[i] = max(mx[i * 2], mx[i * 2 + 1]);
        }
    }

    pair<int, int> get(int l, int r) {
        l += R;
        r += R;
        pair<int, int> ans = make_pair(inf, -inf);
        while (l < r) {
            if (l & 1) {
                ans.first = min(ans.first, mn[l]);
                ans.second = max(ans.second, mx[l]);
                l++;
            }
            if (r & 1) {
                --r;
                ans.first = min(ans.first, mn[r]);
                ans.second = max(ans.second, mx[r]);
            }
            l >>= 1;
            r >>= 1;
        }
        return ans;
    }
};

long long count_rectangles(vector<vector<int>> a) {
    int n = a.size(), m = a[0].size();
    vector<vector<int>> up(n, vector<int>(m, -1)), down(n, vector<int>(m, n)), left(n, vector<int>(m, -1)), right(n, vector<int>(m, m));
    for (int i = 0; i < n; ++i) {
        vector<int> st;
        for (int j = 0; j < m; ++j) {
            while (!st.empty() && a[i][st.back()] <= a[i][j]) {
                right[i][st.back()] = j;
                st.pop_back();
            }
            st.push_back(j);
        }
        st.clear();
        for (int j = m - 1; j >= 0; --j) {
            while (!st.empty() && a[i][st.back()] <= a[i][j]) {
                left[i][st.back()] = j;
                st.pop_back();
            }
            st.push_back(j);
        }
    }
    for (int j = 0; j < m; ++j) {
        vector<int> st;
        for (int i = 0; i < n; ++i) {
            while (!st.empty() && a[st.back()][j] <= a[i][j]) {
                down[st.back()][j] = i;
                st.pop_back();
            }
            st.push_back(i);
        }
        st.clear();
        for (int i = n - 1; i >= 0; --i) {
            while (!st.empty() && a[st.back()][j] <= a[i][j]) {
                up[st.back()][j] = i;
                st.pop_back();
            }
            st.push_back(i);
        }
    }
    vector<SegmentTree> upTree, downTree, leftTree, rightTree;
    for (int i = 0; i < n; ++i) {
        upTree.emplace_back(up[i]);
        downTree.emplace_back(down[i]);
    }
    for (int j = 0; j < m; ++j) {
        vector<int> leftNw, rightNw;
        for (int i = 0; i < n; ++i) {
            leftNw.push_back(left[i][j]);
            rightNw.push_back(right[i][j]);
        }
        leftTree.emplace_back(leftNw);
        rightTree.emplace_back(rightNw);
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            up[i][j] = -1;
            down[i][j] = n;
            left[i][j] = -1;
            right[i][j] = m;
        }
    }
    for (int i = 0; i < n; ++i) {
        vector<int> st;
        for (int j = 0; j < m; ++j) {
            while (!st.empty() && a[i][st.back()] < a[i][j]) {
                right[i][st.back()] = j;
                st.pop_back();
            }
            st.push_back(j);
        }
        st.clear();
        for (int j = m - 1; j >= 0; --j) {
            while (!st.empty() && a[i][st.back()] < a[i][j]) {
                left[i][st.back()] = j;
                st.pop_back();
            }
            st.push_back(j);
        }
    }
    for (int j = 0; j < m; ++j) {
        vector<int> st;
        for (int i = 0; i < n; ++i) {
            while (!st.empty() && a[st.back()][j] < a[i][j]) {
                down[st.back()][j] = i;
                st.pop_back();
            }
            st.push_back(i);
        }
        st.clear();
        for (int i = n - 1; i >= 0; --i) {
            while (!st.empty() && a[st.back()][j] < a[i][j]) {
                up[st.back()][j] = i;
                st.pop_back();
            }
            st.push_back(i);
        }
    }
    vector<pair<pair<int, int>, pair<int, int>>> rects;
    for (int i = 1; i + 1 < n; ++i) {
        for (int j = 1; j + 1 < m; ++j) {
            if (up[i][j] != -1 && down[i][j] != n && left[i][j] != -1 && right[i][j] != m) {
                rects.emplace_back(make_pair(up[i][j], left[i][j]), make_pair(down[i][j], right[i][j]));
            }
        }
    }
    sort(rects.begin(), rects.end());
    rects.resize(unique(rects.begin(), rects.end()) - rects.begin());
    long long ans = 0;
    for (int i = 0; i < (int)rects.size(); ++i) {
        int u = rects[i].first.first, l = rects[i].first.second, d = rects[i].second.first, r = rects[i].second.second;
        if (downTree[u].get(l + 1, r).first >= d && upTree[d].get(l + 1, r).second <= u && rightTree[l].get(u + 1, d).first >= r && leftTree[r].get(u + 1, d).second <= l) {
            ans++;
        }
    }
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 2 ms 1372 KB Output is correct
24 Correct 2 ms 1372 KB Output is correct
25 Correct 2 ms 1372 KB Output is correct
26 Correct 3 ms 1372 KB Output is correct
27 Correct 2 ms 1372 KB Output is correct
28 Correct 3 ms 1368 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 3 ms 1368 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 10 ms 6096 KB Output is correct
31 Correct 10 ms 6100 KB Output is correct
32 Correct 10 ms 5944 KB Output is correct
33 Correct 10 ms 5336 KB Output is correct
34 Correct 13 ms 5844 KB Output is correct
35 Correct 12 ms 6100 KB Output is correct
36 Correct 14 ms 6132 KB Output is correct
37 Correct 14 ms 6100 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 3 ms 1368 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 10 ms 6096 KB Output is correct
26 Correct 10 ms 6100 KB Output is correct
27 Correct 10 ms 5944 KB Output is correct
28 Correct 10 ms 5336 KB Output is correct
29 Correct 13 ms 5844 KB Output is correct
30 Correct 12 ms 6100 KB Output is correct
31 Correct 14 ms 6132 KB Output is correct
32 Correct 14 ms 6100 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 61 ms 60336 KB Output is correct
39 Correct 84 ms 60248 KB Output is correct
40 Correct 63 ms 60208 KB Output is correct
41 Correct 60 ms 60244 KB Output is correct
42 Correct 136 ms 68812 KB Output is correct
43 Correct 137 ms 68752 KB Output is correct
44 Correct 132 ms 69068 KB Output is correct
45 Correct 127 ms 66748 KB Output is correct
46 Correct 131 ms 62400 KB Output is correct
47 Correct 122 ms 67264 KB Output is correct
48 Correct 155 ms 68528 KB Output is correct
49 Correct 167 ms 70424 KB Output is correct
50 Correct 82 ms 35276 KB Output is correct
51 Correct 82 ms 35008 KB Output is correct
52 Correct 170 ms 68308 KB Output is correct
53 Correct 164 ms 69320 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2140 KB Output is correct
2 Correct 3 ms 1644 KB Output is correct
3 Correct 3 ms 1884 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 5 ms 1884 KB Output is correct
6 Correct 4 ms 1884 KB Output is correct
7 Correct 3 ms 1884 KB Output is correct
8 Correct 3 ms 1884 KB Output is correct
9 Correct 3 ms 1884 KB Output is correct
10 Correct 2 ms 1372 KB Output is correct
11 Correct 3 ms 1436 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 659 ms 414384 KB Output is correct
8 Correct 1327 ms 865444 KB Output is correct
9 Correct 1365 ms 868564 KB Output is correct
10 Correct 1371 ms 869196 KB Output is correct
11 Correct 347 ms 398420 KB Output is correct
12 Correct 699 ms 772432 KB Output is correct
13 Correct 756 ms 802384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 424 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 2 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 2 ms 1372 KB Output is correct
21 Correct 3 ms 1372 KB Output is correct
22 Correct 2 ms 1372 KB Output is correct
23 Correct 3 ms 1368 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 10 ms 6096 KB Output is correct
26 Correct 10 ms 6100 KB Output is correct
27 Correct 10 ms 5944 KB Output is correct
28 Correct 10 ms 5336 KB Output is correct
29 Correct 13 ms 5844 KB Output is correct
30 Correct 12 ms 6100 KB Output is correct
31 Correct 14 ms 6132 KB Output is correct
32 Correct 14 ms 6100 KB Output is correct
33 Correct 61 ms 60336 KB Output is correct
34 Correct 84 ms 60248 KB Output is correct
35 Correct 63 ms 60208 KB Output is correct
36 Correct 60 ms 60244 KB Output is correct
37 Correct 136 ms 68812 KB Output is correct
38 Correct 137 ms 68752 KB Output is correct
39 Correct 132 ms 69068 KB Output is correct
40 Correct 127 ms 66748 KB Output is correct
41 Correct 131 ms 62400 KB Output is correct
42 Correct 122 ms 67264 KB Output is correct
43 Correct 155 ms 68528 KB Output is correct
44 Correct 167 ms 70424 KB Output is correct
45 Correct 82 ms 35276 KB Output is correct
46 Correct 82 ms 35008 KB Output is correct
47 Correct 170 ms 68308 KB Output is correct
48 Correct 164 ms 69320 KB Output is correct
49 Correct 4 ms 2140 KB Output is correct
50 Correct 3 ms 1644 KB Output is correct
51 Correct 3 ms 1884 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 5 ms 1884 KB Output is correct
54 Correct 4 ms 1884 KB Output is correct
55 Correct 3 ms 1884 KB Output is correct
56 Correct 3 ms 1884 KB Output is correct
57 Correct 3 ms 1884 KB Output is correct
58 Correct 2 ms 1372 KB Output is correct
59 Correct 3 ms 1436 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 659 ms 414384 KB Output is correct
62 Correct 1327 ms 865444 KB Output is correct
63 Correct 1365 ms 868564 KB Output is correct
64 Correct 1371 ms 869196 KB Output is correct
65 Correct 347 ms 398420 KB Output is correct
66 Correct 699 ms 772432 KB Output is correct
67 Correct 756 ms 802384 KB Output is correct
68 Correct 0 ms 344 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 1 ms 348 KB Output is correct
71 Correct 0 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 881 ms 836728 KB Output is correct
74 Correct 898 ms 836760 KB Output is correct
75 Correct 1014 ms 836768 KB Output is correct
76 Correct 877 ms 836728 KB Output is correct
77 Correct 2277 ms 969340 KB Output is correct
78 Correct 1186 ms 521740 KB Output is correct
79 Correct 1280 ms 546596 KB Output is correct
80 Correct 2119 ms 944200 KB Output is correct
81 Correct 1239 ms 537464 KB Output is correct
82 Correct 1622 ms 705896 KB Output is correct
83 Correct 2129 ms 969392 KB Output is correct
84 Correct 1168 ms 529836 KB Output is correct
85 Correct 2087 ms 969508 KB Output is correct
86 Correct 2069 ms 955396 KB Output is correct
87 Correct 1257 ms 537244 KB Output is correct
88 Correct 2307 ms 968892 KB Output is correct
89 Correct 2319 ms 969264 KB Output is correct
90 Correct 2256 ms 969260 KB Output is correct