Submission #950629

# Submission time Handle Problem Language Result Execution time Memory
950629 2024-03-20T13:59:49 Z qin Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 1048576 KB
#include <bits/stdc++.h>
#define fi first
#define se second
#define ssize(x) int(x.size())
#define pn printf("\n")
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define vv vector
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
int inf = 2e09; ll infll = 2e18; int mod = 119<<23|1; // PAMIETAC, ZE TAM MAM CIAG INDEKSOWANY OD 0
struct segminmax{
		int base;
		struct Node{
				int mn, mx;
				Node(){ mn = inf, mx = 0; }
		};
		vv<Node> t;
		void init(int n, vv<int> &a, vv<int> &b){
				base = 1;
				while(base < n) base <<= 1;
				t.resize(base<<1, Node());
				for(int i = 1; i <= n; ++i) t[i+base-1].mn = b[i-1], t[i+base-1].mx = a[i-1];
				for(int i = base-1; i; --i) t[i].mn = min(t[i<<1].mn, t[i<<1|1].mn), t[i].mx = max(t[i<<1].mx, t[i<<1|1].mx);
		}
		pii query_interval(int i, int s, int e, int x, int y){
				if(x <= s && e <= y) return pii(t[i].mn, t[i].mx);
				int mid = (s+e)>>1; pii result = pii(inf, 0), tmp;
				if(x <= mid) tmp = query_interval(i<<1, s, mid, x, y), result.fi = min(result.fi, tmp.fi), result.se = max(result.se, tmp.se);
				if(mid < y) tmp = query_interval(i<<1|1, mid+1, e, x, y), result.fi = min(result.fi, tmp.fi), result.se = max(result.se, tmp.se);
				return result;
		}
		pii query(int l, int r){
				if(l <= r) return query_interval(1, 1, base, max(1, l), min(base, r));
				return pii(inf, 0);
		}
};
struct seg{
		int base;
		vv<int> t;
		void init(int n){
				base = 1;
				while(base < n) base <<= 1;
				t.resize(base<<1, 0);
		}
		void update(int i, int val){
				for(i += base-1, t[i] = val, i >>= 1; i; i >>= 1) t[i] = t[i<<1]+t[i<<1|1];
		}
		int query_interval(int i, int s, int e, int x, int y){
				if(x <= s && e <= y) return t[i];
				int mid = (s+e)>>1, result = 0;
				if(x <= mid) result += query_interval(i<<1, s, mid, x, y);
				if(mid < y) result += query_interval(i<<1|1, mid+1, e, x, y);
				return result;
		}
		int query(int l, int r){
				if(l <= r) return query_interval(1, 1, base, max(1, l), min(base, r));
				return 0;
		}
};
ll count_rectangles(vv<vv<int>> t){
		int n = ssize(t), m = ssize(t[0]);
		vv<vv<int>> u(n, vv<int>(m, 0)), d(n, vv<int>(m, n-1));
		vv<map<pii, int>> mp(n);
		vv<int> st;
		for(int i = 0; i < n; ++i){ 
				for(int j = 0; j < m; ++j){ // przedzialy gdzie ja to prawy koniec
						while(ssize(st) && t[i][st.back()] < t[i][j]) st.pop_back();
						if(ssize(st)) mp[i][pii(st.back(), j)] = 1;
						st.emplace_back(j);
				} st = vv<int>();
				for(int j = m-1; ~j; --j){  // przedzialy gdzie ja to lewy koniec
						while(ssize(st) && t[i][st.back()] < t[i][j]) st.pop_back();
						if(ssize(st)) mp[i][pii(j, st.back())] = 1;
						st.emplace_back(j);
				} st = vv<int>();
		}
		for(int j = 0; j < m; ++j){ 
				for(int i = 0; i < n; ++i){ // przedzialy gdzie ja to dolny koniec (ustawiamy najblizszego do gory)
						while(ssize(st) && t[st.back()][j] < t[i][j]) st.pop_back();
						if(ssize(st)) u[i][j] = st.back();
						st.emplace_back(i);
				} st = vv<int>();
				for(int i = n-1; ~i; --i){ // przedzialy gdzie ja to gorny koniec (ustawiamy najblizszego na dole)
						while(ssize(st) && t[st.back()][j] < t[i][j]) st.pop_back();
						if(ssize(st)) d[i][j] = st.back();
						st.emplace_back(i);
				} st = vv<int>();
		}
		//~ for(int i = 0; i < n; ++i){
				//~ for(int j = 0; j < m; ++j) printf("%d ", d[i][j]);
				//~ pn;
		//~ }
		ll result = 0;
		vv<segminmax> segm(n);
		vv<vv<int>> rem(n+1);
		seg seg; seg.init(n);
		vv<pii> queries;
		for(int i = 0; i < n; ++i) segm[i].init(m, u[i], d[i]);
		for(int i = 1; i < n-1; ++i){
				for(pair<pii, int> pd : mp[i]) if(pd.se){
						int l = pd.fi.fi+1, r = pd.fi.se-1;
						if(r < l) continue;
						++l, ++r; // do przedzialowca
						int sz = 1;
						for(int j = i+1; j < n-1; ++j) if(mp[j][pd.fi]) ++sz, mp[j][pd.fi] = 0;
													   else break;
						queries.resize(sz+2);
						for(int j = i-1; j <= i+sz; ++j) queries[j-(i-1)] = segm[j].query(l, r);
						for(int j = i; j < i+sz; ++j){
								//~ printf("%d %d %d: %lld\n", j, l-1, r-1, result);
								for(int &x : rem[j-i]) seg.update(x, 0);
								rem[j-i].clear();
								int endpoint = queries[j-1-i+1].fi;
								if(j < endpoint){
										seg.update(j-i+1, 1);
										rem[min(sz, endpoint-i)].emplace_back(j-i+1);
								}
								endpoint = queries[j+1-i+1].se;//segm[j+1].query_max(l, r);
								result += seg.query(max(-1, endpoint-i)+2, j-i+1);
								//~ printf("%d %d %d: %d %lld\n", j, l-1, r-1, endpoint, result);
						}
						for(int &x : rem[sz]) seg.update(x, 0);
						rem[sz].clear(), queries.clear();
				}
		}
		
		
		return result;
}
#ifdef LOCAL
int main(){
		int T = 1;
		for(++T; --T; ){
				int n, m; scanf("%d%d", &n, &m);
				vv<vv<int>> t(n, vv<int>(m, 0));
				for(int i = 0; i < n; ++i)
						for(int j = 0; j < m; ++j) scanf("%d", &t[i][j]);
				ll result = count_rectangles(t);
				printf("%lld\n", result);
		}
		return 0;
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 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 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 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 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 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 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 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 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 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 2 ms 1372 KB Output is correct
23 Correct 3 ms 1500 KB Output is correct
24 Correct 3 ms 1372 KB Output is correct
25 Correct 3 ms 1368 KB Output is correct
26 Correct 4 ms 1628 KB Output is correct
27 Correct 5 ms 1616 KB Output is correct
28 Correct 5 ms 1628 KB Output is correct
29 Correct 2 ms 860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 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 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 3 ms 1500 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 3 ms 1368 KB Output is correct
21 Correct 4 ms 1628 KB Output is correct
22 Correct 5 ms 1616 KB Output is correct
23 Correct 5 ms 1628 KB Output is correct
24 Correct 2 ms 860 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 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 19 ms 6748 KB Output is correct
31 Correct 15 ms 6744 KB Output is correct
32 Correct 16 ms 6748 KB Output is correct
33 Correct 18 ms 5976 KB Output is correct
34 Correct 29 ms 8532 KB Output is correct
35 Correct 30 ms 8532 KB Output is correct
36 Correct 27 ms 8024 KB Output is correct
37 Correct 27 ms 8080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 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 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 3 ms 1500 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 3 ms 1368 KB Output is correct
21 Correct 4 ms 1628 KB Output is correct
22 Correct 5 ms 1616 KB Output is correct
23 Correct 5 ms 1628 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 19 ms 6748 KB Output is correct
26 Correct 15 ms 6744 KB Output is correct
27 Correct 16 ms 6748 KB Output is correct
28 Correct 18 ms 5976 KB Output is correct
29 Correct 29 ms 8532 KB Output is correct
30 Correct 30 ms 8532 KB Output is correct
31 Correct 27 ms 8024 KB Output is correct
32 Correct 27 ms 8080 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 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 246 ms 80532 KB Output is correct
39 Correct 257 ms 80556 KB Output is correct
40 Correct 231 ms 80468 KB Output is correct
41 Correct 263 ms 80576 KB Output is correct
42 Correct 332 ms 80776 KB Output is correct
43 Correct 329 ms 80720 KB Output is correct
44 Correct 335 ms 80724 KB Output is correct
45 Correct 312 ms 75604 KB Output is correct
46 Correct 194 ms 64708 KB Output is correct
47 Correct 236 ms 72268 KB Output is correct
48 Correct 414 ms 104532 KB Output is correct
49 Correct 423 ms 105240 KB Output is correct
50 Correct 205 ms 52304 KB Output is correct
51 Correct 204 ms 52820 KB Output is correct
52 Correct 384 ms 96300 KB Output is correct
53 Correct 385 ms 96484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 1624 KB Output is correct
2 Correct 3 ms 1372 KB Output is correct
3 Correct 2 ms 1112 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 4 ms 1628 KB Output is correct
6 Correct 4 ms 1624 KB Output is correct
7 Correct 4 ms 1628 KB Output is correct
8 Correct 5 ms 1628 KB Output is correct
9 Correct 4 ms 1624 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 2 ms 1116 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 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 1 ms 544 KB Output is correct
7 Correct 1294 ms 389684 KB Output is correct
8 Correct 2866 ms 833996 KB Output is correct
9 Correct 2682 ms 844440 KB Output is correct
10 Correct 2648 ms 844304 KB Output is correct
11 Correct 816 ms 324612 KB Output is correct
12 Correct 1619 ms 626432 KB Output is correct
13 Correct 1759 ms 657284 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 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 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 348 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 344 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 1372 KB Output is correct
18 Correct 3 ms 1500 KB Output is correct
19 Correct 3 ms 1372 KB Output is correct
20 Correct 3 ms 1368 KB Output is correct
21 Correct 4 ms 1628 KB Output is correct
22 Correct 5 ms 1616 KB Output is correct
23 Correct 5 ms 1628 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 19 ms 6748 KB Output is correct
26 Correct 15 ms 6744 KB Output is correct
27 Correct 16 ms 6748 KB Output is correct
28 Correct 18 ms 5976 KB Output is correct
29 Correct 29 ms 8532 KB Output is correct
30 Correct 30 ms 8532 KB Output is correct
31 Correct 27 ms 8024 KB Output is correct
32 Correct 27 ms 8080 KB Output is correct
33 Correct 246 ms 80532 KB Output is correct
34 Correct 257 ms 80556 KB Output is correct
35 Correct 231 ms 80468 KB Output is correct
36 Correct 263 ms 80576 KB Output is correct
37 Correct 332 ms 80776 KB Output is correct
38 Correct 329 ms 80720 KB Output is correct
39 Correct 335 ms 80724 KB Output is correct
40 Correct 312 ms 75604 KB Output is correct
41 Correct 194 ms 64708 KB Output is correct
42 Correct 236 ms 72268 KB Output is correct
43 Correct 414 ms 104532 KB Output is correct
44 Correct 423 ms 105240 KB Output is correct
45 Correct 205 ms 52304 KB Output is correct
46 Correct 204 ms 52820 KB Output is correct
47 Correct 384 ms 96300 KB Output is correct
48 Correct 385 ms 96484 KB Output is correct
49 Correct 3 ms 1624 KB Output is correct
50 Correct 3 ms 1372 KB Output is correct
51 Correct 2 ms 1112 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 4 ms 1628 KB Output is correct
54 Correct 4 ms 1624 KB Output is correct
55 Correct 4 ms 1628 KB Output is correct
56 Correct 5 ms 1628 KB Output is correct
57 Correct 4 ms 1624 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 2 ms 1116 KB Output is correct
60 Correct 1 ms 544 KB Output is correct
61 Correct 1294 ms 389684 KB Output is correct
62 Correct 2866 ms 833996 KB Output is correct
63 Correct 2682 ms 844440 KB Output is correct
64 Correct 2648 ms 844304 KB Output is correct
65 Correct 816 ms 324612 KB Output is correct
66 Correct 1619 ms 626432 KB Output is correct
67 Correct 1759 ms 657284 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 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 3488 ms 1048576 KB Output is correct
74 Correct 3691 ms 1048576 KB Output is correct
75 Correct 3435 ms 1048576 KB Output is correct
76 Correct 3739 ms 1048576 KB Output is correct
77 Execution timed out 5087 ms 1048576 KB Time limit exceeded
78 Halted 0 ms 0 KB -