Submission #1068116

# Submission time Handle Problem Language Result Execution time Memory
1068116 2024-08-21T07:48:12 Z LittleOrange Rectangles (IOI19_rect) C++17
72 / 100
5000 ms 206144 KB
#include "rect.h"
#include<bits/stdc++.h>
using namespace std;
using ll = int;
using lll = long long;
const ll maxn = 2500;
struct BIT{
	ll n;
	vector<ll> a;
	BIT(ll N):n(N),a(N+1,0){}
	void add(ll i, ll v){
		for(;i<=n;i+=i&-i) a[i] += v;
	}
	ll get(ll i){
		ll x = 0;
		for(;i>0;i-=i&-i) x += a[i];
		return x;
	}
};
struct dsu{
	vector<ll> p;
	dsu(ll N):p(N,-1){}
	ll g(ll i){
		return p[i]<0?i:p[i] = g(p[i]);
	}
	bool m(ll a,ll b){
		a = g(a),b = g(b);
		if (a==b) return false;
		if (p[a]>p[b]) swap(a,b);
		p[a] += p[b];
		p[b] = a;
		return true;
	}
};

long long count_rectangles(std::vector<std::vector<int> > a) {
	ll n = a.size(), m = a[0].size();
	if (n<3||m<3) return 0;
	ll sub6 = 1;
	for(auto &o : a) for(ll &i : o) if (i>1) sub6 = 0;
	if (0&&sub6){
		dsu d(n*m);
		for(ll i = 1;i<n-1;i++){
			for(ll j = 1;j<m-1;j++){
				if(i>1&&!a[i][j]&&!a[i-1][j]){
					d.m((i)*m+(j),(i-1)*m+(j));
				}
				if(j>1&&!a[i][j]&&!a[i][j-1]){
					d.m((i)*m+(j),(i)*m+(j-1));
				}
			}
		}
		vector<ll> gs;
		for(ll i = 1;i<n-1;i++){
			for(ll j = 1;j<m-1;j++){
				if(!a[i][j]){
					gs.push_back(d.g((i)*m+(j)));
				}
			}
		}
		sort(gs.begin(),gs.end());
		gs.erase(unique(gs.begin(),gs.end()),gs.end());
		ll gc = gs.size();
		vector<vector<pair<ll,ll>>> ps(gc);
		for(ll i = 1;i<n-1;i++){
			for(ll j = 1;j<m-1;j++){
				if(!a[i][j]){
					ll p = d.g((i)*m+(j));
					ll idx = lower_bound(gs.begin(),gs.end(),p)-gs.begin();
					ps[idx].push_back({i,j});
				}
			}
		}
		ll ans = 0;
		for(auto &o : ps){
			ll x1 = o[0].first, x2=o[0].first, y1=o[0].second, y2=o[0].second;
			for(auto [x,y] : o){
				x1 = min(x,x1);
				x2 = max(x,x2);
				y1 = min(y,y1);
				y2 = max(y,y2);
			}
			ll req = (x2-x1+1)*(y2-y1+1);
			if (req==o.size()) ans++;
		}
		return ans;
	}
	vector<vector<pair<ll,ll>>> row(n),col(m);
	for(ll i = 0;i<n;i++){
		vector<pair<ll,ll>> st;
		for(ll j = 0;j<m;j++){
			while(st.size()&&st.back().first<a[i][j]){
				if(j>st.back().second+1)row[i].push_back({st.back().second,j});
				st.pop_back();
			}
			if(st.size()){
				if(j>st.back().second+1)row[i].push_back({st.back().second,j});
			}
			if (st.size()&&st.back().first==a[i][j]) st.pop_back();
			st.push_back({a[i][j],j});
		}
	}
	for(ll j = 0;j<m;j++){
		vector<pair<ll,ll>> st;
		for(ll i = 0;i<n;i++){
			while(st.size()&&st.back().first<a[i][j]){
				if(i>st.back().second+1)col[j].push_back({st.back().second,i});
				st.pop_back();
			}
			if(st.size()){
				if(i>st.back().second+1)col[j].push_back({st.back().second,i});
			}
			if (st.size()&&st.back().first==a[i][j]) st.pop_back();
			st.push_back({a[i][j],i});
		}
	}
	for(auto &o : row) sort(o.begin(),o.end());
	for(auto &o : col) sort(o.begin(),o.end());
	vector<vector<ll>> down(n,vector<ll>(m,0)),up(n,vector<ll>(m,n));
	for(ll i = 0;i<m;i++){
		for(auto [l,r] : col[i]){
			down[l][i] = r;
			up[l][i] = min(up[l][i],r);
		}
	}
	lll ans = 0;
	for(ll r1 = 1;r1<n-1;r1++){
		vector<pair<ll,ll>> cur = row[r1];
		vector<pair<ll,ll>> nw;
		BIT t(m);
		vector<vector<pair<ll,ll>>> ops(n);
		if (sub6) for(ll i = 0;i<m;i++) {
			ops[max(up[r1-1][i]-1,r1)].push_back({i+1,1});
			ops[down[r1-1][i]].push_back({i+1,1});
		}
		for(ll r2 = r1;r2<n-1;r2++){
			if (sub6) for(auto [i,v] : ops[r2]){
				t.add(i,v);
			}
			if(r2>r1){
				for(auto &o : cur){
					if (binary_search(row[r2].begin(),row[r2].end(),o)){
						nw.push_back(o);
					}
				}
				nw.swap(cur);
				nw.clear();
			}
			if (sub6){
				for(auto &o : cur){
					if (t.get(o.second)-t.get(o.first+1)==(o.second-o.first-1)) ans++;
				}
			}else{
				pair<ll,ll> t = {r1-1,r2+1};
				vector<ll> ok(m,0);
				for(ll i = 0;i<m;i++) ok[i] = down[r1-1][i]>r2&&up[r1-1][i]-1<=r2&&binary_search(col[i].begin(),col[i].end(),t);
				vector<ll> p(m+1,0);
				for(ll i = 0;i<m;i++) p[i+1] = p[i]+ok[i];
				for(auto &o : cur){
					if (p[o.second]-p[o.first+1]==(o.second-o.first-1)) ans++;
				}
			}
		}
	}
	return ans;
}

