Submission #1017342

# Submission time Handle Problem Language Result Execution time Memory
1017342 2024-07-09T07:28:58 Z BestCrazyNoob Rectangles (IOI19_rect) C++17
100 / 100
3348 ms 419152 KB
#include "rect.h"
#include <utility>
#include <tuple>
#include <vector>
#include <functional>
#include <algorithm>

using namespace std;
using pii = pair<int, int>;
constexpr int MAXN = 2'500;

int N, M;

struct SegTreeMax {
	int sz;
	vector<pii> tree; // min, index (min(a, b) works)
	int ql, qr;
	void merge(int i) {
		tree[i] = max(tree[2*i], tree[2*i+1]);
	}
	SegTreeMax(vector<int> V) {
		sz = 1;
		while (sz < V.size()) sz *= 2;
		tree.resize(2*sz);
		for (int i = 0; i < V.size(); i++) tree[sz+i] = {V[i], i};
		for (int i = V.size(); i < sz; i++) tree[sz+i] = {-1, -1};
		for (int i = sz-1; i >= 1; i--) merge(i);
	}
	pii __query(int ni, int nl, int nr) {
		if (qr <= nl || nr <= ql) return {-1, -1};
		if (ql <= nl && nr <= qr) return tree[ni];
		int nm = (nl+nr)/2;
		return max(__query(2*ni, nl, nm), __query(2*ni+1, nm, nr));
	}
	pii query(int l, int r) {
		ql = l;
		qr = r;
		return __query(1, 0, sz);
	}
};

// excluded - excluded, because the last element on both sides is the border
vector<pii> splits(vector<int> V) {
	// TODO: pay attention to equal elements. If there are multiples, return the rightmost.
	SegTreeMax seg(V);
	vector<pii> res;
	function<void(int, int, bool, bool)> rec = [&](int l, int r, bool lGood, bool rGood) {
		// Exit condition
		if (l == r) return;
		// Check that the extremes are actual elements
		if (l != 0 && r != V.size()) res.push_back({l-1, r});
		if (l+1 == r) return;
		auto [mx, mxi] = seg.query(l, r); // rightmost max
		rec(mxi+1, r, true, rGood);
		int lastMxi = mxi;
		// iterate from right to left
		while (true) {
			auto [nxtMx, nxtMxi] = seg.query(l, lastMxi);
			if (nxtMx != mx) break;
			rec(nxtMxi+1, lastMxi, true, true);
			lastMxi = nxtMxi;
		}
		rec(l, lastMxi, lGood, true);
	};
	rec(0, V.size(), false, false);
	return res;
}

long long count_rectangles(vector<vector<int>> a) {
	N = a.size();
	M = a[0].size();
	vector<vector<int>> a2(M, vector<int>(N));
	vector<vector<vector<int>>> grid(N, vector<vector<int>>(M));
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < M; j++) {
			a2[j][i] = a[i][j];
		}
	}
	for (int j = 0; j < M; j++) {
		for (auto [s, e]: splits(a2[j])) {
			grid[e][j].push_back(s);
		}
	}
	vector<vector<pii>> active(M, vector<pii>(M, {-2, -2})); // (incl, incl)
	long long ans = 0;
	for (int i = 1; i < N-1; i++) {
		vector<pii> x = splits(a[i]);
		vector<vector<int>> byEnd(M);
		vector<pii> cleanup;
		vector<pii> active2(N, {-2, -2});
		for (auto[s, e]: x) {
			if (active[s][e].second == i-1) {
				// extend
				active[s][e].second = i;
			} else {
				// start again
				active[s][e] = {i, i};
			}
			byEnd[e].push_back(s);
		}
		for (int j = 1; j < M; j++) {
			sort(grid[i+1][j-1].rbegin(), grid[i+1][j-1].rend());
			sort(byEnd[j].begin(), byEnd[j].end(), [&](int a, int b) {return active[a][j].first > active[b][j].first;});
			for (auto e: grid[i+1][j-1]) {
				// same idea as above
				if (active2[e].second == j-2) active2[e].second = j-1;
				else active2[e] = {j-1, j-1};
			}
			for (int a = 0, b = 0; b < byEnd[j].size(); b++) {
				// TODO: might have to use a treap / sparse tree
				while (a < grid[i+1][j-1].size() && grid[i+1][j-1][a] >= active[byEnd[j][b]][j].first - 1) {
					a++; // ... Insert into treap
				}
				for (int idx = 0; idx < a; idx++) {
					if (active2[grid[i+1][j-1][idx]].first <= byEnd[j][b]+1) {
						ans++;
					}
				}
			}
		}
	}
	return ans;
}

