Submission #171197

# Submission time Handle Problem Language Result Execution time Memory
171197 2019-12-27T18:07:25 Z nobik Rectangles (IOI19_rect) C++14
72 / 100
5000 ms 453192 KB
#include "rect.h"
#include <bits/stdc++.h>

using namespace std;

vector<vector<pair<int, int>>> CalcSegments(const vector<int>& a) {
	int n = static_cast<int>(a.size());
	vector<vector<pair<int, int>>> segments(n);
	vector<int> s;
	for (int i = 0; i < n; ++i) {
		while (!s.empty()) {
			int segment = i - s.back() - 1;
			if (segment > 0) 
				segments[i].emplace_back(segment, 1);
			if (a[s.back()] >= a[i]) 
				break;
			s.pop_back();
		}

		if (!s.empty() && a[s.back()] == a[i])
			s.pop_back();

		s.push_back(i);
	}

	segments.shrink_to_fit();
	return segments;
}

vector<pair<int, int>> Merge(const vector<pair<int, int>>& a, const vector<pair<int, int>>& b) {
	vector<pair<int, int>> r;
	int a_size = static_cast<int>(a.size());
	int j = 0;
	for (const auto& e: b) {
		while (j < a_size && a[j].first < e.first) {
			++j;
		}

		int cnt = e.second;
		if (j < a_size && a[j].first == e.first) {
			cnt += a[j].second;
		}

		r.emplace_back(e.first, cnt);
	}

	return r;
}

long long count_rectangles(std::vector<std::vector<int> > a) {
	int n = static_cast<int>(a.size());
	int m = static_cast<int>(a.front().size());

	vector<vector<vector<pair<int, int>>>> horizontal_segments(n, vector<vector<pair<int, int>>>(m));
	vector<vector<vector<pair<int, int>>>> vertical_segments(n, vector<vector<pair<int, int>>>(m));

	for (int i = 0; i < n; ++i) {
		horizontal_segments[i] = CalcSegments(a[i]);
	}

	for (int j = 0; j < m; ++j) {
		vector<int> b;
		for (int i = 0; i < n; ++i) {
			b.push_back(a[i][j]);
		}

		auto segments = CalcSegments(b);
		for (int i = 0; i < n; ++i) {
			vertical_segments[i][j] = segments[i];
		}
	}

	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			if (i > 0) horizontal_segments[i][j] = Merge(horizontal_segments[i - 1][j], horizontal_segments[i][j]);
			if (j > 0) vertical_segments[i][j] = Merge(vertical_segments[i][j - 1], vertical_segments[i][j]);
		}
	}

	long long result = 0;
	for (int i = 1; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			for (auto& x: horizontal_segments[i - 1][j]) {
				for (auto& y: vertical_segments[i][j - 1]) {
					if (x.first <= y.second && y.first <= x.second)
						++result;
				}
			}
		}
	}

	/* for (int i = 1; i < n; ++i) {
		for (int j = 1; j < m; ++j) {
			cout << i << ' ' << j << ": " << endl;
			cout << "HORIZONTAL: "; for (auto& x: horizontal_segments[i - 1][j]) cout << x.first << ' '; cout << endl;
			cout << "VERTICAL: "; for (auto& x: vertical_segments[i][j - 1]) cout << x.first << ' '; cout << endl;
		}
	} */

	return result;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 356 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 1 ms 376 KB Output is correct
