Submission #1049285

# Submission time Handle Problem Language Result Execution time Memory
1049285 2024-08-08T15:57:12 Z efedmrlr Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 1016992 KB
#include "rect.h"
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>

using namespace std;


#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void fastio() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}


const double EPS = 0.00001;
const int INF = 1e9+500;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int n, m;

struct DSU {
	vector<int> p, sz;
	vector<int> l, r;
	vector<int> used;
	int mx;
	void reset(int s, int mm) {
		p.assign(s + 3, -1);
		sz.assign(s + 3, 0);
		l.assign(s + 3, INF);
		r.assign(s + 3, 0);
		used.assign(s + 3, -1);
		mx = mm;
	}
	void make_set(int x)  {
		p[x] = x;
		sz[x] = 1;
		l[x] = r[x] = x;
	}
	int find_set(int x) {
		return p[x] == x ? x : find_set(p[x]);
 	}
	void union_set(int x, int y) {
		x = find_set(x); y = find_set(y);
		if(x == y) return;
		if(sz[x] < sz[y]) swap(x, y);
		sz[x] += sz[y];
		p[y] = x;
		l[x] = min(l[x], l[y]);
		r[x] = max(r[x], r[y]);	
	}
	bool check_val(int x) {
		return !(l[x] == 0 || r[x] == mx);
	}

};

struct Fenwick {
	vector<int> data;
	int sz;
	void reset(int s) {
		sz = s;
		data.assign(sz + 3, 0);
	}
	void update(int ind, int val) {
		ind++;
		while(ind <= sz) {
			data[ind] += val;
			ind += (-ind) & ind;
		}
	} 
	int getSum(int ind) {
		ind++;
		int ret = 0;
		while(ind > 0) {
			ret += data[ind];
			ind -= ind & (-ind);
		}
		return ret;
	}
	int query(int l, int r) {
		return getSum(r) - (l > 0 ? getSum(l - 1) : 0);
	} 
};

long long count_rectangles(std::vector<std::vector<int> > a) {
	n = (int)a.size();
	m = (int)a[0].size();
	vector<vector<array<int, 3 > > > row(n), col(m);
	
	REP(i, n - 1) {
		if(i == 0) continue;
		DSU ds;
		ds.reset(m, m - 1);
		vector<int> ord;
		REP(j, m) ord.pb(j);
		sort(all(ord), [&](int x, int y) {
			return a[i][x] < a[i][y];
		});
		int last = -1, lind = 0;
		for(int k = 0; k  < ord.size(); k++) {
			auto &c = ord[k];
			if(a[i][c] != last) {
				for(int j = lind; j < k; j++) {
					if(ds.used[ds.find_set(ord[j])] == lind) {
						continue;
					} 
					ds.used[ds.find_set(ord[j])] = lind;
					if(!ds.check_val(ds.find_set(ord[j]))) continue;
					row[i].pb({ds.l[ds.find_set(ord[j])], ds.r[ds.find_set(ord[j])]});
				}
				
				last = a[i][c];
				lind = k;
			}
			ds.make_set(c);
			if(ds.p[c + 1] != -1) ds.union_set(c, c + 1);
			if(c > 0 && ds.p[c - 1] != -1) ds.union_set(c, c - 1);
		}
		for(int j = lind; j < ord.size(); j++) {
			if(ds.used[ds.find_set(ord[j])] == lind) {
				continue;
			} 
			ds.used[ds.find_set(ord[j])] = lind;
			if(!ds.check_val(ds.find_set(ord[j]))) continue;
				row[i].pb({ds.l[ds.find_set(ord[j])], ds.r[ds.find_set(ord[j])], 0});
			}
	}
	REP(j, m - 1) {
		if(j == 0) continue;
		DSU ds;
		ds.reset(n, n - 1);
		vector<int> ord;
		REP(i, n) ord.pb(i);
		sort(all(ord), [&](int x, int y) {
			return a[x][j] < a[y][j];
		});
		int last = -1, lind = 0;
		for(int k = 0; k < ord.size(); k++) {
			auto &c = ord[k];
			if(a[c][j] != last) {
				for(int i = lind; i < k; i++) {
					if(ds.used[ds.find_set(ord[i])] == lind) {
						continue;
					} 
					ds.used[ds.find_set(ord[i])] = lind;
					if(!ds.check_val(ds.find_set(ord[i]))) continue;
					col[j].pb({ds.l[ds.find_set(ord[i])], ds.r[ds.find_set(ord[i])]});
				}
				
				last = a[c][j];
				lind = k;
			}
			ds.make_set(c);
			if(ds.p[c + 1] != -1) ds.union_set(c, c + 1);
			if(c > 0 && ds.p[c - 1] != -1) ds.union_set(c, c - 1);
		}
		for(int i = lind; i < ord.size(); i++) {
			if(ds.used[ds.find_set(ord[i])] == lind) {
				continue;
			} 
			ds.used[ds.find_set(ord[i])] = lind;
			if(!ds.check_val(ds.find_set(ord[i]))) continue;
				col[j].pb({ds.l[ds.find_set(ord[i])], ds.r[ds.find_set(ord[i])], 0});
		}
	}
	// cout << "rows: \n";
	// for(int i = 0; i < n; i++) {
	// 	cout << "i:" << i << "\n";
	// 	for(auto c : row[i]) {
	// 		cout << c[0] << " " << c[1] << "\n";
	// 	}
	// } 

	map<array<int, 3>, int > mpp;
	for(int i = n - 1; i >= 0; i--) {
		for(auto &c : row[i]) {
			mpp[{i, c[0], c[1]}] = c[2] = mpp[{i + 1, c[0], c[1]}] + 1;
		}
	}
	mpp.clear();
	for(int i = m - 1; i >= 0; i--) {
		for(auto &c : col[i]) {
			mpp[{i, c[0], c[1]}] = c[2] = mpp[{i + 1, c[0], c[1]}] + 1;
		}
	}
	
	vector<vector<vector<array<int, 2> > > > rows(n, vector<vector<array<int, 2> > >(m)), cols(n, vector<vector<array<int, 2> > >(m));
	for(int i = 0; i < n; i++) {
		for(auto c : row[i]) {
			rows[i][c[0]].pb({c[1] - c[0] + 1, c[2]});
		}
		for(int j = 0; j < m; j++) {
			sort(all(rows[i][j]), [&](array<int, 2> x, array<int, 2> y) {
				return x[1] < y[1];
			});
		}
	}
	// cout << "zort" << endl;
	for(int j = 0; j < m; j++) {
		for(auto c: col[j]) {
			cols[c[0]][j].pb({c[1] - c[0] + 1, c[2]});
		}
		for(int i = 0; i < n; i++) {
			sort(all(cols[i][j]), [&](array<int, 2> x, array<int, 2> y) {
				return x[0] < y[0];
			});
		}
	}
	// cout << "zort" << endl;
	Fenwick st;
	st.reset(max(n, m) + 2);
	int ans = 0;
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			int id = 0;
			for(auto c: rows[i][j]) {
				while(id < cols[i][j].size() && cols[i][j][id][0] <= c[1]) {
					st.update(cols[i][j][id][1], 1);
					id++;
				}
				ans += st.query(c[0], max(n, m));
			}
			while(id > 0) {
				id--;
				st.update(cols[i][j][id][1], -1);
			}
		}
	}
	return ans;
}

