CSU 1082: 憧憬一下集训 (线段树 扫描线)

来源:互联网 发布:e5 v3cpu 淘宝店推荐 编辑:程序博客网 时间:2024/05/23 01:21
连接 :


http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1082

题目可以将每个人的两种适应度转化为一个在坐标系里的矩形。假设音量为x轴,空调为y轴。

那么题目要求的可以转化为 在所有的矩形里选择一个点 这个点可以覆盖最多的矩形。

题目可以进一步转化 需要用到扫描线,只考虑每个矩形的上边和下边,每个边都构成一个扫描线(沿着y轴方向),对于每个扫描线只要统计在这个扫描线里被覆盖最多的点。答案就是2*n个扫描线中 的最大值。


#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <sstream>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <set>#include <map>#define lson o<<1, l, m#define rson o<<1|1, m+1, r#define PII pair<int, int>#define ALL(x) x.begin(),x.end()#define mem(a) memset(a,0,sizeof(a))typedef long long ll;const double pi = acos(-1.0);const int MAX = 0x3f3f3f3f;const ll mod = 1000000007ll;const int N = 10005;using namespace std;int n;struct C {int x1, x2, y, v;} a[N*2];C con(int x1, int x2, int y, int v) {C tmp; tmp.v = v, tmp.x1 = x1, tmp.x2 = x2, tmp.y = y;return tmp;}bool cmp(C e, C r) {if(e.y != r.y) return e.y < r.y;return e.v > r.v;}int add[N<<2], sum[N<<2], mx[N<<2], A, B, C;void down(int o) {if(add[o] != 0) {add[o<<1] += add[o];add[o<<1|1] += add[o];mx[o<<1] += add[o];mx[o<<1|1] += add[o];mx[o] += add[o];add[o] = 0;}}void update(int o, int l, int r) {if(A <= l && r <= B) {add[o] += C;mx[o] += C;} else {int m = (l+r)/2;down(o);if(A <= m) update(lson);if(m < B ) update(rson);mx[o] = max(mx[o<<1], mx[o<<1|1]);}}int main() {//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);while(cin >> n) {int m = 0, TOT = 0, x1, x2, y1, y2;for(int i = 0; i < n; i++) {scanf("%d%d%d%d", &x1, &x2, &y1, &y2);x1++, x2++, y1++, y2++;a[m++] = con(x1, x2, y1, 1);a[m++] = con(x1, x2, y2, -1);TOT = max(TOT, x2);}sort(a, a+m, cmp);mem(add);mem(mx);int ans = 0;for(int i = 0; i < m-1; i++) {A = a[i].x1;B = a[i].x2;C = a[i].v;update(1, 1, TOT);ans = max(ans, mx[1]);}printf("%d\n", ans);}return 0;}

0 0
原创粉丝点击