Submission #264772

# Submission time Handle Problem Language Result Execution time Memory
264772 2020-08-14T09:21:03 Z hamerin Rectangles (IOI19_rect) C++17
59 / 100
5000 ms 147960 KB
#include "rect.h"

#include <bits/stdc++.h>

#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

using namespace std;

using i64 = long long;
using d64 = long double;
using pi = pair<int, int>;
using pli = pair<i64, i64>;
using ti = tuple<int, int, int>;
using tli = tuple<i64, i64, i64>;

#define iterall(cont) cont.begin(), cont.end()
#define prec(n) setprecision(n) << fixed

vector<pi> findLegalTuple(const vector<int> &v) {
    vector<pi> ret;
    vector<size_t> seq;  // monotonic
    size_t sn = 0;

    for (size_t i = 0; i < v.size(); i++) {
        while (sn >= 1 && v[seq[sn - 1]] < v[i]) {
            seq.pop_back(), --sn;
            if (sn >= 1 && i - seq[sn - 1] >= 2) ret.emplace_back(seq[sn - 1], i);
        }
        if (sn >= 1 && v[seq[sn - 1]] == v[i]) seq.pop_back(), --sn;

        seq.emplace_back(i), ++sn;
    }

    return ret;
}

// 이거 N^3이라서 쫌 이따 바꿔야지
vector<pi> getMaxPos(const vector<int> &v) {
    size_t N = v.size();
    vector<pi> ret(N, {-1, -1});

    for (int i = N - 1; i >= 0; i--) {
        for (int j = i + 1; j < N; j++) {
            if (ret[j].first == -1 && v[i] > v[j]) ret[j].first = i;
        }
    }

    for (int i = 0; i < N; i++) {
        for (int j = 0; j < i; j++) {
            if (ret[j].second == -1 && v[i] > v[j]) ret[j].second = i;
        }
    }

    return ret;
}

vector<pi> processToIntervals(const vector<pi> &v) {
    vector<pi> ret;

    for (auto [el, _] : v) {
        if (!ret.empty() && ret.back().second == el - 1)
            ret.back().second++;
        else
            ret.emplace_back(el, el);
    }

    return ret;
}

bool intervalBinarySearch(const vector<pi> &v, pi t) {
    if (v.empty()) return false;

    const size_t N = v.size();

    int r1 = -1;
    {
        size_t s = 0, e = N - 1;

        while (e - s >= 2) {
            size_t m = (s + e) >> 1;
            if (v[m].first <= t.first && t.first <= v[m].second) {
                r1 = m;
                break;
            }

            if (v[m].second < t.first) s = m + 1;
            if (v[m].first > t.first) e = m - 1;
        }

        if (v[s].first <= t.first && t.first <= v[s].second) r1 = s;
        if (v[e].first <= t.first && t.first <= v[e].second) r1 = e;
    }

    int r2 = -1;
    {
        size_t s = 0, e = N - 1;

        while (e - s >= 2) {
            size_t m = (s + e) >> 1;
            if (v[m].first <= t.second && t.second <= v[m].second) {
                r2 = m;
                break;
            }

            if (v[m].second < t.second) s = m + 1;
            if (v[m].first > t.second) e = m - 1;
        }

        if (v[s].first <= t.second && t.second <= v[s].second) r2 = s;
        if (v[e].first <= t.second && t.second <= v[e].second) r2 = e;
    }

    return r1 != -1 && r2 != -1 && r1 == r2;
}

i64 _hs(i64 a, i64 b, i64 c, i64 d) {
	i64 x = 2501;
	return x*x*x*a+x*x*b+x*c+d;
}

