Submission #1048809

# Submission time Handle Problem Language Result Execution time Memory
1048809 2024-08-08T09:36:53 Z Kel_Mahmut Rectangles (IOI19_rect) C++14
100 / 100
4165 ms 998220 KB
#include "rect.h"
#include <bits/stdc++.h>
#define pb push_back
// #define endl ("\n")
#define all(aa) aa.begin(), aa.end()
typedef long long ll;
using namespace std;

struct DSU{
	int n, t;
	vector<int> p;
	DSU(int n, int t) : n(n), t(t), p(n) {
		for(int i = 0; i < n; i++) p[i] = i;
	}

	int find(int u){
		if(p[u] == u) return u;
		return p[u] = find(p[u]);
	}

	void merge(int u, int v){
		u = find(u);
		v = find(v);
		if(u == v) return;
		if(u > v) swap(u, v);
		if(t)
			p[u] = v;
		else
			p[v] = u;
	}
};

struct fenwick{
	int n;
	vector<int> fen;
	fenwick(int n) : n(n), fen(n){}

	void upd(int pos, int val){
		for(; pos < n; pos |= pos + 1)
			fen[pos] += val;
	}

	int query(int pos){
		int ret = 0;
		for(; pos >= 0; pos = (pos & (pos + 1)) - 1)
			ret += fen[pos];
		return ret;
	}

	int query(int ql, int qr){
		return query(qr) - query(ql - 1);
	}

};

ll count_rectangles(vector<vector<int>> a){
	int n = a.size(), m = a[0].size();
	ll ans = 0;

	vector<vector<pair<int, int>>> rows(n);
	for(int i = 0; i < n; i++){
		vector<pair<int, int>> vals;
		vector<int> act(m);
		DSU left(m, 0), right(m, 1);
		for(int j = 0; j < m; j++)
			vals.pb({a[i][j], j});
		sort(all(vals));
		vector<vector<int>> same_idx(m);
		int c = -1;
		for(int k = 0, lst = -1; k < m; k++){
			c += (lst != vals[k].first);
			same_idx[c].pb(vals[k].second);
			lst = vals[k].first;
		}
		same_idx.resize(c);

		for(int j = 0; j < c; j++){
			for(int ind : same_idx[j]){
				act[ind] = 1;
				if(ind && act[ind - 1])
					left.merge(ind - 1, ind), right.merge(ind - 1, ind);
				if(ind < m - 1 && act[ind + 1])
					left.merge(ind, ind + 1), right.merge(ind, ind + 1);
			}
			set<pair<int, int>> interval;
			for(int ind : same_idx[j])
				interval.insert({left.find(ind), right.find(ind)});
			for(auto [tl, tr] : interval)
				if(tl != 0 && tr != m - 1)
					rows[i].pb({tl, tr});
		}
	}

	vector<vector<vector<pair<int, int>>>> down_extend(n, vector<vector<pair<int, int>>>(m));
	vector<vector<int>> dp(m, vector<int>(m, -1));
	vector<vector<int>> dp2(m, vector<int>(m, -1));
	for(int i = n - 1; i >= 0; i--){
		for(auto [tl, tr] : rows[i]){
			int cev = (dp[tl][tr] == i + 1 ? dp2[tl][tr] + 1 : 1);
			down_extend[i][tl].pb({tr, cev});
			dp[tl][tr] = i;
			dp2[tl][tr] = cev;
		}
	}

	vector<vector<pair<int, int>>> cols(m);
	for(int i = 0; i < m; i++){
		vector<pair<int, int>> vals;
		vector<int> act(n);
		DSU up(n, 0), down(n, 1);
		for(int j = 0; j < n; j++)
			vals.pb({a[j][i], j});
		sort(all(vals));
		vector<vector<int>> same_idx(n);
		int c = -1;
		for(int k = 0, lst = - 1; k < n; k++){
			c += (vals[k].first != lst);
			same_idx[c].pb(vals[k].second);
			lst = vals[k].first;
		}
		same_idx.resize(c);

		for(int j = 0; j < c; j++){
			for(int ind : same_idx[j]){
				act[ind] = 1;
				if(ind && act[ind - 1])
					up.merge(ind - 1, ind), down.merge(ind - 1, ind);
				if(ind < n - 1 && act[ind + 1])
					up.merge(ind, ind + 1), down.merge(ind, ind + 1);
			}
			set<pair<int, int>> interval;
			for(int ind : same_idx[j])
				interval.insert({up.find(ind), down.find(ind)});
			for(auto [tl, tr] : interval)
				if(tl != 0 && tr != n - 1)
					cols[i].pb({tl, tr});
		}
	}

	vector<vector<vector<pair<int, int>>>> right_extend(n, vector<vector<pair<int, int>>>(m));
	vector<vector<int>> DP(n, vector<int>(n));
	vector<vector<int>> DP2(n, vector<int>(n));
	for(int i = m - 1; i >= 0; i--){
		for(auto [tl, tr] : cols[i]){
			int cev = (DP[tl][tr] == i + 1 ? DP2[tl][tr] + 1 : 1);
			right_extend[tl][i].pb({cev, tr});
			DP[tl][tr] = i;
			DP2[tl][tr] = cev;
		}
	}

	fenwick fen(n);
	for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
			sort(all(right_extend[i][j]));
			sort(all(down_extend[i][j])); reverse(all(down_extend[i][j]));
	}

	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			int c = right_extend[i][j].size() - 1;
			vector<int> rollback;
			for(auto [tr, ext] : down_extend[i][j]){
				while(c >= 0 && j + right_extend[i][j][c].first - 1 >= tr){
					fen.upd(right_extend[i][j][c].second , 1);
					rollback.pb(right_extend[i][j][c].second);
					c--;
				}
				ans += fen.query(i, i + ext - 1);
			}
			for(int x : rollback)
				fen.upd(x, -1);
		}
	}

	return ans;
}