18 Correct 2 ms 256 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 356 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 5 ms 888 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 4 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 6 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 6 ms 888 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 1 ms 376 KB Output is correct
26 Correct 2 ms 256 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 2 ms 376 KB Output is correct
29 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 356 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 5 ms 888 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 4 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 6 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 6 ms 888 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 11 ms 3576 KB Output is correct
26 Correct 11 ms 3576 KB Output is correct
27 Correct 11 ms 3704 KB Output is correct
28 Correct 16 ms 3320 KB Output is correct
29 Correct 23 ms 4248 KB Output is correct
30 Correct 27 ms 4344 KB Output is correct
31 Correct 22 ms 4216 KB Output is correct
32 Correct 23 ms 4216 KB Output is correct
33 Correct 1 ms 376 KB Output is correct
34 Correct 2 ms 256 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 2 ms 376 KB Output is correct
37 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 356 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 5 ms 888 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 4 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 6 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 6 ms 888 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 11 ms 3576 KB Output is correct
26 Correct 11 ms 3576 KB Output is correct
27 Correct 11 ms 3704 KB Output is correct
28 Correct 16 ms 3320 KB Output is correct
29 Correct 23 ms 4248 KB Output is correct
30 Correct 27 ms 4344 KB Output is correct
31 Correct 22 ms 4216 KB Output is correct
32 Correct 23 ms 4216 KB Output is correct
33 Correct 193 ms 36188 KB Output is correct
34 Correct 114 ms 41428 KB Output is correct
35 Correct 161 ms 41000 KB Output is correct
36 Correct 178 ms 45816 KB Output is correct
37 Correct 113 ms 41752 KB Output is correct
38 Correct 101 ms 41720 KB Output is correct
39 Correct 114 ms 42012 KB Output is correct
40 Correct 112 ms 39928 KB Output is correct
41 Correct 147 ms 35932 KB Output is correct
42 Correct 197 ms 38596 KB Output is correct
43 Correct 297 ms 48004 KB Output is correct
44 Correct 295 ms 50168 KB Output is correct
45 Correct 145 ms 25208 KB Output is correct
46 Correct 143 ms 25080 KB Output is correct
47 Correct 272 ms 47556 KB Output is correct
48 Correct 276 ms 48660 KB Output is correct
49 Correct 1 ms 376 KB Output is correct
50 Correct 2 ms 256 KB Output is correct
51 Correct 3 ms 376 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1016 KB Output is correct
2 Correct 4 ms 888 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 5 ms 1144 KB Output is correct
6 Correct 5 ms 1016 KB Output is correct
7 Correct 5 ms 920 KB Output is correct
8 Correct 5 ms 1016 KB Output is correct
9 Correct 5 ms 1016 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 4 ms 760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 926 ms 207948 KB Output is correct
3 Correct 2079 ms 450944 KB Output is correct
4 Correct 2111 ms 453164 KB Output is correct
5 Correct 2225 ms 453192 KB Output is correct
6 Correct 428 ms 175636 KB Output is correct
7 Correct 840 ms 333560 KB Output is correct
8 Correct 913 ms 355576 KB Output is correct
9 Correct 1 ms 376 KB Output is correct
10 Correct 2 ms 256 KB Output is correct
11 Correct 3 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 256 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 6 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 356 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 5 ms 888 KB Output is correct
18 Correct 4 ms 888 KB Output is correct
19 Correct 4 ms 888 KB Output is correct
20 Correct 5 ms 888 KB Output is correct
21 Correct 6 ms 888 KB Output is correct
22 Correct 5 ms 888 KB Output is correct
23 Correct 6 ms 888 KB Output is correct
24 Correct 4 ms 632 KB Output is correct
25 Correct 11 ms 3576 KB Output is correct
26 Correct 11 ms 3576 KB Output is correct
27 Correct 11 ms 3704 KB Output is correct
28 Correct 16 ms 3320 KB Output is correct
29 Correct 23 ms 4248 KB Output is correct
30 Correct 27 ms 4344 KB Output is correct
31 Correct 22 ms 4216 KB Output is correct
32 Correct 23 ms 4216 KB Output is correct
33 Correct 193 ms 36188 KB Output is correct
34 Correct 114 ms 41428 KB Output is correct
35 Correct 161 ms 41000 KB Output is correct
36 Correct 178 ms 45816 KB Output is correct
37 Correct 113 ms 41752 KB Output is correct
38 Correct 101 ms 41720 KB Output is correct
39 Correct 114 ms 42012 KB Output is correct
40 Correct 112 ms 39928 KB Output is correct
41 Correct 147 ms 35932 KB Output is correct
42 Correct 197 ms 38596 KB Output is correct
43 Correct 297 ms 48004 KB Output is correct
44 Correct 295 ms 50168 KB Output is correct
45 Correct 145 ms 25208 KB Output is correct
46 Correct 143 ms 25080 KB Output is correct
47 Correct 272 ms 47556 KB Output is correct
48 Correct 276 ms 48660 KB Output is correct
49 Correct 4 ms 1016 KB Output is correct
50 Correct 4 ms 888 KB Output is correct
51 Correct 3 ms 760 KB Output is correct
52 Correct 2 ms 376 KB Output is correct
53 Correct 5 ms 1144 KB Output is correct
54 Correct 5 ms 1016 KB Output is correct
55 Correct 5 ms 920 KB Output is correct
56 Correct 5 ms 1016 KB Output is correct
57 Correct 5 ms 1016 KB Output is correct
58 Correct 3 ms 632 KB Output is correct
59 Correct 4 ms 760 KB Output is correct
60 Correct 2 ms 256 KB Output is correct
61 Correct 926 ms 207948 KB Output is correct
62 Correct 2079 ms 450944 KB Output is correct
63 Correct 2111 ms 453164 KB Output is correct
64 Correct 2225 ms 453192 KB Output is correct
65 Correct 428 ms 175636 KB Output is correct
66 Correct 840 ms 333560 KB Output is correct
67 Correct 913 ms 355576 KB Output is correct
68 Execution timed out 5115 ms 432864 KB Time limit exceeded
69 Halted 0 ms 0 KB -