Mayor's posters POJ - 2528 线段树区间覆盖

//线段树区间覆盖
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=100010;
int flag;
struct node{
    int l,r;
    //vis 是这块区域是否完全被覆盖
    bool vis;
}tr[N<<2];
struct point
{
    int id;
    int x;
}post[N<<2];
int cmp1(point a,point b)
{
    return a.x<b.x;
}
int cmp2(point a,point b)
{
    if(a.id==b.id)
        return a.x<b.x;
    return a.id>b.id;
}
void pushup(int u)
{
    tr[u].vis=tr[u<<1].vis&&tr[u<<1|1].vis;
}
void build(int u,int l,int r)
{
    tr[u]={l,r,0};
    if(l==r)
        return ;
    int mid=l+r>>1;
    build(u<<1,l,mid);
    build(u<<1|1,mid+1,r);
}
void query(int u,int l,int r)
{
    if(tr[u].vis)
        return ;
    if(tr[u].l==l&&tr[u].r==r)
    {
        tr[u].vis=1;
        flag=1;
        return;
    }
    int mid=tr[u].l+tr[u].r>>1;
    if(r<=mid)
        query(u<<1,l,r);
    else if(l>mid)
        query(u<<1|1,l,r);
    else
    {
        query(u<<1,l,mid);
        query(u<<1|1,mid+1,r);
    }
    pushup(u);
}
int main()
{
    int t,n;
    cin>>t;
    while(t--)
    {
        cin>>n;
        //离散化之前的数据
        for(int i=0;i<2*n;i+=2)
        {
            cin>>post[i].x>>post[i+1].x;
            post[i].id=post[i+1].id=i;
        }
        //按照离散化之前的数据排序
        sort(post,post+2*n,cmp1);
        //离散化
        int tot=0,pre=0;
        for(int i=0;i<2*n;i++)
        {
            //如果和上一个一样
            //那么排名也就一样
            if(post[i].x==pre)
            {
                post[i].x=tot;
            }
            //如果不一样,记录当前值
            //排名++
            else
            {
                pre=post[i].x;
                post[i].x=++tot;
            }
        }
        build(1,1,2*n);
        //排序,从后往前贴
        //id大的在前
        sort(post,post+2*n,cmp2);
        int ans=0;
        for(int i=0;i<2*n;i+=2)
        {
            int l=post[i].x;
            int r=post[i+1].x;
            flag=0;
            query(1,l,r);
            if(flag)
                ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

Mayor's posters POJ - 2528 线段树区间覆盖

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/12293741.html

时间: 2024-08-27 23:51:29

Mayor's posters POJ - 2528 线段树区间覆盖的相关文章

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

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

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

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

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

因为将每个单位都作为一个最小单元的话会爆内存的 所以,将海报的每个端点进行排序,将这些端点最为最小的区间. 毕竟是刚刚接触线段树,理解起来还有些吃力,还是那句话,题做多了慢慢就好了. 萌萌的AC代码君贴上. 1 //#define LOCAL 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 7 int n; 8 struct CPost 9

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

强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 强烈不推荐在POJ做这道题!!! 推荐去UVA 10587 或 SCU 2249 POJ的数据比较水且可能有错,一些本来错误的数据但可以水过,以及在UVA与SCU同样题目都能AC的程序在POJ莫名WA了. 建议写完程序后跑下这组数据: 1 3 1 10 1 3 6 10 好多题解的答案是2,但答案明显是3,这是因为每个数字其实表示的是一个单位长度,并非一个点 , 这就会导致像这样的区间: 1-10 1-4 5-10 1-10 1

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

题目大意:往一面墙上贴与墙等高的海报,n次贴完后,求可以看见的海报总数(看见一部分也算) 思路:明显的区间维护,用线段树,不过裸的线段树超时超空间,可以把坐标离散,得到不超过200000个有效点,每个点都表示一个小区间(a[i]~a[i+1]这一段),然后就可以轻松地解决了.不过题目有个坑,给定的右坐标其实不是实际坐标,还要+1,区间总数等于有效点数 -1,更新的时候把查找到的右端点-1再代入,具体见代码: 1 /*****************************************

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

题目:poj 2528 Mayor's posters 题意:给一个长度非常长的墙上贴长度为ai的海报,由于有的会覆盖掉,求最后能看见的海报个数. 分析:题目和POJ2777 一模一样,方法也一样,只不过这个要离散化,其次要数组开大一点.至少2倍. 离散化的时候用了C++的 pair 类,还是比较好用的. 代码: #include <iostream> #include <algorithm> #include <utility> #include <cstrin