Submission #1071228

# Submission time Handle Problem Language Result Execution time Memory
1071228 2024-08-23T06:04:33 Z j_vdd16 Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 595796 KB
#include "rect.h"

#include <algorithm>
#include <bitset>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits.h>
#include <math.h>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

//#define int long long
#define ll long long
#define loop(X, N) for(int X = 0; X < (N); X++)
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;

struct MSTree {
    int n, N;
    vvi tree;

    MSTree() = default;
    MSTree(const vvi& values, int sz) {
        n = sz;
        N = 1;
        while (N < n) N *= 2;

        tree = vvi(2 * N);
        loop(i, values.size()) {
            for (int j : values[i]) {
                tree[N + j].push_back(i); 
            }
        }

        for (int i = N - 1; i >= 1; i--) {
            tree[i] = merge(tree[2 * i], tree[2 * i + 1]);
        }
    }

    vi merge(const vi& a, const vi& b) {
        int idx1 = 0, idx2 = 0;
        vi out;
        while (idx1 < a.size() && idx2 < b.size()) {
            if (a[idx1] < b[idx2]) {
                idx1++;
            }
            else if (a[idx1] > b[idx2]) {
                idx2++;
            }
            else {
                out.push_back(a[idx1]);
                idx1++;
                idx2++;
            }
        }

        return out;
    }

    vi getRange(int l, int r, int i = 1, int tl = 0, int tr = -1) {
        if (tr == -1) {
            tr = N;
        }

        if (tr <= l || tl >= r) {
            cout << "ERROR" << endl;
            return {};
        }

        if (tl >= l && tr <= r) {
            return tree[i];
        }

        int tm = (tl + tr) / 2;
        if (tm >= r) {
            return getRange(l, r, i * 2, tl, tm);
        }
        if (tm <= l) {
            return getRange(l, r, i * 2 + 1, tm, tr);
        }
        return merge(getRange(l, r, i * 2, tl, tm), getRange(l, r, i * 2 + 1, tm, tr));
    }
};

long long count_rectangles(vvi a) {
	int n = a.size();
    int m = a[0].size();

    vector<vvi> validRows(m, vvi(m));
    for (int i = 1; i < n - 1; i++) {
        map<int, vi, greater<int>> values;
        loop(j, m) {
            values[a[i][j]].push_back(j);
        }

        map<int, int> rec;
        for (auto [val, vec] : values) {
            loop(idx, vec.size()) {
                if (rec.size() == 0) {
                    rec[vec[idx]] = val;
                    continue;
                }

                auto next = rec.upper_bound(vec[idx]);
                if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
                    validRows[vec[idx]][next->first].push_back(i);
                }

                if (next != rec.begin()) {
                    next--;
                    if (vec[idx] - next->first > 1) {
                        validRows[next->first][vec[idx]].push_back(i);
                    }
                }

                rec[vec[idx]] = val;
            }
        }
    }
    vector<vvi> validCols(n, vvi(n));
    for (int j = 1; j < m - 1; j++) {
        map<int, vi, greater<int>> values;
        loop(i, n) {
            values[a[i][j]].push_back(i);
        }
        
        map<int, int> rec;
        for (auto [val, vec] : values) {
            loop(idx, vec.size()) {
                if (rec.size() == 0) {
                    rec[vec[idx]] = val;
                    continue;
                }

                auto next = rec.upper_bound(vec[idx]);
                if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
                    validCols[vec[idx]][next->first].push_back(j);
                }

                if (next != rec.begin()) {
                    next--;
                    if (vec[idx] - next->first > 1) {
                        validCols[next->first][vec[idx]].push_back(j);
                    }
                }

                rec[vec[idx]] = val;
            }
        }
    }

    // vector<MSTree> trees(n);
    // loop(i, n) {
    //     trees[i] = MSTree(validCols[i], m);
    // }

    //bottleneck
    ll result = 0;
    for (int j1 = 0; j1 < m; j1++) {
        for (int j2 = j1 + 2; j2 < m; j2++) {
            const auto& valid = validRows[j1][j2];

            int furthest = -1;
            loop(idx, valid.size()) {
                //this happens O(n * m) times

                if (furthest < idx) {
                    furthest = idx;
                    while (furthest < valid.size() - 1 && valid[furthest + 1] == valid[furthest] + 1) {
                        furthest++;
                    }
                }

                // vi validIndices = trees[valid[idx] - 1].getRange(j1 + 1, j2 - 1 + 1);
                // auto upp = --upper_bound(all(validIndices), valid[furthest] + 1);
                // int extra = upp - validIndices.begin() + 1;

                // result += extra;

                for (int idx2 = idx; idx2 <= furthest; idx2++) {
                    const auto& relevantCols = validCols[valid[idx] - 1][valid[idx2] + 1];
                    auto start = lower_bound(all(relevantCols), j1 + 1);
                    auto end = lower_bound(all(relevantCols), j2 - 1);

                    if (start != relevantCols.end() && end != relevantCols.end() && *start == j1 + 1 && *end == j2 - 1 && (end - start) == j2 - j1 - 2) {   
                        result++;
                    }
                }
            }
        }
    }

    return result;
}

