【线段树】 POJ 3277 City Horizon 裸扫描线

将r-1是为了防止在update 时出现 l==r 的情况 X[r]-X[l]=0  使得单点的更新无法进行

扫描线讲解

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 1000006;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 11521204;
const int mod=1000000007;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 84444;
int cnt[maxn << 2];
int sum[maxn << 2];
int X[maxn];
struct Seg
{
    int h , l , r;
    int s;
    Seg() {}
    Seg(int a,int b,int c,int d) : l(a) , r(b) , h(c) , s(d) {}
    bool operator < (const Seg &cmp) const
    {
        return h < cmp.h;
    }
} ss[maxn];
void PushUp(int rt,int l,int r)
{
    if (cnt[rt]) sum[rt] = X[r+1] - X[l];
    else if (l == r) sum[rt] = 0;
    else sum[rt] = sum[rt<<1] + sum[rt<<1|1];
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if (L <= l && r <= R)
    {
        cnt[rt] += c;
        PushUp(rt , l , r);
        return ;
    }
    int m = (l + r) >> 1;
    if (L <= m) update(L , R , c , lson);
    if (m < R) update(L , R , c , rson);
    PushUp(rt , l , r);
}
int Bin(int key,int n,int X[])
{
    int l = 0 , r = n - 1;
    while (l <= r)
    {
        int m = (l + r) >> 1;
        if (X[m] == key) return m;
        if (X[m] < key) l = m + 1;
        else r = m - 1;
    }
    return -1;
}
int main()
{
    int n ;
   // IN;
    while (scanf("%d",&n)!=EOF)
    {
        int m = 0;
        while (n --)
        {
            int x,y,z,o=0;
            scanf("%d%d%d",&x,&y,&z);
            X[m] = x;
            ss[m++] = Seg(x , y , o , 1);
            X[m] = y;
            ss[m++] = Seg(x , y , z , -1);
        }
        sort(X , X + m);
        sort(ss , ss + m);
        int k = 1;
        for (int i = 1 ; i < m ; i ++)
        {
            if (X[i] != X[i-1]) X[k++] = X[i];
        }
        memset(cnt , 0 , sizeof(cnt));
        memset(sum , 0 , sizeof(sum));
        LL ret = 0;
        for (int i = 0 ; i < m - 1 ; i ++)
        {
            int l = Bin(ss[i].l , k , X);
            int r = Bin(ss[i].r , k , X) - 1;
            if (l <= r) update(l , r , ss[i].s , 0 , k - 1, 1);
            ret +=(LL)sum[1] * (ss[i+1].h - ss[i].h);
        }
        printf("%I64d\n", ret);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值