线段树 csu1082 憧憬一下集训

最简单的扫描线思想。。

如果把空调温度当作横坐标,音响声音大小当作纵坐标,那么题意可以转换成,给你许多个矩形,问一个点最多被覆盖多少次~


那么按照纵坐标大小排序,从下向上扫描,用线段树维护点的覆盖次数


有个地方要注意

排序的时候,因为不仅要按照坐标大小排序,因为s,t可能会相等,那么矩形会退化成一条线。

那么我们一般的面积并的时候,是考虑两根扫描线之间的距离,而这题不仅要考虑距离,还要考虑扫描线的更新顺序

我们应该让进矩形的扫描线总是在出矩形之前

这样对于一个纵坐标,会让x一直增大,直到x不能再增大时,再考虑出去的扫描线,使x减小

这样才能d得到最大的x

所以在排序的时候,不仅要考虑扫描线的位置,还要考虑扫描线的类型

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 2e4 + 5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define root 0,10000,1

int MAX[MX << 2], col[MX << 2];

struct Que {
    int L, R, top, d;
    bool operator<(const Que &b)const {
        if(top == b.top) return d > b.d;
        return top < b.top;
    }
    Que(int _top = 0, int _L = 0, int _R = 0, int _d = 0) {
        L = _L; R = _R; top = _top; d = _d;
    }
} Q[MX];

void push_up(int rt) {
    MAX[rt] = max(MAX[rt << 1], MAX[rt << 1 | 1]);
}

void push_down(int rt) {
    if(col[rt]) {
        col[rt << 1] += col[rt];
        col[rt << 1 | 1] += col[rt];
        MAX[rt << 1] += col[rt];
        MAX[rt << 1 | 1] += col[rt];
        col[rt] = 0;
    }
}

void update(int L, int R, int d, int l, int r, int rt) {
    if(L <= l && r <= R) {
        MAX[rt] += d;
        col[rt] += d;
        return;
    }

    int m = (l + r) >> 1;
    push_down(rt);
    if(L <= m) update(L, R, d, lson);
    if(R > m) update(L, R, d, rson);
    push_up(rt);
}

int main() {
    int n;
    //freopen("input.txt", "r", stdin);
    while(~scanf("%d", &n)) {
        memset(MAX, 0, sizeof(MAX));
        memset(col, 0, sizeof(col));

        for(int i = 1; i <= n; i++) {
            int x1, x2, y1, y2;
            scanf("%d%d%d%d", &x1, &x2, &y1, &y2);
            Q[i] = Que(y1, x1, x2, 1);
            Q[i + n] = Que(y2, x1, x2, -1);
        }
        sort(Q + 1, Q + 1 + 2 * n);

        int ans = 0;
        for(int i = 1; i <= 2 * n; i++) {
            update(Q[i].L, Q[i].R, Q[i].d, root);
            ans = max(ans, MAX[1]);
        }
        printf("%d\n", ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值