Compilation message

rect.cpp: In constructor 'SegTreeMax::SegTreeMax(std::vector<int>)':
rect.cpp:23:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   23 |   while (sz < V.size()) sz *= 2;
      |          ~~~^~~~~~~~~~
rect.cpp:25:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |   for (int i = 0; i < V.size(); i++) tree[sz+i] = {V[i], i};
      |                   ~~^~~~~~~~~~
rect.cpp: In lambda function:
rect.cpp:51:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   51 |   if (l != 0 && r != V.size()) res.push_back({l-1, r});
      |                 ~~^~~~~~~~~~~
rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:109:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  109 |    for (int a = 0, b = 0; b < byEnd[j].size(); b++) {
      |                           ~~^~~~~~~~~~~~~~~~~
rect.cpp:111:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |     while (a < grid[i+1][j-1].size() && grid[i+1][j-1][a] >= active[byEnd[j][b]][j].first - 1) {
      |            ~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 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 0 ms 344 KB Output is correct
18 Correct 0 ms 420 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 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 0 ms 344 KB Output is correct
18 Correct 0 ms 420 KB Output is correct
19 Correct 0 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 3 ms 604 KB Output is correct
23 Correct 2 ms 600 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 2 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 600 KB Output is correct
28 Correct 2 ms 604 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 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 604 KB Output is correct
18 Correct 2 ms 600 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 0 ms 344 KB Output is correct
26 Correct 0 ms 420 KB Output is correct
27 Correct 0 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 11 ms 2652 KB Output is correct
31 Correct 12 ms 2396 KB Output is correct
32 Correct 12 ms 2636 KB Output is correct
33 Correct 12 ms 2652 KB Output is correct
34 Correct 16 ms 2908 KB Output is correct
35 Correct 17 ms 3136 KB Output is correct
36 Correct 15 ms 2908 KB Output is correct
37 Correct 15 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 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 604 KB Output is correct
18 Correct 2 ms 600 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 11 ms 2652 KB Output is correct
26 Correct 12 ms 2396 KB Output is correct
27 Correct 12 ms 2636 KB Output is correct
28 Correct 12 ms 2652 KB Output is correct
29 Correct 16 ms 2908 KB Output is correct
30 Correct 17 ms 3136 KB Output is correct
31 Correct 15 ms 2908 KB Output is correct
32 Correct 15 ms 2908 KB Output is correct
33 Correct 0 ms 344 KB Output is correct
34 Correct 0 ms 420 KB Output is correct
35 Correct 0 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 148 ms 26276 KB Output is correct
39 Correct 185 ms 26192 KB Output is correct
40 Correct 190 ms 32564 KB Output is correct
41 Correct 212 ms 32552 KB Output is correct
42 Correct 182 ms 27860 KB Output is correct
43 Correct 182 ms 27728 KB Output is correct
44 Correct 178 ms 27988 KB Output is correct
45 Correct 167 ms 26704 KB Output is correct
46 Correct 128 ms 26352 KB Output is correct
47 Correct 168 ms 27484 KB Output is correct
48 Correct 224 ms 31140 KB Output is correct
49 Correct 231 ms 33456 KB Output is correct
50 Correct 111 ms 15700 KB Output is correct
51 Correct 106 ms 18768 KB Output is correct
52 Correct 215 ms 31560 KB Output is correct
53 Correct 201 ms 32596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 50260 KB Output is correct
2 Correct 16 ms 36444 KB Output is correct
3 Correct 19 ms 49760 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 24 ms 49892 KB Output is correct
6 Correct 20 ms 50012 KB Output is correct
7 Correct 19 ms 49904 KB Output is correct
8 Correct 19 ms 49900 KB Output is correct
9 Correct 20 ms 49936 KB Output is correct
10 Correct 17 ms 49500 KB Output is correct
11 Correct 18 ms 49572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 420 KB Output is correct
3 Correct 0 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 0 ms 344 KB Output is correct
7 Correct 851 ms 169148 KB Output is correct
8 Correct 1896 ms 330088 KB Output is correct
9 Correct 1850 ms 331344 KB Output is correct
10 Correct 1915 ms 331248 KB Output is correct
11 Correct 516 ms 164456 KB Output is correct
12 Correct 1166 ms 261892 KB Output is correct
13 Correct 1096 ms 282308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 392 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 420 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 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 604 KB Output is correct
18 Correct 2 ms 600 KB Output is correct
19 Correct 2 ms 604 KB Output is correct
20 Correct 2 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 600 KB Output is correct
23 Correct 2 ms 604 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 11 ms 2652 KB Output is correct
26 Correct 12 ms 2396 KB Output is correct
27 Correct 12 ms 2636 KB Output is correct
28 Correct 12 ms 2652 KB Output is correct
29 Correct 16 ms 2908 KB Output is correct
30 Correct 17 ms 3136 KB Output is correct
31 Correct 15 ms 2908 KB Output is correct
32 Correct 15 ms 2908 KB Output is correct
33 Correct 148 ms 26276 KB Output is correct
34 Correct 185 ms 26192 KB Output is correct
35 Correct 190 ms 32564 KB Output is correct
36 Correct 212 ms 32552 KB Output is correct
37 Correct 182 ms 27860 KB Output is correct
38 Correct 182 ms 27728 KB Output is correct
39 Correct 178 ms 27988 KB Output is correct
40 Correct 167 ms 26704 KB Output is correct
41 Correct 128 ms 26352 KB Output is correct
42 Correct 168 ms 27484 KB Output is correct
43 Correct 224 ms 31140 KB Output is correct
44 Correct 231 ms 33456 KB Output is correct
45 Correct 111 ms 15700 KB Output is correct
46 Correct 106 ms 18768 KB Output is correct
47 Correct 215 ms 31560 KB Output is correct
48 Correct 201 ms 32596 KB Output is correct
49 Correct 27 ms 50260 KB Output is correct
50 Correct 16 ms 36444 KB Output is correct
51 Correct 19 ms 49760 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 24 ms 49892 KB Output is correct
54 Correct 20 ms 50012 KB Output is correct
55 Correct 19 ms 49904 KB Output is correct
56 Correct 19 ms 49900 KB Output is correct
57 Correct 20 ms 49936 KB Output is correct
58 Correct 17 ms 49500 KB Output is correct
59 Correct 18 ms 49572 KB Output is correct
60 Correct 0 ms 344 KB Output is correct
61 Correct 851 ms 169148 KB Output is correct
62 Correct 1896 ms 330088 KB Output is correct
63 Correct 1850 ms 331344 KB Output is correct
64 Correct 1915 ms 331248 KB Output is correct
65 Correct 516 ms 164456 KB Output is correct
66 Correct 1166 ms 261892 KB Output is correct
67 Correct 1096 ms 282308 KB Output is correct
68 Correct 0 ms 344 KB Output is correct
69 Correct 0 ms 420 KB Output is correct
70 Correct 0 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 2477 ms 335472 KB Output is correct
74 Correct 2705 ms 335488 KB Output is correct
75 Correct 3058 ms 415032 KB Output is correct
76 Correct 3348 ms 415136 KB Output is correct
77 Correct 2764 ms 358568 KB Output is correct
78 Correct 1804 ms 255868 KB Output is correct
79 Correct 2013 ms 245100 KB Output is correct
80 Correct 3126 ms 393444 KB Output is correct
81 Correct 1940 ms 271464 KB Output is correct
82 Correct 2532 ms 327636 KB Output is correct
83 Correct 3220 ms 419152 KB Output is correct
84 Correct 1763 ms 259372 KB Output is correct
85 Correct 3003 ms 412280 KB Output is correct
86 Correct 3073 ms 401892 KB Output is correct
87 Correct 1449 ms 203860 KB Output is correct
88 Correct 2611 ms 358188 KB Output is correct
89 Correct 2524 ms 358436 KB Output is correct
90 Correct 2639 ms 358624 KB Output is correct