poj 2528 Mayor's posters【离散化+线段树】

题目:poj 2528 Mayor‘s posters

题意:给一个长度非常长的墙上贴长度为ai的海报,由于有的会覆盖掉,求最后能看见的海报个数。

分析:题目和POJ2777 一模一样,方法也一样,只不过这个要离散化,其次要数组开大一点。至少2倍。

离散化的时候用了C++的 pair 类,还是比较好用的。

代码:

#include <iostream>
#include <algorithm>
#include <utility>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = 25000;
pair<int,int> p[2*N];
pair<int,int> v[N];
int n;
int comp(pair<int,int> a,pair<int,int> b)
{
    if(a.first!=b.first)
        return a.first<b.first;
}
int cmp(pair<int,int> a,pair<int,int> b)
{
    if(a.second!=b.second)
        return a.second<b.second;
    if(a.first!=b.first)
        return a.first<b.first;
}
int Discrete()  //离散化
{
    for(int i=0; i<n; i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        p[i*2]=make_pair(x,i);
        p[i*2+1]=make_pair(y,i);
    }
    sort(p,p+2*n,comp);
    int cnt=2,tmp=p[0].first;
    p[0].first=1;
    for(int i=1; i<n*2; i++)
    {
        if(p[i].first==tmp)
        {
            tmp=p[i].first;
            p[i].first=(cnt-1);
        }
        else
        {
            tmp=p[i].first;
            p[i].first=cnt++;
        }
    }
    sort(p,p+2*n,cmp);
    for(int i=0; i<2*n; i+=2)
        v[i/2]=make_pair(p[i].first,p[i+1].first);
    cnt--;
    return cnt;
}
struct Node
{
    int l,r;
    long long num;
};
Node tree[4*N];
int vis[N*2];
void build(int l,int r,int o)
{
    tree[o].l=l;
    tree[o].r=r;
    tree[o].num=1;
    if(l==r)
        return ;
    int mid=(l+r)/2;
    build(l,mid,o*2);
    build(mid+1,r,o*2+1);
}
void update(int l,int r,int t,int o)
{
    if(tree[o].l==l && tree[o].r==r)
    {
        tree[o].num=t;
        return;
    }
    if(tree[o].num==t) return;
    if(tree[o].num!=-1)
    {
        tree[2*o].num=tree[o].num;
        tree[2*o+1].num=tree[o].num;
        tree[o].num=-1;
    }
    int mid=(tree[o].l+tree[o].r)>>1;
    if(r<=mid)
        update(l,r,t,o+o);
    else if(l>mid)
        update(l,r,t,o+o+1);
    else
    {
        update(l,mid,t,o*2);
        update(mid+1,r,t,o*2+1);
    }
}
void query(int l,int r,int o)
{
    if(tree[o].num!=-1)
    {
        vis[tree[o].num]=1;
        return ;
    }
    int mid=(tree[o].l+tree[o].r)>>1;
    if(r<=mid)
        query(l,r,o+o);
    else if(l>mid)
        query(l,r,o+o+1);
    else
    {
        query(l,mid,o*2);
        query(mid+1,r,o*2+1);
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int cnt=Discrete();
        build(1,cnt,1);
        for(int i=0;i<n;i++)
        {
            update(v[i].first,v[i].second,i,1);
        }
        memset(vis,0,sizeof(vis));
        query(1,cnt,1);
        int ans=0;
        for(int i=0;i<=cnt;i++)
            if(vis[i]){
                //printf("--%d ",i);
                ans++;
            }
        printf("%d\n",ans);
        memset(p,0,sizeof(p));
        memset(v,0,sizeof(v));
    }
    return 0;
}

poj 2528 Mayor's posters【离散化+线段树】,布布扣,bubuko.com

poj 2528 Mayor's posters【离散化+线段树】

时间: 2024-08-10 23:30:54

poj 2528 Mayor's posters【离散化+线段树】的相关文章

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(离散化 线段树)

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到 先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了 但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果

POJ - 2528 - Mayor&#39;s posters 【线段树+离散化+补点】

http://poj.org/problem?id=2528 #include <cstdio> #include <iostream> #include <set> #include <cstring> #include <string> #define left rt<<1 #define right rt<<1|1 using namespace std; const int MAXN = 32768 + 5; in

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

题目链接:Mayor's posters 题意:按顺序往墙上贴海报,可以重叠,问最后可以看到多少海报.(被覆盖的海报是看不到的) 注意: 因为数据比较大,所以不离散化,肯定爆内存. 还有就是,不能只是单纯的离散化,还要处理好点的边界 举个例子 4 2  10. 2  8 3  6 6  8 8  10 离散化后 2 3 6 8 10 1 2 3 4 5 覆盖掉了 1 5   和  1 4俩段 只剩下 2  3  .3  4. 4  5 答案是 3 但是正确答案是4 所以,离散化处理时要处理边界,

【POJ】2528 Mayor&#39;s posters ——离散化+线段树

Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K 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

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

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 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(线段树+离散化)

这道题目让我又重新认识了一下离散化: 首先总结一下离散化的特点: 1)有时区间的端点并不是整数,或者区间太大导致建树内存开销过大而MLE,那么就需要进行离散化后再建树. 2)意思是将区间范围很大的数据集映射到较小的数据集,这样建树更加有效,或者说我们只取需要的值来用. 这个意思说到底就是进行映射,把原来很大的映射到一个较小的空间中去. 题意: 给定一些海报,它们可能相互重叠,告诉你每个海报的宽度(它们的高度都是一样的)和先后的叠放次序,问没有被完全盖住的海报有多少张? 这里我们注意到了数据的范围

poj 2528 Mayor&#39;s posters 【线段树 + 离散化】

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 50643   Accepted: 14675 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 (线段树+离散化)

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(线段树 离散化 区间更新 贴海报)

     这个题目本来对大神来说可能是水题, 对我就不行了,昨晚非折腾到下半夜一点 搞定, 并且可以总结出 ,只有把问题想清楚,或着看人家解题报告自己把问题和代码思路 搞清楚,才能谈的上调bug,否则根本就不知道错在哪儿.说说这个题目的理解,他是如何转化为线段树问题的呢?我们知道线段树有一个区间更新的东西,每张海报的宽度不就是一个区间么?那么我们可以用一棵树中的部分结点 来表示整张海报的可视部分,也就是说每个结点只允许表示一张完整的或着不完整的海报(好几个结点才可以表示成完整的一张海报),那么如