// int main() {
// 	int n, m;
// 	cin >> n >> m;
// 	vector<vector<int> > grid(n,vector<int>(m));
// 	REP(i, n) REP(j, m) {
// 		cin >> grid[i][j];
// 	}
// 	cout << count_rectangles(grid) << "\n";
// 	return 0;
// }

Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:110:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  110 |   for(int k = 0; k  < ord.size(); k++) {
      |                  ~~~^~~~~~~~~~~~
rect.cpp:129:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  129 |   for(int j = lind; j < ord.size(); j++) {
      |                     ~~^~~~~~~~~~~~
rect.cpp:148:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |   for(int k = 0; k < ord.size(); k++) {
      |                  ~~^~~~~~~~~~~~
rect.cpp:167:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |   for(int i = lind; i < ord.size(); i++) {
      |                     ~~^~~~~~~~~~~~
rect.cpp:227:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  227 |     while(id < cols[i][j].size() && cols[i][j][id][0] <= c[1]) {
      |           ~~~^~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 604 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 348 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 0 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 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 604 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 348 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 0 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 4 ms 1624 KB Output is correct
23 Correct 4 ms 1628 KB Output is correct
24 Correct 4 ms 1628 KB Output is correct
25 Correct 2 ms 1116 KB Output is correct
26 Correct 5 ms 1624 KB Output is correct
27 Correct 6 ms 1868 KB Output is correct
28 Correct 4 ms 1732 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 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 604 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 4 ms 1624 KB Output is correct
18 Correct 4 ms 1628 KB Output is correct
19 Correct 4 ms 1628 KB Output is correct
20 Correct 2 ms 1116 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 6 ms 1868 KB Output is correct
23 Correct 4 ms 1732 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 0 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 28 ms 8540 KB Output is correct
31 Correct 27 ms 8540 KB Output is correct
32 Correct 27 ms 8528 KB Output is correct
33 Correct 14 ms 5724 KB Output is correct
34 Correct 35 ms 9392 KB Output is correct
35 Correct 35 ms 9600 KB Output is correct
36 Correct 33 ms 8696 KB Output is correct
37 Correct 34 ms 8792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 604 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 4 ms 1624 KB Output is correct
18 Correct 4 ms 1628 KB Output is correct
19 Correct 4 ms 1628 KB Output is correct
20 Correct 2 ms 1116 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 6 ms 1868 KB Output is correct
23 Correct 4 ms 1732 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 28 ms 8540 KB Output is correct
26 Correct 27 ms 8540 KB Output is correct
27 Correct 27 ms 8528 KB Output is correct
28 Correct 14 ms 5724 KB Output is correct
29 Correct 35 ms 9392 KB Output is correct
30 Correct 35 ms 9600 KB Output is correct
31 Correct 33 ms 8696 KB Output is correct
32 Correct 34 ms 8792 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 0 ms 348 KB Output is correct
35 Correct 0 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 230 ms 81236 KB Output is correct
39 Correct 223 ms 76372 KB Output is correct
40 Correct 222 ms 76700 KB Output is correct
41 Correct 204 ms 71804 KB Output is correct
42 Correct 401 ms 105272 KB Output is correct
43 Correct 424 ms 105048 KB Output is correct
44 Correct 415 ms 105232 KB Output is correct
45 Correct 375 ms 98584 KB Output is correct
46 Correct 150 ms 53588 KB Output is correct
47 Correct 225 ms 67924 KB Output is correct
48 Correct 570 ms 116308 KB Output is correct
49 Correct 576 ms 117424 KB Output is correct
50 Correct 267 ms 58704 KB Output is correct
51 Correct 254 ms 58344 KB Output is correct
52 Correct 502 ms 105812 KB Output is correct
53 Correct 530 ms 106064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1372 KB Output is correct
2 Correct 3 ms 1116 KB Output is correct
3 Correct 1 ms 856 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 1312 KB Output is correct
6 Correct 2 ms 1116 KB Output is correct
7 Correct 2 ms 1116 KB Output is correct
8 Correct 2 ms 1116 KB Output is correct
9 Correct 2 ms 1116 KB Output is correct
10 Correct 1 ms 604 KB Output is correct
11 Correct 1 ms 604 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 0 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 0 ms 348 KB Output is correct
7 Correct 1062 ms 315148 KB Output is correct
8 Correct 2338 ms 681064 KB Output is correct
9 Correct 2384 ms 684164 KB Output is correct
10 Correct 2314 ms 684364 KB Output is correct
11 Correct 216 ms 169556 KB Output is correct
12 Correct 475 ms 321936 KB Output is correct
13 Correct 526 ms 343380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 1 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 604 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 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 4 ms 1624 KB Output is correct
18 Correct 4 ms 1628 KB Output is correct
19 Correct 4 ms 1628 KB Output is correct
20 Correct 2 ms 1116 KB Output is correct
21 Correct 5 ms 1624 KB Output is correct
22 Correct 6 ms 1868 KB Output is correct
23 Correct 4 ms 1732 KB Output is correct
24 Correct 2 ms 860 KB Output is correct
25 Correct 28 ms 8540 KB Output is correct
26 Correct 27 ms 8540 KB Output is correct
27 Correct 27 ms 8528 KB Output is correct
28 Correct 14 ms 5724 KB Output is correct
29 Correct 35 ms 9392 KB Output is correct
30 Correct 35 ms 9600 KB Output is correct
31 Correct 33 ms 8696 KB Output is correct
32 Correct 34 ms 8792 KB Output is correct
33 Correct 230 ms 81236 KB Output is correct
34 Correct 223 ms 76372 KB Output is correct
35 Correct 222 ms 76700 KB Output is correct
36 Correct 204 ms 71804 KB Output is correct
37 Correct 401 ms 105272 KB Output is correct
38 Correct 424 ms 105048 KB Output is correct
39 Correct 415 ms 105232 KB Output is correct
40 Correct 375 ms 98584 KB Output is correct
41 Correct 150 ms 53588 KB Output is correct
42 Correct 225 ms 67924 KB Output is correct
43 Correct 570 ms 116308 KB Output is correct
44 Correct 576 ms 117424 KB Output is correct
45 Correct 267 ms 58704 KB Output is correct
46 Correct 254 ms 58344 KB Output is correct
47 Correct 502 ms 105812 KB Output is correct
48 Correct 530 ms 106064 KB Output is correct
49 Correct 4 ms 1372 KB Output is correct
50 Correct 3 ms 1116 KB Output is correct
51 Correct 1 ms 856 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 2 ms 1312 KB Output is correct
54 Correct 2 ms 1116 KB Output is correct
55 Correct 2 ms 1116 KB Output is correct
56 Correct 2 ms 1116 KB Output is correct
57 Correct 2 ms 1116 KB Output is correct
58 Correct 1 ms 604 KB Output is correct
59 Correct 1 ms 604 KB Output is correct
60 Correct 0 ms 348 KB Output is correct
61 Correct 1062 ms 315148 KB Output is correct
62 Correct 2338 ms 681064 KB Output is correct
63 Correct 2384 ms 684164 KB Output is correct
64 Correct 2314 ms 684364 KB Output is correct
65 Correct 216 ms 169556 KB Output is correct
66 Correct 475 ms 321936 KB Output is correct
67 Correct 526 ms 343380 KB Output is correct
68 Correct 0 ms 348 KB Output is correct
69 Correct 0 ms 348 KB Output is correct
70 Correct 0 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 3561 ms 1016992 KB Output is correct
74 Correct 3263 ms 950156 KB Output is correct
75 Correct 3628 ms 997344 KB Output is correct
76 Correct 2895 ms 929636 KB Output is correct
77 Execution timed out 5032 ms 654932 KB Time limit exceeded
78 Halted 0 ms 0 KB -