Submission #1066194

# Submission time Handle Problem Language Result Execution time Memory
1066194 2024-08-19T16:13:09 Z Tsovak Rectangles (IOI19_rect) C++17
100 / 100
1985 ms 906116 KB
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#include "rect.h"
using namespace std;

// -------------------- Typedefs -------------------- //

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;

// -------------------- Defines -------------------- //

#define pr pair
#define mpr make_pair
#define ff first
#define ss second

#define mset multiset
#define mmap multimap
#define uset unordered_set
#define umap unordered_map
#define umset unordered_multiset
#define ummap unordered_multimap
#define pqueue priority_queue

#define sz(x) (int((x).size()))
#define len(x) (int((x).length()))
#define all(x) (x).begin(), (x).end()
#define clr(x) (x).clear()

#define ft front
#define bk back
#define pf push_front
#define pb push_back
#define popf pop_front
#define popb pop_back

#define lb lower_bound
#define ub upper_bound
#define bs binary_search

// -------------------- Constants -------------------- //

const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);

// -------------------- Solution -------------------- //

const int N = 2505, M = 2505, LG = 13;
int a[N][M];
short ul[N][M], ur[N][M], uu[N][M], ud[N][M];
short hl[N][M], hr[N][M], hu[N][M], hd[N][M];
int n, m, lgn, lgm;

pr<short, short> h[N];
pr<short, short> th[N][M][LG], tv[M][N][LG];

ll e[N * M];
int ind;

void bld()
{
	int i, j, k;
	int g;

	h[1] = mpr(0, 1);
	for (i = 2; i <= 2503; i++) {
		h[i] = h[i - 1];
		if (h[i].ss * 2 <= i) {
			h[i].ff++;
			h[i].ss *= 2;
		}
	}

	while ((1 << lgn) <= n) lgn++;
	lgn--;
	while ((1 << lgm) <= m) lgm++;
	lgm--;


	for (i = 1; i <= n; i++) {
		for (j = 1; j <= m; j++) th[i][j][0] = mpr(hu[i][j], hd[i][j]);

		g = 1;
		for (k = 1; k <= lgm; k++) {
			for (j = 1; j <= m - 2 * g + 1; j++) {
				th[i][j][k].ff = max(th[i][j][k - 1].ff, th[i][j + g][k - 1].ff);
				th[i][j][k].ss = min(th[i][j][k - 1].ss, th[i][j + g][k - 1].ss);
			}
			g *= 2;
		}
	}

	for (j = 1; j <= m; j++) {
		for (i = 1; i <= n; i++) tv[j][i][0] = mpr(hl[i][j], hr[i][j]);

		g = 1;
		for (k = 1; k <= lgn; k++) {
			for (i = 1; i <= n - 2 * g + 1; i++) {
				tv[j][i][k].ff = max(tv[j][i][k - 1].ff, tv[j][i + g][k - 1].ff);
				tv[j][i][k].ss = min(tv[j][i][k - 1].ss, tv[j][i + g][k - 1].ss);
			}
			g *= 2;
		}
	}

	return;
}

pr<int, int> qryh(int i, int l, int r)
{
	int len = r - l + 1;

	pr<int, int> res;

	res.ff = max(th[i][l][h[len].ff].ff, th[i][r - h[len].ss + 1][h[len].ff].ff);
	res.ss = min(th[i][l][h[len].ff].ss, th[i][r - h[len].ss + 1][h[len].ff].ss);

	return res;
}

pr<int, int> qryv(int j, int l, int r)
{
	int len = r - l + 1;

	pr<int, int> res;

	res.ff = max(tv[j][l][h[len].ff].ff, tv[j][r - h[len].ss + 1][h[len].ff].ff);
	res.ss = min(tv[j][l][h[len].ff].ss, tv[j][r - h[len].ss + 1][h[len].ff].ss);

	return res;
}