Compilation message

rect.cpp: In constructor 'MSTree::MSTree(const vvi&, int)':
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define loop(X, N) for(int X = 0; X < (N); X++)
      |                                     ^
rect.cpp:45:9: note: in expansion of macro 'loop'
   45 |         loop(i, values.size()) {
      |         ^~~~
rect.cpp: In member function 'vi MSTree::merge(const vi&, const vi&)':
rect.cpp:59:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         while (idx1 < a.size() && idx2 < b.size()) {
      |                ~~~~~^~~~~~~~~~
rect.cpp:59:40: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   59 |         while (idx1 < a.size() && idx2 < b.size()) {
      |                                   ~~~~~^~~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(vvi)':
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define loop(X, N) for(int X = 0; X < (N); X++)
      |                                     ^
rect.cpp:114:13: note: in expansion of macro 'loop'
  114 |             loop(idx, vec.size()) {
      |             ^~~~
rect.cpp:121:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |                 if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
      |                                           ~~~~^~~~~~~~~~~~~~~~~
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define loop(X, N) for(int X = 0; X < (N); X++)
      |                                     ^
rect.cpp:145:13: note: in expansion of macro 'loop'
  145 |             loop(idx, vec.size()) {
      |             ^~~~
rect.cpp:152:47: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  152 |                 if (next != rec.end() && (idx == vec.size() - 1 || vec[idx + 1] > next->first) && next->first - vec[idx] > 1) {
      |                                           ~~~~^~~~~~~~~~~~~~~~~
rect.cpp:20:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 | #define loop(X, N) for(int X = 0; X < (N); X++)
      |                                     ^
rect.cpp:180:13: note: in expansion of macro 'loop'
  180 |             loop(idx, valid.size()) {
      |             ^~~~
rect.cpp:185:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  185 |                     while (furthest < valid.size() - 1 && valid[furthest + 1] == valid[furthest] + 1) {
      |                            ~~~~~~~~~^~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 468 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 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 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 468 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 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 1 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 3 ms 856 KB Output is correct
23 Correct 3 ms 860 KB Output is correct
24 Correct 4 ms 700 KB Output is correct
25 Correct 2 ms 860 KB Output is correct
26 Correct 5 ms 708 KB Output is correct
27 Correct 5 ms 860 KB Output is correct
28 Correct 5 ms 860 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 468 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 3 ms 856 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 4 ms 700 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 5 ms 708 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 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 1 ms 348 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 30 ms 3164 KB Output is correct
31 Correct 30 ms 3100 KB Output is correct
32 Correct 31 ms 3164 KB Output is correct
33 Correct 13 ms 2908 KB Output is correct
34 Correct 27 ms 3236 KB Output is correct
35 Correct 30 ms 3420 KB Output is correct
36 Correct 32 ms 3412 KB Output is correct
37 Correct 32 ms 3412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 468 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 3 ms 856 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 4 ms 700 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 5 ms 708 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 30 ms 3164 KB Output is correct
26 Correct 30 ms 3100 KB Output is correct
27 Correct 31 ms 3164 KB Output is correct
28 Correct 13 ms 2908 KB Output is correct
29 Correct 27 ms 3236 KB Output is correct
30 Correct 30 ms 3420 KB Output is correct
31 Correct 32 ms 3412 KB Output is correct
32 Correct 32 ms 3412 KB Output is correct
33 Correct 1 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 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 247 ms 44628 KB Output is correct
39 Correct 223 ms 45984 KB Output is correct
40 Correct 234 ms 45904 KB Output is correct
41 Correct 223 ms 45904 KB Output is correct
42 Correct 886 ms 36336 KB Output is correct
43 Correct 887 ms 36688 KB Output is correct
44 Correct 888 ms 36692 KB Output is correct
45 Correct 784 ms 34716 KB Output is correct
46 Correct 174 ms 30036 KB Output is correct
47 Correct 183 ms 30804 KB Output is correct
48 Correct 390 ms 35364 KB Output is correct
49 Correct 458 ms 37632 KB Output is correct
50 Correct 202 ms 22100 KB Output is correct
51 Correct 214 ms 21840 KB Output is correct
52 Correct 466 ms 35444 KB Output is correct
53 Correct 473 ms 36472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 147796 KB Output is correct
2 Correct 52 ms 106580 KB Output is correct
3 Correct 84 ms 147280 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 78 ms 147796 KB Output is correct
6 Correct 75 ms 147800 KB Output is correct
7 Correct 74 ms 147792 KB Output is correct
8 Correct 74 ms 147572 KB Output is correct
9 Correct 80 ms 148048 KB Output is correct
10 Correct 73 ms 147280 KB Output is correct
11 Correct 74 ms 147280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 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 1110 ms 198676 KB Output is correct
8 Correct 2530 ms 375428 KB Output is correct
9 Correct 2526 ms 376704 KB Output is correct
10 Correct 2595 ms 377172 KB Output is correct
11 Correct 697 ms 213492 KB Output is correct
12 Correct 1223 ms 333964 KB Output is correct
13 Correct 1293 ms 355408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 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 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 468 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 0 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 3 ms 856 KB Output is correct
18 Correct 3 ms 860 KB Output is correct
19 Correct 4 ms 700 KB Output is correct
20 Correct 2 ms 860 KB Output is correct
21 Correct 5 ms 708 KB Output is correct
22 Correct 5 ms 860 KB Output is correct
23 Correct 5 ms 860 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 30 ms 3164 KB Output is correct
26 Correct 30 ms 3100 KB Output is correct
27 Correct 31 ms 3164 KB Output is correct
28 Correct 13 ms 2908 KB Output is correct
29 Correct 27 ms 3236 KB Output is correct
30 Correct 30 ms 3420 KB Output is correct
31 Correct 32 ms 3412 KB Output is correct
32 Correct 32 ms 3412 KB Output is correct
33 Correct 247 ms 44628 KB Output is correct
34 Correct 223 ms 45984 KB Output is correct
35 Correct 234 ms 45904 KB Output is correct
36 Correct 223 ms 45904 KB Output is correct
37 Correct 886 ms 36336 KB Output is correct
38 Correct 887 ms 36688 KB Output is correct
39 Correct 888 ms 36692 KB Output is correct
40 Correct 784 ms 34716 KB Output is correct
41 Correct 174 ms 30036 KB Output is correct
42 Correct 183 ms 30804 KB Output is correct
43 Correct 390 ms 35364 KB Output is correct
44 Correct 458 ms 37632 KB Output is correct
45 Correct 202 ms 22100 KB Output is correct
46 Correct 214 ms 21840 KB Output is correct
47 Correct 466 ms 35444 KB Output is correct
48 Correct 473 ms 36472 KB Output is correct
49 Correct 68 ms 147796 KB Output is correct
50 Correct 52 ms 106580 KB Output is correct
51 Correct 84 ms 147280 KB Output is correct
52 Correct 1 ms 344 KB Output is correct
53 Correct 78 ms 147796 KB Output is correct
54 Correct 75 ms 147800 KB Output is correct
55 Correct 74 ms 147792 KB Output is correct
56 Correct 74 ms 147572 KB Output is correct
57 Correct 80 ms 148048 KB Output is correct
58 Correct 73 ms 147280 KB Output is correct
59 Correct 74 ms 147280 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 1110 ms 198676 KB Output is correct
62 Correct 2530 ms 375428 KB Output is correct
63 Correct 2526 ms 376704 KB Output is correct
64 Correct 2595 ms 377172 KB Output is correct
65 Correct 697 ms 213492 KB Output is correct
66 Correct 1223 ms 333964 KB Output is correct
67 Correct 1293 ms 355408 KB Output is correct
68 Correct 1 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 1 ms 348 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 3585 ms 595584 KB Output is correct
74 Correct 3249 ms 595796 KB Output is correct
75 Correct 3492 ms 595628 KB Output is correct
76 Correct 3340 ms 595796 KB Output is correct
77 Execution timed out 5054 ms 467800 KB Time limit exceeded
78 Halted 0 ms 0 KB -