zoukankan      html  css  js  c++  java
  • POJ 2528-Mayor'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 placing the posters and introduce the following rules:

    Every candidate can place exactly one poster on the wall.

    All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown).

    The wall is divided into segments and the width of each segment is one byte.

    Each poster must completely cover a contiguous number of wall segments.


    They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections.
    Your task is to find the number of visible posters when all the posters are placed given the information about posters' size, their place and order of placement on the electoral wall.

    Input

    The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers l i and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+1 ,... , ri.

    Output

    For each input data set print the number of visible posters after all the posters are placed.

    The picture below illustrates the case of the sample input.

    Sample Input

    1
    5
    1 4
    2 6
    8 10
    3 4
    7 10

    Sample Output

     4

    核心思想:

    线段树。
    由于墙的长度太长,开不出这么大的数组,先离散化再建树。

    代码如下:

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<algorithm>
      4 using namespace std;
      5 typedef long long ll;
      6 const int N=1e4+20;
      7 int b[N<<1],d[N<<1],cd,chu[N<<1],cnt;
      8 //存数据准备离散 
      9 struct node{
     10     int l,r;
     11 }a[N];
     12 struct tnode{
     13     int l,r,sum,lazy;
     14 }tr[N<<3];
     15 void pushdown(int m)
     16 {
     17     int t=tr[m].lazy;
     18     if(t!=-1)
     19     {
     20         tr[m<<1].lazy=t;
     21         tr[m<<1|1].lazy=t;
     22     }
     23     tr[m].lazy=-1;
     24     return;
     25 }
     26 void build(int m,int l,int r)
     27 {
     28     tr[m].l=l;
     29     tr[m].r=r;
     30     tr[m].lazy=-1;
     31     if(l==r)
     32         return;
     33     int mid=(l+r)>>1;
     34     build(m<<1,l,mid);
     35     build(m<<1|1,mid+1,r);
     36     return;
     37 }
     38 void update(int m,int l,int r,int v)
     39 {
     40     if(tr[m].l==l&&tr[m].r==r)
     41     {
     42         tr[m].lazy=v;
     43         return;
     44     }
     45     pushdown(m);
     46     int mid=(tr[m].l+tr[m].r)>>1;
     47     if(r<=mid)
     48         update(m<<1,l,r,v);
     49     else if(l>mid)
     50         update(m<<1|1,l,r,v);
     51     else
     52     {
     53         update(m<<1,l,mid,v);
     54         update(m<<1|1,mid+1,r,v);
     55     }
     56     return;
     57 }
     58 void query(int m)
     59 {
     60     if(tr[m].l==tr[m].r)
     61     {
     62         chu[cnt++]=tr[m].lazy;
     63         return;
     64     }
     65     pushdown(m);
     66     query(m<<1);
     67     query(m<<1|1);
     68 }
     69 int getid(int x)
     70 {
     71     return lower_bound(d,d+cd,x)-d+1;
     72 }
     73 int main()
     74 {
     75     int T;
     76     cin>>T;
     77     while(T--)
     78     {
     79         int n;
     80         scanf("%d",&n);
     81         //输入并准备离散 
     82         int cb=1;
     83         b[0]=-1;
     84         for(int i=0;i<n;i++)
     85         {
     86             scanf("%d%d",&a[i].l,&a[i].r);
     87             b[cb++]=a[i].l;
     88             b[cb++]=a[i].r;
     89         }
     90         //离散 
     91         sort(b+1,b+cb);
     92         cd=0;
     93         for(int i=1;i<cb;i++)
     94             if(b[i]!=b[i-1])
     95                 d[cd++]=b[i];
     96         //根据离散后的长度cd建树 
     97         build(1,1,cd);
     98         //更新线段树 
     99         for(int i=0;i<n;i++)
    100             update(1,getid(a[i].l),getid(a[i].r),i+1);
    101         cnt=1;
    102         chu[0]=-1;
    103         //将pushdown全部推到底部 
    104         query(1);
    105         //求ans 
    106         sort(chu+1,chu+cnt);
    107         int ans=0;
    108         for(int i=1;i<cnt;i++)
    109             if(chu[i]!=chu[i-1])
    110                 ans++;
    111         //输出 
    112         printf("%d
    ",ans);
    113     }
    114     return 0;
    115 }
  • 相关阅读:
    oracle 与mysql 的当前时间比较
    easyui 时间定格为 时分
    date类型数据插入
    mac 获取idea&&datagrip激活码
    静态代码块
    nginx mac 下启动 停止 重启,查看安装位置
    定时任务的时间规则
    雅酷帮微信公众平台操作手册
    微信公众平台中通过网页增加好友
    微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
  • 原文地址:https://www.cnblogs.com/NothingbutFight/p/11307282.html
Copyright © 2011-2022 走看看