i64 count_rectangles(vector<vector<int>> a) {
	const size_t N = a.size();
    const size_t M = a[0].size();

    // transpose
    vector<vector<int>> b(M, vector<int>(N));
    for (int i = 0; i < N; i++)
        for (int j = 0; j < M; j++) b[j][i] = a[i][j];

    // get Max Position
    vector<vector<pi>> aM(N, vector<pi>(M));
    vector<vector<pi>> bM(M, vector<pi>(N));

    for (int i = 0; i < N; i++) aM[i] = getMaxPos(a[i]);
    for (int i = 0; i < M; i++) bM[i] = getMaxPos(b[i]);

    // find Legal Tuple && assign
    vector<vector<vector<pi>>> aL(M, decltype(aL)::value_type(M));
    vector<vector<vector<pi>>> bL(N, decltype(bL)::value_type(N));

    for (int i = 0; i < N; i++) {
        auto Lg = findLegalTuple(a[i]);
        for (auto [l, r] : Lg) aL[l][r].emplace_back(i, 0);
    }
    for (int i = 0; i < M; i++) {
        auto Lg = findLegalTuple(b[i]);
        for (auto [l, r] : Lg) bL[l][r].emplace_back(i, 0);
    }

    for (auto &vec : aL)
        for (auto &el : vec) el = processToIntervals(el);
    for (auto &vec : bL)
        for (auto &el : vec) el = processToIntervals(el);

    // Main Logic
	unordered_set<i64> us;
	
	for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            auto [L, R] = aM[i][j];
            auto [T, B] = bM[j][i];
            auto H = _hs(L, R, T, B);
			if (L == -1 || R == -1 || T == -1 || B == -1) continue;
			if (us.find(H) != us.end()) continue;

            if (intervalBinarySearch(aL[L][R], {T + 1, B - 1}) &&
                intervalBinarySearch(bL[T][B], {L + 1, R - 1}))
                us.insert(H);
        }
    }

    return us.size();
}

Compilation message