Compilation message

rect.cpp: In function 'll count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:88:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   88 |    for(auto [tl, tr] : interval)
      |             ^
rect.cpp:98:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   98 |   for(auto [tl, tr] : rows[i]){
      |            ^
rect.cpp:134:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  134 |    for(auto [tl, tr] : interval)
      |             ^
rect.cpp:144:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  144 |   for(auto [tl, tr] : cols[i]){
      |            ^
rect.cpp:162:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  162 |    for(auto [tr, ext] : down_extend[i][j]){
      |             ^
# 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 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 760 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 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 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 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 760 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 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 1372 KB Output is correct
24 Correct 2 ms 1372 KB Output is correct
25 Correct 3 ms 860 KB Output is correct
26 Correct 3 ms 1116 KB Output is correct
27 Correct 4 ms 1212 KB Output is correct
28 Correct 3 ms 1208 KB Output is correct
29 Correct 1 ms 860 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 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 760 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 2 ms 1372 KB Output is correct
18 Correct 3 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 4 ms 1212 KB Output is correct
23 Correct 3 ms 1208 KB Output is correct
24 Correct 1 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 14 ms 6748 KB Output is correct
31 Correct 14 ms 6580 KB Output is correct
32 Correct 20 ms 6748 KB Output is correct
33 Correct 11 ms 4444 KB Output is correct
34 Correct 19 ms 5468 KB Output is correct
35 Correct 20 ms 5724 KB Output is correct
36 Correct 18 ms 5528 KB Output is correct
37 Correct 18 ms 5500 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 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 760 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 2 ms 1372 KB Output is correct
18 Correct 3 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 4 ms 1212 KB Output is correct
23 Correct 3 ms 1208 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 14 ms 6748 KB Output is correct
26 Correct 14 ms 6580 KB Output is correct
27 Correct 20 ms 6748 KB Output is correct
28 Correct 11 ms 4444 KB Output is correct
29 Correct 19 ms 5468 KB Output is correct
30 Correct 20 ms 5724 KB Output is correct
31 Correct 18 ms 5528 KB Output is correct
32 Correct 18 ms 5500 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 150 ms 59312 KB Output is correct
39 Correct 176 ms 54356 KB Output is correct
40 Correct 127 ms 54352 KB Output is correct
41 Correct 118 ms 49748 KB Output is correct
42 Correct 170 ms 80212 KB Output is correct
43 Correct 173 ms 79952 KB Output is correct
44 Correct 185 ms 80288 KB Output is correct
45 Correct 184 ms 75564 KB Output is correct
46 Correct 144 ms 46608 KB Output is correct
47 Correct 151 ms 51688 KB Output is correct
48 Correct 279 ms 65996 KB Output is correct
49 Correct 283 ms 68060 KB Output is correct
50 Correct 140 ms 35096 KB Output is correct
51 Correct 117 ms 35168 KB Output is correct
52 Correct 273 ms 65752 KB Output is correct
53 Correct 267 ms 66384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 50264 KB Output is correct
2 Correct 14 ms 36696 KB Output is correct
3 Correct 17 ms 49992 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 19 ms 50192 KB Output is correct
6 Correct 23 ms 50268 KB Output is correct
7 Correct 19 ms 50140 KB Output is correct
8 Correct 20 ms 50264 KB Output is correct
9 Correct 19 ms 50268 KB Output is correct
10 Correct 18 ms 49756 KB Output is correct
11 Correct 18 ms 50012 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 348 KB Output is correct
7 Correct 792 ms 281420 KB Output is correct
8 Correct 1748 ms 587580 KB Output is correct
9 Correct 1671 ms 590260 KB Output is correct
10 Correct 1736 ms 590248 KB Output is correct
11 Correct 292 ms 236284 KB Output is correct
12 Correct 539 ms 424832 KB Output is correct
13 Correct 589 ms 452416 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 0 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 0 ms 348 KB Output is correct
12 Correct 1 ms 760 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 2 ms 1372 KB Output is correct
18 Correct 3 ms 1372 KB Output is correct
19 Correct 2 ms 1372 KB Output is correct
20 Correct 3 ms 860 KB Output is correct
21 Correct 3 ms 1116 KB Output is correct
22 Correct 4 ms 1212 KB Output is correct
23 Correct 3 ms 1208 KB Output is correct
24 Correct 1 ms 860 KB Output is correct
25 Correct 14 ms 6748 KB Output is correct
26 Correct 14 ms 6580 KB Output is correct
27 Correct 20 ms 6748 KB Output is correct
28 Correct 11 ms 4444 KB Output is correct
29 Correct 19 ms 5468 KB Output is correct
30 Correct 20 ms 5724 KB Output is correct
31 Correct 18 ms 5528 KB Output is correct
32 Correct 18 ms 5500 KB Output is correct
33 Correct 150 ms 59312 KB Output is correct
34 Correct 176 ms 54356 KB Output is correct
35 Correct 127 ms 54352 KB Output is correct
36 Correct 118 ms 49748 KB Output is correct
37 Correct 170 ms 80212 KB Output is correct
38 Correct 173 ms 79952 KB Output is correct
39 Correct 185 ms 80288 KB Output is correct
40 Correct 184 ms 75564 KB Output is correct
41 Correct 144 ms 46608 KB Output is correct
42 Correct 151 ms 51688 KB Output is correct
43 Correct 279 ms 65996 KB Output is correct
44 Correct 283 ms 68060 KB Output is correct
45 Correct 140 ms 35096 KB Output is correct
46 Correct 117 ms 35168 KB Output is correct
47 Correct 273 ms 65752 KB Output is correct
48 Correct 267 ms 66384 KB Output is correct
49 Correct 21 ms 50264 KB Output is correct
50 Correct 14 ms 36696 KB Output is correct
51 Correct 17 ms 49992 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 19 ms 50192 KB Output is correct
54 Correct 23 ms 50268 KB Output is correct
55 Correct 19 ms 50140 KB Output is correct
56 Correct 20 ms 50264 KB Output is correct
57 Correct 19 ms 50268 KB Output is correct
58 Correct 18 ms 49756 KB Output is correct
59 Correct 18 ms 50012 KB Output is correct
60 Correct 1 ms 348 KB Output is correct
61 Correct 792 ms 281420 KB Output is correct
62 Correct 1748 ms 587580 KB Output is correct
63 Correct 1671 ms 590260 KB Output is correct
64 Correct 1736 ms 590248 KB Output is correct
65 Correct 292 ms 236284 KB Output is correct
66 Correct 539 ms 424832 KB Output is correct
67 Correct 589 ms 452416 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 2003 ms 744940 KB Output is correct
74 Correct 1833 ms 678136 KB Output is correct
75 Correct 1733 ms 678020 KB Output is correct
76 Correct 1579 ms 611020 KB Output is correct
77 Correct 2525 ms 998148 KB Output is correct
78 Correct 2325 ms 498532 KB Output is correct
79 Correct 2455 ms 531656 KB Output is correct
80 Correct 4021 ms 811088 KB Output is correct
81 Correct 2298 ms 515692 KB Output is correct
82 Correct 3123 ms 667080 KB Output is correct
83 Correct 4165 ms 839764 KB Output is correct
84 Correct 2167 ms 493080 KB Output is correct
85 Correct 3862 ms 817024 KB Output is correct
86 Correct 3664 ms 794012 KB Output is correct
87 Correct 1423 ms 611136 KB Output is correct
88 Correct 2492 ms 997236 KB Output is correct
89 Correct 2678 ms 997816 KB Output is correct
90 Correct 2629 ms 998220 KB Output is correct