Submission #956974

# Submission time Handle Problem Language Result Execution time Memory
956974 2024-04-02T18:17:16 Z Macker Rectangles (IOI19_rect) C++17
72 / 100
2318 ms 1048576 KB
#include "rect.h"
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define FOR(i, n) for (int i = 0; i < n; i++)

struct node{
	int on = 0, mxx, mnx, mxy, mny;
};
vector<vector<node>> st;
int lenx = 1, leny = 1;
node zero({1, 0, INT_MAX/2, 0, INT_MAX/2});

vector<int> get_last_big(vector<int> v){
	int n = v.size();
	vector<int> res(n, -1);
	stack<pii> s;
	s.push({INT_MAX/2, -1});
	FOR(i, n){
		int a = v[i];
		while(a >= s.top().ff) s.pop();
		res[i] = s.top().ss;
		s.push({a, i});
	}
	return res;
}

node comb(node a, node b){
	node res;
	res.on = min(a.on, b.on);
	res.mnx = min(a.mnx, b.mnx);
	res.mny = min(a.mny, b.mny);
	res.mxx = max(a.mxx, b.mxx);
	res.mxy = max(a.mxy, b.mxy);
	return res;
}

void updy(int y, node val, int i, int j = 1, int s = 0, int e = leny){
	if(y < s || y >= e) return;
	if(y == s && s + 1 == e) {
		if(i >= lenx) st[i][j] = val;
		else st[i][j] = comb(st[i * 2][j], st[i * 2 + 1][j]);
		return;
	}

	updy(y, val, i, j * 2, s, (s + e) / 2);
	updy(y, val, i, j * 2 + 1, (s + e) / 2, e);
	st[i][j] = comb(st[i][j * 2], st[i][j * 2 + 1]);
}

void updx(int x, int y, node val, int i = 1, int s = 0, int e = lenx){
	if(x < s || x >= e) return;
	if(x == s && s + 1 == e) {updy(y, val, i); return;}

	updx(x, y, val, i * 2, s, (s + e) / 2);
	updx(x, y, val, i * 2 + 1, (s + e) / 2, e);
	
	updy(y, val, i);
}

node qryy(int l, int r, int i, int j = 1, int s = 0, int e = leny){
	if(l >= e || r <= s) return zero;
	if(l <= s && e <= r) return st[i][j];

	return comb(qryy(l, r, i, j * 2, s, (s + e) / 2),
				qryy(l, r, i, j * 2 + 1, (s + e) / 2, e));
}

node qryx(int xl, int xr, int yl, int yr, int i = 1, int s = 0, int e = lenx){
	if(xl >= e || xr <= s) return zero;
	if(xl <= s && e <= xr) return qryy(yl, yr, i);

	return comb(qryx(xl, xr, yl, yr, i * 2, s, (s + e) / 2),
				qryx(xl, xr, yl, yr, i * 2 + 1, (s + e) / 2, e));
}

long long count_rectangles_2(vector<vector<signed>> v) {
	int n = v.size();
	int m = v[0].size();
	st.assign(n, vector<node>(m));
 
	FOR(i, n){
		vector<int> cur;
		vector<int> res;
		FOR(j, m){
			cur.push_back(v[i][j]);
		}
		res = get_last_big(cur);
		FOR(j, m){
			st[i][j].mnx = res[j];
		}
		reverse(all(cur));
		res = get_last_big(cur);
		reverse(all(res));
		FOR(j, m){
			st[i][j].mxx = m - res[j] - 1;
		}
	}
	FOR(j, m){
		vector<int> cur;
		vector<int> res;
		FOR(i, n){
			cur.push_back(v[i][j]);
		}
		res = get_last_big(cur);
		FOR(i, n){
			st[i][j].mny = res[i];
		}
		reverse(all(cur));
		res = get_last_big(cur);
		reverse(all(res));
		FOR(i, n){
			st[i][j].mxy = n - res[i] - 1;
		}
	}
	
	vector<vector<pii>> ord(7000005);
	FOR(i, n) FOR(j, m){
		ord[v[i][j]].push_back({i, j});
	}
	ll res = 0;
	for (auto o : ord) {
		for (auto [x, y] : o) {
			st[x][y].on = true;
			int mxx, mnx, mxy, mny;
			mxx = st[x][y].mxx;
			mnx = st[x][y].mnx;
			mxy = st[x][y].mxy;
			mny = st[x][y].mny;
			if(mxx >= m || mxy >= n || mnx < 0 || mny < 0) continue;
 
			bool f = 0;
			for (int i = mny + 1; i < mxy; i++){
				for (int j = mnx + 1; j < mxx; j++) {
					if(!st[i][j].on) {f = 1; break;}
					if(st[i][j].mnx < mnx) {f = 1; break;}
					if(st[i][j].mny < mny) {f = 1; break;}
					if(st[i][j].mxx > mxx) {f = 1; break;}
					if(st[i][j].mxy > mxy) {f = 1; break;}
				}
				if(f) break;
			}
			if(!f) res++;
		}
	}
	return res;
}