Compilation message

rect.cpp: In function 'long long int count_rectangles(std::vector<std::vector<int> >)':
rect.cpp:84:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'int'} and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   84 |    if (req==o.size()) ans++;
      |        ~~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 0 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 1 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 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 0 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 1 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 600 KB Output is correct
23 Correct 5 ms 604 KB Output is correct
24 Correct 4 ms 748 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 2 ms 604 KB Output is correct
27 Correct 2 ms 604 KB Output is correct
28 Correct 3 ms 604 KB Output is correct
29 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 0 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 600 KB Output is correct
18 Correct 5 ms 604 KB Output is correct
19 Correct 4 ms 748 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 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 59 ms 1884 KB Output is correct
31 Correct 58 ms 1880 KB Output is correct
32 Correct 58 ms 2136 KB Output is correct
33 Correct 9 ms 1368 KB Output is correct
34 Correct 19 ms 1884 KB Output is correct
35 Correct 15 ms 2140 KB Output is correct
36 Correct 17 ms 2140 KB Output is correct
37 Correct 16 ms 2140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 0 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 600 KB Output is correct
18 Correct 5 ms 604 KB Output is correct
19 Correct 4 ms 748 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 59 ms 1884 KB Output is correct
26 Correct 58 ms 1880 KB Output is correct
27 Correct 58 ms 2136 KB Output is correct
28 Correct 9 ms 1368 KB Output is correct
29 Correct 19 ms 1884 KB Output is correct
30 Correct 15 ms 2140 KB Output is correct
31 Correct 17 ms 2140 KB Output is correct
32 Correct 16 ms 2140 KB Output is correct
33 Correct 1 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 247 ms 15992 KB Output is correct
39 Correct 249 ms 16100 KB Output is correct
40 Correct 225 ms 16032 KB Output is correct
41 Correct 242 ms 16088 KB Output is correct
42 Correct 2598 ms 21752 KB Output is correct
43 Correct 2671 ms 21588 KB Output is correct
44 Correct 2611 ms 21588 KB Output is correct
45 Correct 2358 ms 20820 KB Output is correct
46 Correct 43 ms 11856 KB Output is correct
47 Correct 232 ms 14740 KB Output is correct
48 Correct 357 ms 21204 KB Output is correct
49 Correct 351 ms 22868 KB Output is correct
50 Correct 166 ms 11860 KB Output is correct
51 Correct 151 ms 11600 KB Output is correct
52 Correct 510 ms 21584 KB Output is correct
53 Correct 418 ms 22864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 860 KB Output is correct
2 Correct 1 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 1 ms 780 KB Output is correct
8 Correct 1 ms 604 KB Output is correct
9 Correct 1 ms 604 KB Output is correct
10 Correct 0 ms 444 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 1 ms 344 KB Output is correct
7 Correct 288 ms 69972 KB Output is correct
8 Correct 651 ms 148140 KB Output is correct
9 Correct 645 ms 148760 KB Output is correct
10 Correct 616 ms 148808 KB Output is correct
11 Correct 153 ms 54028 KB Output is correct
12 Correct 313 ms 101832 KB Output is correct
13 Correct 319 ms 110844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 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 0 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 600 KB Output is correct
18 Correct 5 ms 604 KB Output is correct
19 Correct 4 ms 748 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 2 ms 604 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 59 ms 1884 KB Output is correct
26 Correct 58 ms 1880 KB Output is correct
27 Correct 58 ms 2136 KB Output is correct
28 Correct 9 ms 1368 KB Output is correct
29 Correct 19 ms 1884 KB Output is correct
30 Correct 15 ms 2140 KB Output is correct
31 Correct 17 ms 2140 KB Output is correct
32 Correct 16 ms 2140 KB Output is correct
33 Correct 247 ms 15992 KB Output is correct
34 Correct 249 ms 16100 KB Output is correct
35 Correct 225 ms 16032 KB Output is correct
36 Correct 242 ms 16088 KB Output is correct
37 Correct 2598 ms 21752 KB Output is correct
38 Correct 2671 ms 21588 KB Output is correct
39 Correct 2611 ms 21588 KB Output is correct
40 Correct 2358 ms 20820 KB Output is correct
41 Correct 43 ms 11856 KB Output is correct
42 Correct 232 ms 14740 KB Output is correct
43 Correct 357 ms 21204 KB Output is correct
44 Correct 351 ms 22868 KB Output is correct
45 Correct 166 ms 11860 KB Output is correct
46 Correct 151 ms 11600 KB Output is correct
47 Correct 510 ms 21584 KB Output is correct
48 Correct 418 ms 22864 KB Output is correct
49 Correct 1 ms 860 KB Output is correct
50 Correct 1 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 1 ms 604 KB Output is correct
54 Correct 1 ms 604 KB Output is correct
55 Correct 1 ms 780 KB Output is correct
56 Correct 1 ms 604 KB Output is correct
57 Correct 1 ms 604 KB Output is correct
58 Correct 0 ms 444 KB Output is correct
59 Correct 0 ms 348 KB Output is correct
60 Correct 1 ms 344 KB Output is correct
61 Correct 288 ms 69972 KB Output is correct
62 Correct 651 ms 148140 KB Output is correct
63 Correct 645 ms 148760 KB Output is correct
64 Correct 616 ms 148808 KB Output is correct
65 Correct 153 ms 54028 KB Output is correct
66 Correct 313 ms 101832 KB Output is correct
67 Correct 319 ms 110844 KB Output is correct
68 Correct 1 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 Execution timed out 5025 ms 206144 KB Time limit exceeded
74 Halted 0 ms 0 KB -