Submission #937420

# Submission time Handle Problem Language Result Execution time Memory
937420 2024-03-04T03:32:40 Z GrindMachine Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 710504 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) (int)a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x,y) ((x+y-1)/(y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a,b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a,b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

read some solutions long back, remember some ideas from there

*/

const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

#include "rect.h"

long long count_rectangles(std::vector<std::vector<int> > a) {
	int n = sz(a), m = sz(a[0]);
	if(n <= 2 or m <= 2) return 0;

	vector<pii> up[n][m];
	int ptr = 0;

	rep(j,m){
		stack<int> st;
		rep(i,n){
			bool eq = false;
			while(!st.empty() and a[i][j] >= a[st.top()][j]){
				int k = st.top();
				st.pop();
				if(a[i][j] == a[k][j]){
					eq = true;
				}
				if(i-k >= 2){
					up[i][j].pb({k,ptr++});
				}
			}

			if(!st.empty() and !eq){
				int k = st.top();
				if(i-k >= 2){
					up[i][j].pb({k,ptr++});
				}
			}

			st.push(i);
		}
	}

	vector<int> farthest(ptr);
	map<array<int,3>,int> mp;

	rev(j,m-1,0){
		rep(i,n){
			for(auto [k,ptr] : up[i][j]){
				array<int,3> ar = {j,k,i};
				array<int,3> want = {j+1,k,i};
				if(mp.count(want)){
					farthest[ptr] = farthest[mp[want]];
				}
				else{
					farthest[ptr] = j;
				}

				mp[ar] = ptr;
			}
		}
	}

	int prev_occ[m][m], first_occ[m][m];
	memset(prev_occ,-1,sizeof prev_occ);
	memset(first_occ,-1,sizeof first_occ);

	ll ans = 0;

	rep1(i,n-2){
		stack<int> st;
		vector<pii> good;
		rep(j,m){
			bool eq = false;
			while(!st.empty() and a[i][j] >= a[i][st.top()]){
				int k = st.top();
				st.pop();
				if(a[i][j] == a[i][k]){
					eq = true;
				}
				if(j-k >= 2){
					good.pb({k,j});
				}
			}

			// cout << j << endl;
			// cout << eq << endl;
			// auto st_copy = st;
			// while(!st_copy.empty()){
			// 	cout << st_copy.top() << " ";
			// 	st_copy.pop();
			// }
			// cout << endl;

			if(!st.empty() and !eq){
				int k = st.top();
				if(j-k >= 2){
					// cout << k << " " << j << endl;
					good.pb({k,j});
				}
			}

			st.push(j);
		}

		for(auto [l,r] : good){
			if(prev_occ[l][r] != i-1){
				first_occ[l][r] = i;
			}

			prev_occ[l][r] = i;

			for(auto [k,ptr] : up[i+1][l+1]){
				if(k >= first_occ[l][r]-1){
					if(farthest[ptr] >= r-1){
						ans++;
					}
				}
				else{
					break;
				}
			}
		}
	}

	/*

	int oans = 0;

	rep1(i1,n-1){
		rep1(j1,m-1){
			for(int i2 = i1; i2 < n-1; ++i2){
				for(int j2 = j1; j2 < m-1; ++j2){
					bool ok = true;
					for(int r = i1; r <= i2; ++r){
						for(int c = j1; c <= j2; ++c){
							if(a[r][c] >= min({a[i1-1][c],a[i2+1][c],a[r][j1-1],a[r][j2+1]})){
								ok = false;
								break;
							}
						}
						if(!ok) break;
					}

					if(ok){
						oans++;
						cout << i1 << " " << j1 << " " << i2 << " " << j2 << endl;
					}
				}
			}
		}
	}
	
	*/
	
	// cout << ans << endl;
	// cout << oans << endl;
	// cout << endl;

	// assert(ans == oans);

	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
14 Correct 0 ms 344 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 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
14 Correct 0 ms 344 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 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 0 ms 428 KB Output is correct
22 Correct 3 ms 1368 KB Output is correct
23 Correct 3 ms 1116 KB Output is correct
24 Correct 3 ms 1116 KB Output is correct
25 Correct 1 ms 860 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
27 Correct 3 ms 1116 KB Output is correct
28 Correct 3 ms 1076 KB Output is correct
29 Correct 1 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
14 Correct 0 ms 344 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 1368 KB Output is correct
18 Correct 3 ms 1116 KB Output is correct
19 Correct 3 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 3 ms 1076 KB Output is correct
24 Correct 1 ms 604 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 344 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 0 ms 428 KB Output is correct
30 Correct 18 ms 5208 KB Output is correct
31 Correct 18 ms 5196 KB Output is correct
32 Correct 18 ms 5212 KB Output is correct
33 Correct 8 ms 3420 KB Output is correct
34 Correct 20 ms 5316 KB Output is correct
35 Correct 17 ms 5468 KB Output is correct
36 Correct 17 ms 5212 KB Output is correct
37 Correct 16 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
14 Correct 0 ms 344 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 1368 KB Output is correct
18 Correct 3 ms 1116 KB Output is correct
19 Correct 3 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 3 ms 1076 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 18 ms 5208 KB Output is correct
26 Correct 18 ms 5196 KB Output is correct
27 Correct 18 ms 5212 KB Output is correct
28 Correct 8 ms 3420 KB Output is correct
29 Correct 20 ms 5316 KB Output is correct
30 Correct 17 ms 5468 KB Output is correct
31 Correct 17 ms 5212 KB Output is correct
32 Correct 16 ms 5208 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 344 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 0 ms 428 KB Output is correct
38 Correct 116 ms 40500 KB Output is correct
39 Correct 115 ms 40496 KB Output is correct
40 Correct 123 ms 45188 KB Output is correct
41 Correct 126 ms 45120 KB Output is correct
42 Correct 262 ms 59480 KB Output is correct
43 Correct 274 ms 59500 KB Output is correct
44 Correct 261 ms 59732 KB Output is correct
45 Correct 250 ms 56400 KB Output is correct
46 Correct 81 ms 32336 KB Output is correct
47 Correct 114 ms 37972 KB Output is correct
48 Correct 250 ms 61264 KB Output is correct
49 Correct 252 ms 62804 KB Output is correct
50 Correct 124 ms 30800 KB Output is correct
51 Correct 125 ms 33276 KB Output is correct
52 Correct 247 ms 58796 KB Output is correct
53 Correct 249 ms 59532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 49756 KB Output is correct
2 Correct 22 ms 36176 KB Output is correct
3 Correct 29 ms 49420 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 30 ms 49752 KB Output is correct
6 Correct 31 ms 49620 KB Output is correct
7 Correct 29 ms 49748 KB Output is correct
8 Correct 29 ms 49500 KB Output is correct
9 Correct 30 ms 49756 KB Output is correct
10 Correct 0 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 344 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 428 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 565 ms 205104 KB Output is correct
8 Correct 1335 ms 402552 KB Output is correct
9 Correct 1328 ms 404260 KB Output is correct
10 Correct 1333 ms 404224 KB Output is correct
11 Correct 147 ms 149328 KB Output is correct
12 Correct 319 ms 232980 KB Output is correct
13 Correct 342 ms 251732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 600 KB Output is correct
7 Correct 1 ms 348 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 348 KB Output is correct
14 Correct 0 ms 344 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 1368 KB Output is correct
18 Correct 3 ms 1116 KB Output is correct
19 Correct 3 ms 1116 KB Output is correct
20 Correct 1 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 3 ms 1116 KB Output is correct
23 Correct 3 ms 1076 KB Output is correct
24 Correct 1 ms 604 KB Output is correct
25 Correct 18 ms 5208 KB Output is correct
26 Correct 18 ms 5196 KB Output is correct
27 Correct 18 ms 5212 KB Output is correct
28 Correct 8 ms 3420 KB Output is correct
29 Correct 20 ms 5316 KB Output is correct
30 Correct 17 ms 5468 KB Output is correct
31 Correct 17 ms 5212 KB Output is correct
32 Correct 16 ms 5208 KB Output is correct
33 Correct 116 ms 40500 KB Output is correct
34 Correct 115 ms 40496 KB Output is correct
35 Correct 123 ms 45188 KB Output is correct
36 Correct 126 ms 45120 KB Output is correct
37 Correct 262 ms 59480 KB Output is correct
38 Correct 274 ms 59500 KB Output is correct
39 Correct 261 ms 59732 KB Output is correct
40 Correct 250 ms 56400 KB Output is correct
41 Correct 81 ms 32336 KB Output is correct
42 Correct 114 ms 37972 KB Output is correct
43 Correct 250 ms 61264 KB Output is correct
44 Correct 252 ms 62804 KB Output is correct
45 Correct 124 ms 30800 KB Output is correct
46 Correct 125 ms 33276 KB Output is correct
47 Correct 247 ms 58796 KB Output is correct
48 Correct 249 ms 59532 KB Output is correct
49 Correct 30 ms 49756 KB Output is correct
50 Correct 22 ms 36176 KB Output is correct
51 Correct 29 ms 49420 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 30 ms 49752 KB Output is correct
54 Correct 31 ms 49620 KB Output is correct
55 Correct 29 ms 49748 KB Output is correct
56 Correct 29 ms 49500 KB Output is correct
57 Correct 30 ms 49756 KB Output is correct
58 Correct 0 ms 348 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 565 ms 205104 KB Output is correct
62 Correct 1335 ms 402552 KB Output is correct
63 Correct 1328 ms 404260 KB Output is correct
64 Correct 1333 ms 404224 KB Output is correct
65 Correct 147 ms 149328 KB Output is correct
66 Correct 319 ms 232980 KB Output is correct
67 Correct 342 ms 251732 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 344 KB Output is correct
71 Correct 1 ms 348 KB Output is correct
72 Correct 0 ms 428 KB Output is correct
73 Correct 2382 ms 484060 KB Output is correct
74 Correct 2365 ms 483920 KB Output is correct
75 Correct 1999 ms 597224 KB Output is correct
76 Correct 2083 ms 597772 KB Output is correct
77 Execution timed out 5094 ms 710504 KB Time limit exceeded
78 Halted 0 ms 0 KB -