long long count_rectangles(vector<vector<signed>> v) {
	int n = v.size();
	while(lenx < n) lenx *= 2;
	int m = v[0].size();
	while(leny < m) leny *= 2;

	int mxval = 0;
	FOR(i, n) FOR(j, m) mxval = max(mxval, v[i][j]);
	if(mxval < 2){
		return count_rectangles_2(v);
	}

	st.assign(2 * lenx, vector<node>(2 * leny));
	vector<vector<node>> passive(n, vector<node>(m));

	FOR(i, n){
		vector<int> cur;
		vector<int> res;
		FOR(j, m){
			cur.push_back(v[i][j]);
		}
		res = get_last_big(cur);
		FOR(j, m){
			passive[i][j].mnx = res[j];
		}
		reverse(all(cur));
		res = get_last_big(cur);
		reverse(all(res));
		FOR(j, m){
			passive[i][j].mxx = m - res[j] - 1;
		}
	}
	FOR(j, m){
		vector<int> cur;
		vector<int> res;
		FOR(i, n){
			cur.push_back(v[i][j]);
		}
		res = get_last_big(cur);
		FOR(i, n){
			passive[i][j].mny = res[i];
		}
		reverse(all(cur));
		res = get_last_big(cur);
		reverse(all(res));
		FOR(i, n){
			passive[i][j].mxy = n - res[i] - 1;
		}
	}
	
	vector<vector<pii>> ord(mxval + 1);
	FOR(i, n) FOR(j, m){
		ord[v[i][j]].push_back({i, j});
	}
	ll res = 0;
	for (auto o : ord) {
		for (auto [x, y] : o) {
			passive[x][y].on = true;
			updx(x, y, passive[x][y]);

			int mnx = passive[x][y].mnx;
			int mny = passive[x][y].mny;
			int mxx = passive[x][y].mxx;
			int mxy = passive[x][y].mxy;

			if(mxx >= m || mxy >= n || mnx < 0 || mny < 0) continue;
			
			node ret = qryx(
				mny + 1,
				mxy,
				mnx + 1,
				mxx
			);
			bool f = 0;
			if(!ret.on) f = 1;
			if(ret.mnx < mnx) f = 1;
			if(ret.mny < mny) f = 1;
			if(ret.mxx > mxx) f = 1;
			if(ret.mxy > mxy) f = 1;

			if(!f)
				res++;
		}
	}
	return res;
}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 55 ms 164824 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 53 ms 164684 KB Output is correct
6 Correct 31 ms 94300 KB Output is correct
7 Correct 30 ms 94296 KB Output is correct
8 Correct 30 ms 94044 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 32 ms 94300 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 53 ms 164788 KB Output is correct
16 Correct 0 ms 416 KB Output is correct
17 Correct 56 ms 164624 KB Output is correct
18 Correct 57 ms 164684 KB Output is correct
19 Correct 53 ms 164700 KB Output is correct
20 Correct 53 ms 164696 KB Output is correct
21 Correct 53 ms 164780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 55 ms 164824 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 53 ms 164684 KB Output is correct
6 Correct 31 ms 94300 KB Output is correct
7 Correct 30 ms 94296 KB Output is correct
8 Correct 30 ms 94044 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 32 ms 94300 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 53 ms 164788 KB Output is correct
16 Correct 0 ms 416 KB Output is correct
17 Correct 56 ms 164624 KB Output is correct
18 Correct 57 ms 164684 KB Output is correct
19 Correct 53 ms 164700 KB Output is correct
20 Correct 53 ms 164696 KB Output is correct
21 Correct 53 ms 164780 KB Output is correct
22 Correct 13 ms 2136 KB Output is correct
23 Correct 69 ms 166228 KB Output is correct
24 Correct 20 ms 3164 KB Output is correct
25 Correct 12 ms 1880 KB Output is correct
26 Correct 9 ms 1884 KB Output is correct
27 Correct 11 ms 4956 KB Output is correct
28 Correct 9 ms 1884 KB Output is correct
29 Correct 35 ms 95072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 55 ms 164824 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 53 ms 164684 KB Output is correct
6 Correct 31 ms 94300 KB Output is correct
7 Correct 30 ms 94296 KB Output is correct
8 Correct 30 ms 94044 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 32 ms 94300 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 53 ms 164788 KB Output is correct
16 Correct 0 ms 416 KB Output is correct
17 Correct 13 ms 2136 KB Output is correct
18 Correct 69 ms 166228 KB Output is correct
19 Correct 20 ms 3164 KB Output is correct
20 Correct 12 ms 1880 KB Output is correct
21 Correct 9 ms 1884 KB Output is correct
22 Correct 11 ms 4956 KB Output is correct
23 Correct 9 ms 1884 KB Output is correct
24 Correct 35 ms 95072 KB Output is correct
25 Correct 56 ms 164624 KB Output is correct
26 Correct 57 ms 164684 KB Output is correct
27 Correct 53 ms 164700 KB Output is correct
28 Correct 53 ms 164696 KB Output is correct
29 Correct 53 ms 164780 KB Output is correct
30 Correct 115 ms 8796 KB Output is correct
31 Correct 104 ms 8796 KB Output is correct
32 Correct 150 ms 101712 KB Output is correct
33 Correct 56 ms 7004 KB Output is correct
34 Correct 71 ms 7000 KB Output is correct
35 Correct 120 ms 101792 KB Output is correct
36 Correct 78 ms 16988 KB Output is correct
37 Correct 149 ms 172120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 55 ms 164824 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 53 ms 164684 KB Output is correct
6 Correct 31 ms 94300 KB Output is correct
7 Correct 30 ms 94296 KB Output is correct
8 Correct 30 ms 94044 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 32 ms 94300 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 53 ms 164788 KB Output is correct
16 Correct 0 ms 416 KB Output is correct
17 Correct 13 ms 2136 KB Output is correct
18 Correct 69 ms 166228 KB Output is correct
19 Correct 20 ms 3164 KB Output is correct
20 Correct 12 ms 1880 KB Output is correct
21 Correct 9 ms 1884 KB Output is correct
22 Correct 11 ms 4956 KB Output is correct
23 Correct 9 ms 1884 KB Output is correct
24 Correct 35 ms 95072 KB Output is correct
25 Correct 115 ms 8796 KB Output is correct
26 Correct 104 ms 8796 KB Output is correct
27 Correct 150 ms 101712 KB Output is correct
28 Correct 56 ms 7004 KB Output is correct
29 Correct 71 ms 7000 KB Output is correct
30 Correct 120 ms 101792 KB Output is correct
31 Correct 78 ms 16988 KB Output is correct
32 Correct 149 ms 172120 KB Output is correct
33 Correct 56 ms 164624 KB Output is correct
34 Correct 57 ms 164684 KB Output is correct
35 Correct 53 ms 164700 KB Output is correct
36 Correct 53 ms 164696 KB Output is correct
37 Correct 53 ms 164780 KB Output is correct
38 Correct 886 ms 109396 KB Output is correct
39 Correct 902 ms 109280 KB Output is correct
40 Correct 883 ms 109408 KB Output is correct
41 Correct 882 ms 109392 KB Output is correct
42 Correct 2273 ms 122704 KB Output is correct
43 Correct 2212 ms 132180 KB Output is correct
44 Correct 2318 ms 156248 KB Output is correct
45 Correct 2092 ms 248044 KB Output is correct
46 Correct 109 ms 186112 KB Output is correct
47 Correct 995 ms 101492 KB Output is correct
48 Correct 1828 ms 102520 KB Output is correct
49 Correct 2153 ms 204352 KB Output is correct
50 Correct 894 ms 149588 KB Output is correct
51 Correct 910 ms 149588 KB Output is correct
52 Correct 1729 ms 103580 KB Output is correct
53 Correct 1972 ms 203104 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 38 ms 95928 KB Output is correct
2 Correct 39 ms 95924 KB Output is correct
3 Correct 70 ms 165000 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 41 ms 96124 KB Output is correct
6 Correct 66 ms 166552 KB Output is correct
7 Correct 55 ms 143032 KB Output is correct
8 Correct 7 ms 2232 KB Output is correct
9 Correct 40 ms 96112 KB Output is correct
10 Correct 52 ms 157444 KB Output is correct
11 Correct 13 ms 27320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 164624 KB Output is correct
2 Correct 57 ms 164684 KB Output is correct
3 Correct 53 ms 164700 KB Output is correct
4 Correct 53 ms 164696 KB Output is correct
5 Correct 53 ms 164780 KB Output is correct
6 Correct 55 ms 164696 KB Output is correct
7 Correct 382 ms 290276 KB Output is correct
8 Correct 850 ms 447928 KB Output is correct
9 Correct 825 ms 449224 KB Output is correct
10 Correct 828 ms 448632 KB Output is correct
11 Correct 342 ms 316884 KB Output is correct
12 Correct 677 ms 452956 KB Output is correct
13 Correct 710 ms 472636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 55 ms 164824 KB Output is correct
4 Correct 1 ms 860 KB Output is correct
5 Correct 53 ms 164684 KB Output is correct
6 Correct 31 ms 94300 KB Output is correct
7 Correct 30 ms 94296 KB Output is correct
8 Correct 30 ms 94044 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 604 KB Output is correct
12 Correct 32 ms 94300 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 53 ms 164788 KB Output is correct
16 Correct 0 ms 416 KB Output is correct
17 Correct 13 ms 2136 KB Output is correct
18 Correct 69 ms 166228 KB Output is correct
19 Correct 20 ms 3164 KB Output is correct
20 Correct 12 ms 1880 KB Output is correct
21 Correct 9 ms 1884 KB Output is correct
22 Correct 11 ms 4956 KB Output is correct
23 Correct 9 ms 1884 KB Output is correct
24 Correct 35 ms 95072 KB Output is correct
25 Correct 115 ms 8796 KB Output is correct
26 Correct 104 ms 8796 KB Output is correct
27 Correct 150 ms 101712 KB Output is correct
28 Correct 56 ms 7004 KB Output is correct
29 Correct 71 ms 7000 KB Output is correct
30 Correct 120 ms 101792 KB Output is correct
31 Correct 78 ms 16988 KB Output is correct
32 Correct 149 ms 172120 KB Output is correct
33 Correct 886 ms 109396 KB Output is correct
34 Correct 902 ms 109280 KB Output is correct
35 Correct 883 ms 109408 KB Output is correct
36 Correct 882 ms 109392 KB Output is correct
37 Correct 2273 ms 122704 KB Output is correct
38 Correct 2212 ms 132180 KB Output is correct
39 Correct 2318 ms 156248 KB Output is correct
40 Correct 2092 ms 248044 KB Output is correct
41 Correct 109 ms 186112 KB Output is correct
42 Correct 995 ms 101492 KB Output is correct
43 Correct 1828 ms 102520 KB Output is correct
44 Correct 2153 ms 204352 KB Output is correct
45 Correct 894 ms 149588 KB Output is correct
46 Correct 910 ms 149588 KB Output is correct
47 Correct 1729 ms 103580 KB Output is correct
48 Correct 1972 ms 203104 KB Output is correct
49 Correct 38 ms 95928 KB Output is correct
50 Correct 39 ms 95924 KB Output is correct
51 Correct 70 ms 165000 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 41 ms 96124 KB Output is correct
54 Correct 66 ms 166552 KB Output is correct
55 Correct 55 ms 143032 KB Output is correct
56 Correct 7 ms 2232 KB Output is correct
57 Correct 40 ms 96112 KB Output is correct
58 Correct 52 ms 157444 KB Output is correct
59 Correct 13 ms 27320 KB Output is correct
60 Correct 55 ms 164696 KB Output is correct
61 Correct 382 ms 290276 KB Output is correct
62 Correct 850 ms 447928 KB Output is correct
63 Correct 825 ms 449224 KB Output is correct
64 Correct 828 ms 448632 KB Output is correct
65 Correct 342 ms 316884 KB Output is correct
66 Correct 677 ms 452956 KB Output is correct
67 Correct 710 ms 472636 KB Output is correct
68 Correct 56 ms 164624 KB Output is correct
69 Correct 57 ms 164684 KB Output is correct
70 Correct 53 ms 164700 KB Output is correct
71 Correct 53 ms 164696 KB Output is correct
72 Correct 53 ms 164780 KB Output is correct
73 Runtime error 618 ms 1048576 KB Execution killed with signal 9
74 Halted 0 ms 0 KB -