ll count_rectangles(vector<vector<int>> a0)
{
	int i, j;

	n = sz(a0); m = sz(a0[0]);

	for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) a[i][j] = ++a0[i - 1][j - 1];

	if (n < 3 || m < 3) return 0;

	// closest biggers

	stack<pr<int, int>> st;

	for (i = 1; i <= n; i++) {
		while (!st.empty()) st.pop();

		for (j = 1; j <= m; j++) {
			while (!st.empty() && st.top().ff <= a[i][j]) st.pop();

			if (!st.empty()) ul[i][j] = st.top().ss;

			st.push(mpr(a[i][j], j));
		}

		while (!st.empty()) st.pop();

		for (j = m; j >= 1; j--) {
			while (!st.empty() && st.top().ff <= a[i][j]) st.pop();

			if (!st.empty()) ur[i][j] = st.top().ss;
			else ur[i][j] = m + 1;

			st.push(mpr(a[i][j], j));
		}
	}

	for (j = 1; j <= m; j++) {
		while (!st.empty()) st.pop();

		for (i = 1; i <= n; i++) {
			while (!st.empty() && st.top().ff <= a[i][j]) st.pop();

			if (!st.empty()) uu[i][j] = st.top().ss;

			st.push(mpr(a[i][j], i));
		}

		while (!st.empty()) st.pop();

		for (i = n; i >= 1; i--) {
			while (!st.empty() && st.top().ff <= a[i][j]) st.pop();

			if (!st.empty()) ud[i][j] = st.top().ss;
			else ud[i][j] = n + 1;

			st.push(mpr(a[i][j], i));
		}
	}

	// closest overlappings

	for (i = 1; i <= n; i++) {
		while (!st.empty()) st.pop();

		for (j = 3; j <= m; j++) {
			st.push(mpr(j - 1, ur[i][j - 1]));

			while (!st.empty() && st.top().ss <= j) st.pop();

			if (st.empty()) hl[i][j] = 0;
			else hl[i][j] = st.top().ff;
		}

		while (!st.empty()) st.pop();

		for (j = m - 2; j >= 1; j--) {
			st.push(mpr(ul[i][j + 1], j + 1));

			while (!st.empty() && st.top().ff >= j) st.pop();

			if (st.empty()) hr[i][j] = m + 1;
			else hr[i][j] = st.top().ss;
		}
	}

	for (j = 1; j <= m; j++) {
		while (!st.empty()) st.pop();

		for (i = 3; i <= n; i++) {
			st.push(mpr(i - 1, ud[i - 1][j]));

			while (!st.empty() && st.top().ss <= i) st.pop();

			if (st.empty()) hu[i][j] = 0;
			else hu[i][j] = st.top().ff;
		}

		while (!st.empty()) st.pop();

		for (i = n - 2; i >= 1; i--) {
			st.push(mpr(uu[i + 1][j], i + 1));

			while (!st.empty() && st.top().ff >= i) st.pop();

			if (st.empty()) hd[i][j] = n + 1;
			else hd[i][j] = st.top().ss;
		}
	}

	bld();

	int li, ri, lj, rj;

	for (i = 2; i < n; i++) {
		for (j = 2; j < m; j++) {
			li = uu[i][j]; ri = ud[i][j];
			lj = ul[i][j]; rj = ur[i][j];

			if (li <= 0 || ri >= n + 1 || lj <= 0 || rj >= m + 1) continue;

			if (qryh(li, lj + 1, rj - 1).ss < ri) continue;
			if (qryh(ri, lj + 1, rj - 1).ff > li) continue;

			if (qryv(lj, li + 1, ri - 1).ss < rj) continue;
			if (qryv(rj, li + 1, ri - 1).ff > lj) continue;

			e[++ind] = 1LL + (1LL * li) + (2500LL * ri) + (2500LL * 2500LL * lj) + (2500LL * 2500LL * 2500LL * rj);
		}
	}

	int ans = 0;

	sort(e + 1, e + ind + 1);
	for (i = 1; i <= ind; i++) ans += (e[i - 1] != e[i]);

	return ans;
}

