【poj2528】Mayor's posters

这是poj题目分类上线段树的第一题……
普通的区间赋值操作,可是我调了一天……
简直要哭了QAQ


题目大意为一个市长给一面墙上贴海报,后贴的海报会覆盖之前贴的海报,按顺序给你贴的海报的左右端点,最后求有多少张海报会露在外面


然后这是一道区间赋值的题目,但是因为l,r太大所以需要离散化一下
那么离散化就来了问题
看了这位神犇的题解明白了特殊的离散化姿势
神犇举的例子是

3
1 10
1 4
6 10

总的意思来说的话,就是说 单点没有方向 嗯嗯
类似于 SDOI2008 校门外的区间 为什么需要向中间插入.5的意思


然后比较麻烦的事情就是判断什么时候循环到n什么时候循环到tot的问题了
注意些还是问题不大

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define L(x) (x << 1)
#define R(x) (x << 1 | 1)
#define sz(x) (tree[x].r - tree[x].l + 1)
#define KILL puts("haha");
using namespace std;
const int MAXN = 100000 + 5;
int lsh[MAXN << 1];
int tot;
int ler[MAXN],rer[MAXN];
struct tree_dot
{
    int l,r;
    int fz;
}tree[MAXN << 2];
void spread(int p)
{
    if(!tree[p].fz)return;
    tree[L(p)].fz = tree[p].fz;
    tree[R(p)].fz = tree[p].fz;
    tree[p].fz = 0;
    return;
}
void update(int p)
{
    return;
}
void build(int p,int l,int r)
{
    tree[p].l = l;
    tree[p].r = r;
    tree[p].fz = 0;
    if(l == r)
        return;
    int mid = (l + r) / 2;
    build(L(p),l,mid);
    build(R(p),mid + 1,r);
    update(p);
    return;
}
void change(int p,int l,int r,int v)
{
    if(l <= tree[p].l && tree[p].r <= r)
    {
        tree[p].fz = v;
        return;
    }
    spread(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if(l <= mid)change(L(p),l,r,v);
    if(mid <  r)change(R(p),l,r,v);
    return;
}
int ask(int p,int x)
{
    if(x == tree[p].l && tree[p].r == x)
    {
        return tree[p].fz;
    }
    spread(p);
    int mid = (tree[p].l + tree[p].r) >> 1;
    if(x <= mid)return ask(L(p),x);
    if(mid <  x)return ask(R(p),x);
}
int where(int x)
{
    return lower_bound(lsh + 1,lsh + tot + 1,x) - lsh;
}
int T;
int n,l,r;
bool used[MAXN];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        memset(used,0,sizeof(used));
        tot = 0;
        for(int i = 1;i <= n;i ++)
        {
            scanf("%d %d",&ler[i],&rer[i]);
            lsh[++tot] = ler[i];
            lsh[++tot] = rer[i];
        }
        sort(lsh + 1,lsh + tot + 1);
        tot = unique(lsh + 1,lsh + tot + 1) - (lsh + 1);
        for(int i = tot;i > 0;i --)
            lsh[i * 2 - 1] = lsh[i];
        tot = tot * 2 - 1;
        for(int i = 2;i <= tot;i += 2)
            lsh[i] = lsh[i + 1] - 1;
        tot = unique(lsh + 1,lsh + tot + 1) - (lsh + 1);
        build(1,1,tot);
        for(int i = 1;i <= n;i ++)
            change(1,where(ler[i]),where(rer[i]),i);
        int ans = 0;
        for(int i = 1;i <= tot;i ++)
        {
            int k = ask(1,i);
            if(used[k] || !k)
                continue;
            ans ++;
            used[k] = true;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值