题意:贴海报
有一面很长的墙,大概有10000000 这么长,现有有一些海报会贴在墙上,当然贴海报的顺序是有先后的,问你当最后一张海报也贴上的时候能不能求出来在这面墙上能看到多少张不同的海报?
分析:因为后面贴的海报会把前面贴的覆盖掉,不太容易求出来,但是如果从最后一张倒着贴,只要判断墙上这段区间有没有被完全覆盖就可以了,因为墙比较长,所以需要离散化一下。
************************************************************************
注意:在向上跟新的时候返回值要在更新的下面要不直接返回了怎么更新??
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 40005; int Hash[maxn];//记录离散化后的数据
struct Post{int l, r;}p[maxn];//记录海报
struct Tree
{
    int L, R;
    bool isCover;//记录这段区间是否被覆盖
    int Mid(){return (L+R)/2;}
}tree[maxn*4]; void Up(int root)//向上更新,如果左右子树都被覆盖,那么他也会被覆盖
{
    if(tree[root].L != tree[root].R)
    if(tree[root<<1].isCover && tree[root<<1|1].isCover)
        tree[root].isCover = true;
}
void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    tree[root].isCover = false;     if(L == R)return ;     Build(root<<1, L, tree[root].Mid());
    Build(root<<1|1, tree[root].Mid()+1, R);
}
//如果区间能内还有位置返回真,否则返回假
bool Insert(int root, int L, int R)
{
    if(tree[root].isCover)return false;     if(tree[root].L == L && tree[root].R == R)
    {
        tree[root].isCover = true;
        return true;
    }
    
    bool ans;     if(R <= tree[root].Mid())
        ans =  Insert(root<<1, L, R);
    else if(L > tree[root].Mid())
        ans =  Insert(root<<1|1, L, R);
    else
    {
        bool Lson = Insert(root<<1, L, tree[root].Mid());
        bool Rson = Insert(root<<1|1, tree[root].Mid()+1, R);         ans = Lson | Rson;
    }     Up(root);     return ans;
} int main()
{
    int T;     scanf("%d", &T);     while(T--)
    {
        int i, N, nh=0;         scanf("%d", &N);         for(i=1; i<=N; i++)
        {
            scanf("%d%d", &p[i].l, &p[i].r);
            Hash[nh++] = p[i].l, Hash[nh++] = p[i].l-1;
            Hash[nh++] = p[i].r, Hash[nh++] = p[i].r+1;
        }
        sort(Hash, Hash+nh);
        nh = unique(Hash, Hash+nh) - Hash;         Build(1, 1, nh);         int ans = 0;         for(i=N; i>0; i--)
        {
            int l = lower_bound(Hash, Hash+nh, p[i].l) - Hash;
            int r = lower_bound(Hash, Hash+nh, p[i].r) - Hash;             if(Insert(1, l, r) == true)
                ans += 1;
        }         printf("%d\n", ans);
    }     return 0;
}

D - Mayor's posters - 2528(区间覆盖)的更多相关文章

  1. POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

    任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. POJ 2528 - Mayor's posters - [离散化+区间修改线段树]

    题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...

  3. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45703   Accepted: 13239 ...

  4. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

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

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  6. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  7. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  8. POJ 2528 Mayor's posters(线段树/区间更新 离散化)

    题目链接: 传送门 Mayor's posters Time Limit: 1000MS     Memory Limit: 65536K Description The citizens of By ...

  9. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

随机推荐

  1. C#解leetcode 189. Rotate Array

    Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array  ...

  2. ViewPager滑动特效实现

    ewPager最常用于结合Fragment,这是一个方便的方式来供应和管理每个页面的生命周期. MainAcitivity的代码 import java.util.ArrayList; import ...

  3. PHP 文件上传功能

    <?php /** * TestGuest Version1.0 * ================================================ * Web:2955089 ...

  4. NHibernate执行原生带sum的sql

    可以自行查出想要的字段,并且返回的是model类型,可以点出所有的字段. 但是select 后面的字段名 必须有xml的字段名一模一样. ISession iSession = null; strin ...

  5. Python sys.path.append

    python sys.path.append 对于模块和自己写的程序不在同一个目录下,可以把模块的路径通过sys.path.append(路径)添加到程序中. 在程序开头加上: import syss ...

  6. Swift - 18 - 数组的基础操作

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...

  7. (转载)图解Linux系统的系统架构

    我以下图为基础,说明Linux的架构(architecture).(该图参考<Advanced Programming in Unix Environment>) 最内层是硬件,最外层是用 ...

  8. Phaser开源2d引擎 javascript/html5游戏框架

    功能特点(Features) 易维护代码(Easy Asset Loading) Phaser可以加载图片,音频文件,数据文件,文本文件和自动解析精灵图和纹理地图集数据(出口纹理封隔器或Flash C ...

  9. JQUERY1.9学习笔记 之基本过滤器(九) 小于选择器

    小于选择器 jQuery( ":lt(index)" ) jQuery( ":lt(-index)" ) 描述:选择所有小于指定下标的元素. <!DOCT ...

  10. WEB兼容性之JS

    1. 获取iframe的window对象 //三种方法 window.iframeName window.frames[iframeName] document.getElementById(ifra ...