AIM Tech Round 5 C. Rectangles

地址:https://codeforces.com/contest/1028/problem/C

题目:

给定n个矩形,至少有(n-1)个矩形有公共点,一个点只有在矩形内或者边上才算属于这个矩形。

求一个至少被(n-1)个矩形包含的点,并输出坐标。

输入规模:

n - 10^5

题解:

这道题和前几天比赛的另一道题目非常像,但也有一点点不同。

这道题和上一道题都可以使用前缀+后缀和来完成,区别在于后者需要对区间进行排序,而这道题不需要排序。

假设有n个矩形,要求一个点至少属于(n-1)个矩形,那么我们可以求出,除了某个外,剩余所有矩形相交得到的结果。

如果结果是一个有效矩形(哪怕只是一个点)那么可以选择该属于该矩形的任意一点作为结果。

使用前缀+后缀和的思想,假设所有矩形为 s[ 1 ... n]

设  pre[i] = s[1] | s[2] | ...  | s[i]   , suf[i] = s[i] | s[i+1] | ... | s[n];

其中   1<=i <= n;

a | b 指矩形a 和 b相交,两个矩形如何相交,画个图想一下就好。

很容易将其转为递推式

pre[i] = pre[i-1] | s[i];

suf[i] = suf[i+1] | s[i];

那么 除去第i个矩形,剩余的所有矩形相交的结果为: res = pre[i-1] | suf[i+1];

时间复杂度O(n)

 

代码:


#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
#include <cmath>
#include <string>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <cctype>
#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <numeric>
#include <bitset>
using namespace std;

#define FASTIO ios::sync_with_stdio(false), cin.tie(nullptr);
#define rep(i,a,b) for(int i=a; i < b; ++i)
#define repn(i, n) for(int i=1; i <=n; ++i)
#define pre(i,a,b) for(int i=a; i >=b; --i)
#define pren(i, a) for(int i=a; i > 0; --i)
#define all(x) (x).begin(),(x).end()
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int INF = 0x3f3f3f3f;
const ll  INF_LL = (ll)1e18;

using namespace std;

struct rect
{
	int x1, y1, x2, y2;
	rect operator|(rect& oth) {
		rect ans;
		ans.x1 = max(x1, oth.x1);
		ans.x2 = min(x2, oth.x2);
		ans.y1 = max(y1, oth.y1);
		ans.y2 = min(y2, oth.y2);
		return ans;
	}
	operator bool() {
		return x1 <= x2 && y1 <= y2;
	}
}rec[150000], pre[150000], suf[150000];

int main()
{
	FASTIO;
	int n;
	cin >> n;
	repn(i, n) 
		cin >> rec[i].x1 >> rec[i].y1 >> rec[i].x2 >> rec[i].y2;
	
	pre[0] = suf[n + 1] = { -(int)1e9, -(int)1e9, (int)1e9 ,(int)1e9 };

	repn(i, n)
		pre[i] = pre[i - 1] | rec[i];
	pren(i, n)
		suf[i] = suf[i + 1] | rec[i];

	repn(i, n) {
		rect now = pre[i - 1] | suf[i + 1];
		if (now) {
			cout << now.x1 << " " <<  now.y1 << endl;
			break;
		}
	}

	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值