P7181 [BalticOI 2004] Rectangles (Day2) 题解
由于每个矩形都在第一象限内,所以我们从原点发射出若干条直线,不难发现想要有交点,必须保证这条直线在矩形左上角与右下角之间。
我们假设一个矩形的左下角坐标是
将原点与左上角和右下角分别相连,那么这两条直线的解析式分别是:
我们试图求出这两条直线分别与整个边框
问题是这两条直线到底是和
其实可以假设它和
然后我们将边框
怎么做区间覆盖?不难想到差分。坐标太大怎么办?离散化即可。
时间复杂度
注意每个区间的上下取整问题。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e4 + 5;
int n, m, a, b, c, d, X, Y, tot, ans;
ll p, q, pos;
ll l[maxn], r[maxn], tmp[maxn<<1], sum[maxn<<1];
int main() {
scanf("%d %d %d", &X, &Y, &n);
for(int i = 1; i <= n; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
p = ceil((double)Y*a/d), q = floor((double)X*d/a);
l[i] = (p <= X) ? p : X + Y - q;
p = floor((double)Y*c/b), q = ceil((double)X*b/c);
r[i] = (p <= X) ? p : X + Y - q;
tmp[++tot] = l[i]; tmp[++tot] = r[i];
}
sort(tmp+1, tmp+1+tot);
m = unique(tmp+1, tmp+1+tot) - tmp;
for(int i = 1; i <= n; i++) {
l[i] = lower_bound(tmp+1, tmp+1+m, l[i]) - tmp;
r[i] = lower_bound(tmp+1, tmp+1+m, r[i]) - tmp;
sum[l[i]]++; sum[r[i]+1]--;
}
for(int i = 1; i <= m; i++) {
sum[i] += sum[i-1];
if(sum[i] > ans) {
ans = sum[i];
pos = tmp[i];
}
}
printf("%d ", ans);
if(pos <= X) printf("%lld %d\n", pos, Y);
else printf("%d %lld\n", X, X+Y-pos);
return 0;
}