/*
	# # # #   # # # #   # # # #   #       #    #       #     #
	   #      #         #     #    #     #    # #      #   #
	   #      # # # #   #     #     #   #    #   #     # #
	   #            #   #     #      # #    # # # #    #   #
	   #      # # # #   # # # #       #    #       #   #     #
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1720 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1880 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 860 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 1628 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1720 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1880 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 860 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 1884 KB Output is correct
20 Correct 1 ms 1628 KB Output is correct
21 Correct 0 ms 348 KB Output is correct
22 Correct 3 ms 4700 KB Output is correct
23 Correct 3 ms 4696 KB Output is correct
24 Correct 3 ms 4700 KB Output is correct
25 Correct 3 ms 4700 KB Output is correct
26 Correct 3 ms 4700 KB Output is correct
27 Correct 5 ms 4696 KB Output is correct
28 Correct 3 ms 4700 KB Output is correct
29 Correct 2 ms 4188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1720 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1880 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 860 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 3 ms 4700 KB Output is correct
18 Correct 3 ms 4696 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 3 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4696 KB Output is correct
23 Correct 3 ms 4700 KB Output is correct
24 Correct 2 ms 4188 KB Output is correct
25 Correct 0 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 1 ms 1884 KB Output is correct
28 Correct 1 ms 1628 KB Output is correct
29 Correct 0 ms 348 KB Output is correct
30 Correct 13 ms 14940 KB Output is correct
31 Correct 12 ms 14940 KB Output is correct
32 Correct 13 ms 14940 KB Output is correct
33 Correct 12 ms 14428 KB Output is correct
34 Correct 12 ms 14580 KB Output is correct
35 Correct 12 ms 14936 KB Output is correct
36 Correct 13 ms 14844 KB Output is correct
37 Correct 14 ms 14792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1720 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1880 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 860 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 3 ms 4700 KB Output is correct
18 Correct 3 ms 4696 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 3 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4696 KB Output is correct
23 Correct 3 ms 4700 KB Output is correct
24 Correct 2 ms 4188 KB Output is correct
25 Correct 13 ms 14940 KB Output is correct
26 Correct 12 ms 14940 KB Output is correct
27 Correct 13 ms 14940 KB Output is correct
28 Correct 12 ms 14428 KB Output is correct
29 Correct 12 ms 14580 KB Output is correct
30 Correct 12 ms 14936 KB Output is correct
31 Correct 13 ms 14844 KB Output is correct
32 Correct 14 ms 14792 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 1 ms 1884 KB Output is correct
36 Correct 1 ms 1628 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 92 ms 94720 KB Output is correct
39 Correct 93 ms 94884 KB Output is correct
40 Correct 90 ms 95060 KB Output is correct
41 Correct 95 ms 95268 KB Output is correct
42 Correct 148 ms 99152 KB Output is correct
43 Correct 158 ms 99176 KB Output is correct
44 Correct 146 ms 99304 KB Output is correct
45 Correct 141 ms 93488 KB Output is correct
46 Correct 108 ms 93268 KB Output is correct
47 Correct 117 ms 93520 KB Output is correct
48 Correct 137 ms 95148 KB Output is correct
49 Correct 143 ms 97104 KB Output is correct
50 Correct 77 ms 64228 KB Output is correct
51 Correct 70 ms 50180 KB Output is correct
52 Correct 148 ms 96632 KB Output is correct
53 Correct 158 ms 97712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 11864 KB Output is correct
2 Correct 4 ms 10332 KB Output is correct
3 Correct 4 ms 11868 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 5 ms 11868 KB Output is correct
6 Correct 5 ms 11868 KB Output is correct
7 Correct 5 ms 12032 KB Output is correct
8 Correct 5 ms 11868 KB Output is correct
9 Correct 5 ms 11868 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 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 1 ms 1884 KB Output is correct
4 Correct 1 ms 1628 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 856 KB Output is correct
7 Correct 624 ms 395856 KB Output is correct
8 Correct 1375 ms 821492 KB Output is correct
9 Correct 1359 ms 823816 KB Output is correct
10 Correct 1387 ms 824064 KB Output is correct
11 Correct 469 ms 407072 KB Output is correct
12 Correct 917 ms 772692 KB Output is correct
13 Correct 977 ms 808020 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 1 ms 1884 KB Output is correct
3 Correct 1 ms 1884 KB Output is correct
4 Correct 1 ms 1884 KB Output is correct
5 Correct 1 ms 1720 KB Output is correct
6 Correct 1 ms 1884 KB Output is correct
7 Correct 1 ms 1628 KB Output is correct
8 Correct 1 ms 1116 KB Output is correct
9 Correct 1 ms 1884 KB Output is correct
10 Correct 1 ms 1884 KB Output is correct
11 Correct 1 ms 1884 KB Output is correct
12 Correct 1 ms 1880 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 860 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 3 ms 4700 KB Output is correct
18 Correct 3 ms 4696 KB Output is correct
19 Correct 3 ms 4700 KB Output is correct
20 Correct 3 ms 4700 KB Output is correct
21 Correct 3 ms 4700 KB Output is correct
22 Correct 5 ms 4696 KB Output is correct
23 Correct 3 ms 4700 KB Output is correct
24 Correct 2 ms 4188 KB Output is correct
25 Correct 13 ms 14940 KB Output is correct
26 Correct 12 ms 14940 KB Output is correct
27 Correct 13 ms 14940 KB Output is correct
28 Correct 12 ms 14428 KB Output is correct
29 Correct 12 ms 14580 KB Output is correct
30 Correct 12 ms 14936 KB Output is correct
31 Correct 13 ms 14844 KB Output is correct
32 Correct 14 ms 14792 KB Output is correct
33 Correct 92 ms 94720 KB Output is correct
34 Correct 93 ms 94884 KB Output is correct
35 Correct 90 ms 95060 KB Output is correct
36 Correct 95 ms 95268 KB Output is correct
37 Correct 148 ms 99152 KB Output is correct
38 Correct 158 ms 99176 KB Output is correct
39 Correct 146 ms 99304 KB Output is correct
40 Correct 141 ms 93488 KB Output is correct
41 Correct 108 ms 93268 KB Output is correct
42 Correct 117 ms 93520 KB Output is correct
43 Correct 137 ms 95148 KB Output is correct
44 Correct 143 ms 97104 KB Output is correct
45 Correct 77 ms 64228 KB Output is correct
46 Correct 70 ms 50180 KB Output is correct
47 Correct 148 ms 96632 KB Output is correct
48 Correct 158 ms 97712 KB Output is correct
49 Correct 6 ms 11864 KB Output is correct
50 Correct 4 ms 10332 KB Output is correct
51 Correct 4 ms 11868 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 5 ms 11868 KB Output is correct
54 Correct 5 ms 11868 KB Output is correct
55 Correct 5 ms 12032 KB Output is correct
56 Correct 5 ms 11868 KB Output is correct
57 Correct 5 ms 11868 KB Output is correct
58 Correct 1 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 856 KB Output is correct
61 Correct 624 ms 395856 KB Output is correct
62 Correct 1375 ms 821492 KB Output is correct
63 Correct 1359 ms 823816 KB Output is correct
64 Correct 1387 ms 824064 KB Output is correct
65 Correct 469 ms 407072 KB Output is correct
66 Correct 917 ms 772692 KB Output is correct
67 Correct 977 ms 808020 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 1 ms 1884 KB Output is correct
71 Correct 1 ms 1628 KB Output is correct
72 Correct 0 ms 348 KB Output is correct
73 Correct 1161 ms 855552 KB Output is correct
74 Correct 1100 ms 855752 KB Output is correct
75 Correct 1090 ms 855680 KB Output is correct
76 Correct 1083 ms 856028 KB Output is correct
77 Correct 1883 ms 906116 KB Output is correct
78 Correct 1068 ms 519248 KB Output is correct
79 Correct 1126 ms 598580 KB Output is correct
80 Correct 1759 ms 848692 KB Output is correct
81 Correct 1081 ms 534312 KB Output is correct
82 Correct 1515 ms 733236 KB Output is correct
83 Correct 1835 ms 873984 KB Output is correct
84 Correct 1082 ms 531492 KB Output is correct
85 Correct 1888 ms 880852 KB Output is correct
86 Correct 1878 ms 866108 KB Output is correct
87 Correct 1225 ms 602572 KB Output is correct
88 Correct 1985 ms 905548 KB Output is correct
89 Correct 1918 ms 905816 KB Output is correct
90 Correct 1859 ms 905992 KB Output is correct