rect.cpp: In function 'std::vector<std::pair<int, int> > getMaxPos(const std::vector<int>&)':
rect.cpp:44:31: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   44 |         for (int j = i + 1; j < N; j++) {
      |                             ~~^~~
rect.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for (int i = 0; i < N; i++) {
      |                     ~~^~~
rect.cpp: In function 'i64 count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:128:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  128 |     for (int i = 0; i < N; i++)
      |                     ~~^~~
rect.cpp:129:27: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  129 |         for (int j = 0; j < M; j++) b[j][i] = a[i][j];
      |                         ~~^~~
rect.cpp:135:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  135 |     for (int i = 0; i < N; i++) aM[i] = getMaxPos(a[i]);
      |                     ~~^~~
rect.cpp:136:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  136 |     for (int i = 0; i < M; i++) bM[i] = getMaxPos(b[i]);
      |                     ~~^~~
rect.cpp:142:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  142 |     for (int i = 0; i < N; i++) {
      |                     ~~^~~
rect.cpp:146:23: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  146 |     for (int i = 0; i < M; i++) {
      |                     ~~^~~
rect.cpp:159:20: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  159 |  for (int i = 0; i < N; i++) {
      |                  ~~^~~
rect.cpp:160:27: warning: comparison of integer expressions of different signedness: 'int' and 'const size_t' {aka 'const long unsigned int'} [-Wsign-compare]
  160 |         for (int j = 0; j < M; j++) {
      |                         ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 1 ms 256 KB Output is correct
18 Correct 1 ms 256 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 308 KB Output is correct
21 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 4 ms 1024 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 6 ms 1024 KB Output is correct
23 Correct 5 ms 1024 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 1 ms 256 KB Output is correct
26 Correct 1 ms 256 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 308 KB Output is correct
29 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 4 ms 1024 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 6 ms 1024 KB Output is correct
23 Correct 5 ms 1024 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 30 ms 5052 KB Output is correct
26 Correct 30 ms 5040 KB Output is correct
27 Correct 31 ms 5052 KB Output is correct
28 Correct 46 ms 3832 KB Output is correct
29 Correct 36 ms 4728 KB Output is correct
30 Correct 37 ms 4728 KB Output is correct
31 Correct 35 ms 4472 KB Output is correct
32 Correct 35 ms 4480 KB Output is correct
33 Correct 1 ms 256 KB Output is correct
34 Correct 1 ms 256 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 308 KB Output is correct
37 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 4 ms 1024 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 6 ms 1024 KB Output is correct
23 Correct 5 ms 1024 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 30 ms 5052 KB Output is correct
26 Correct 30 ms 5040 KB Output is correct
27 Correct 31 ms 5052 KB Output is correct
28 Correct 46 ms 3832 KB Output is correct
29 Correct 36 ms 4728 KB Output is correct
30 Correct 37 ms 4728 KB Output is correct
31 Correct 35 ms 4472 KB Output is correct
32 Correct 35 ms 4480 KB Output is correct
33 Correct 851 ms 52344 KB Output is correct
34 Correct 815 ms 52344 KB Output is correct
35 Correct 842 ms 52344 KB Output is correct
36 Correct 814 ms 52344 KB Output is correct
37 Correct 856 ms 56292 KB Output is correct
38 Correct 850 ms 56308 KB Output is correct
39 Correct 857 ms 56288 KB Output is correct
40 Correct 782 ms 52988 KB Output is correct
41 Correct 1328 ms 40680 KB Output is correct
42 Correct 1198 ms 42644 KB Output is correct
43 Correct 819 ms 54640 KB Output is correct
44 Correct 772 ms 54748 KB Output is correct
45 Correct 319 ms 30488 KB Output is correct
46 Correct 334 ms 30592 KB Output is correct
47 Correct 751 ms 51848 KB Output is correct
48 Correct 768 ms 51944 KB Output is correct
49 Correct 1 ms 256 KB Output is correct
50 Correct 1 ms 256 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 308 KB Output is correct
53 Correct 0 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 147960 KB Output is correct
2 Correct 126 ms 106744 KB Output is correct
3 Correct 181 ms 147832 KB Output is correct
4 Correct 0 ms 256 KB Output is correct
5 Correct 164 ms 147832 KB Output is correct
6 Correct 168 ms 147832 KB Output is correct
7 Correct 172 ms 147960 KB Output is correct
8 Correct 171 ms 147832 KB Output is correct
9 Correct 180 ms 147704 KB Output is correct
10 Correct 161 ms 147576 KB Output is correct
11 Correct 165 ms 147704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Execution timed out 5059 ms 79096 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 256 KB Output is correct
2 Correct 1 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 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 0 ms 256 KB Output is correct
14 Correct 1 ms 256 KB Output is correct
15 Correct 1 ms 256 KB Output is correct
16 Correct 0 ms 256 KB Output is correct
17 Correct 4 ms 1024 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
19 Correct 4 ms 1024 KB Output is correct
20 Correct 6 ms 896 KB Output is correct
21 Correct 5 ms 1024 KB Output is correct
22 Correct 6 ms 1024 KB Output is correct
23 Correct 5 ms 1024 KB Output is correct
24 Correct 3 ms 640 KB Output is correct
25 Correct 30 ms 5052 KB Output is correct
26 Correct 30 ms 5040 KB Output is correct
27 Correct 31 ms 5052 KB Output is correct
28 Correct 46 ms 3832 KB Output is correct
29 Correct 36 ms 4728 KB Output is correct
30 Correct 37 ms 4728 KB Output is correct
31 Correct 35 ms 4472 KB Output is correct
32 Correct 35 ms 4480 KB Output is correct
33 Correct 851 ms 52344 KB Output is correct
34 Correct 815 ms 52344 KB Output is correct
35 Correct 842 ms 52344 KB Output is correct
36 Correct 814 ms 52344 KB Output is correct
37 Correct 856 ms 56292 KB Output is correct
38 Correct 850 ms 56308 KB Output is correct
39 Correct 857 ms 56288 KB Output is correct
40 Correct 782 ms 52988 KB Output is correct
41 Correct 1328 ms 40680 KB Output is correct
42 Correct 1198 ms 42644 KB Output is correct
43 Correct 819 ms 54640 KB Output is correct
44 Correct 772 ms 54748 KB Output is correct
45 Correct 319 ms 30488 KB Output is correct
46 Correct 334 ms 30592 KB Output is correct
47 Correct 751 ms 51848 KB Output is correct
48 Correct 768 ms 51944 KB Output is correct
49 Correct 172 ms 147960 KB Output is correct
50 Correct 126 ms 106744 KB Output is correct
51 Correct 181 ms 147832 KB Output is correct
52 Correct 0 ms 256 KB Output is correct
53 Correct 164 ms 147832 KB Output is correct
54 Correct 168 ms 147832 KB Output is correct
55 Correct 172 ms 147960 KB Output is correct
56 Correct 171 ms 147832 KB Output is correct
57 Correct 180 ms 147704 KB Output is correct
58 Correct 161 ms 147576 KB Output is correct
59 Correct 165 ms 147704 KB Output is correct
60 Correct 1 ms 256 KB Output is correct
61 Execution timed out 5059 ms 79096 KB Time limit exceeded
62 Halted 0 ms 0 KB -