POJ2528(离散化+线段树区间更新)

#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=10005;
struct Post{
    int l,r;
}posters[MAXN];
int x[MAXN*2];
int hash[10000005];
struct node{
    int l,r;
    bool beCovered;
};
int cnt;
node a[MAXN*8];//*4 会RE ?? 

void build(int rt,int l,int r)
{
    a[rt].l=l;
    a[rt].r=r;
    if(l==r)
    {
        a[rt].beCovered=false;
        return ;
    }

    int mid=(l+r)>>1;
    build(rt<<1,l,mid);
    build((rt<<1)|1,mid+1,r);
    a[rt].beCovered=(a[rt<<1].beCovered&&a[(rt<<1)|1].beCovered);
}

void update(int rt,int l,int r)
{
    if(a[rt].l==l&&a[rt].r==r)
    {
        a[rt].beCovered=true;
        return ;
    }

    if(a[rt].beCovered)
    {
        a[rt<<1].beCovered=true;
        a[(rt<<1)|1].beCovered=true;
    }

    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    update(rt<<1,l,r);
    else if(mid<l)    update((rt<<1)|1,l,r);
    else
    {
        update(rt<<1,l,mid);
        update((rt<<1)|1,mid+1,r);
    }
    a[rt].beCovered=(a[rt<<1].beCovered&&a[(rt<<1)|1].beCovered);
}

bool query(int rt,int l,int r)
{
    if(a[rt].l==l&&a[rt].r==r)
    {
        return a[rt].beCovered;
    }

    if(a[rt].beCovered)
    {
        a[rt<<1].beCovered=true;
        a[(rt<<1)|1].beCovered=true;
    }

    int mid=(a[rt].l+a[rt].r)>>1;
    if(r<=mid)    return query(rt<<1,l,r);
    else if(mid<l)    return query((rt<<1)|1,l,r);
    else{
        return (query(rt<<1,l,mid)&&query((rt<<1)|1,mid+1,r));
    }

}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cnt=0;
        int n;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&posters[i].l,&posters[i].r);
            x[cnt++]=posters[i].l;
            x[cnt++]=posters[i].r;
        }
        sort(x,x+cnt);
        cnt=unique(x,x+cnt)-x;
        for(int i=0;i<cnt;i++)
        {
            hash[x[i]]=i+1;
        }
        //区间离散化完毕
        build(1,1,cnt);
        int res=0;
        for(int i=n-1;i>=0;i--)
        {
            if(!query(1,hash[posters[i].l],hash[posters[i].r]))    res++;
            update(1,hash[posters[i].l],hash[posters[i].r]);
        }
        printf("%d\n",res);
    }

    return 0;
}
时间: 2024-09-27 23:45:18

POJ2528(离散化+线段树区间更新)的相关文章

POJ 2528 Mayor&#39;s posters (离散化+线段树区间更新)

Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for

poj-----(2528)Mayor&#39;s posters(线段树区间更新及区间统计+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

POJ 2528 Mayor&#39;s posters (线段树区间更新+离散化)

题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值.由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案. 写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目. 1 #include <iostream> 2 #include <

线段树区间更新,区间统计+离散化 POJ 2528 Mayor&#39;s posters

题意:有一个很长的板子(10000000长),在上面贴n(n<=10000)张海报,问最后从外面能看到几张不同的海报. 因为板子有10000000长,直接建树肯定会爆,所以需要离散化处理,对于每张海报,有两个端点值,最后能看到几张海报跟他们的端点值的相对大小有关,跟绝对大小无关,所以就把所有海报的端点离散化处理,总共2n个端点,排序去重,对应p(p<=2n)个点.然后建树,因为p不超过20000,所以这样就可以接受了.区间更新时,因为我们只关心最外面海报的颜色有多少种,所以向下传递节点信息的时

线段树 区间更新

poj3468 A Simple Problem with Integers ( m - ( m >> 1 ) )这里跪了几发.. - 的优先级大于 >> 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 #include<cstdio> 5 #include<string> 6 #include<queue> 7 #include

POJ - 2528 Mayor&#39;s posters (离散化+线段树区间修改)

https://cn.vjudge.net/problem/POJ-2528 题意 给定一些海报,可能相互重叠,告诉你每个海报的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张? 分析 海报最多10000张,但是墙有10000000块瓷砖长,海报不会落在瓷砖中间. 如果直接建树,就算不TLE,也会MLE.即单位区间长度太多. 其实10000张海报,有20000个点,最多有19999个区间.对各个区间编号,就是离散化.然后建树. 可以直接进行区间修改,最后再统计. 这里采用比较巧妙的

POJ - 2528Mayor&#39;s posters (离散化+线段树区间覆盖)

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the

HDU 1689 Just a Hook 线段树区间更新求和

点击打开链接 Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18894    Accepted Submission(s): 9483 Problem Description In the game of DotA, Pudge's meat hook is actually the most horrible

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include