zoukankan      html  css  js  c++  java
  • (线段树,离散化)poj2528-Mayor'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 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

    本身的数据范围是比较大的,但考虑到我们其实关心的只是区间的关系,每个区间的长度并不在意,故可以建立一个映射,使数据范围降下来。题目中是用一个字母i表示一个区间,改成[i,i+1)
    会好很多,也方便进行排序。排序时使用set进行(这样用[i,i+1)排序不再需要像其他一些题解里所写的一样加数字),用map建立索引。之后就是线段树的更新操作。向下结点的更新在更改到
    这个父结点时再进行。
      1 #include <iostream>
      2 //#include<bits/stdc++.h>
      3 #include <stack>
      4 #include <queue>
      5 #include <map>
      6 #include <set>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 using namespace std;
     11 typedef long long ll;
     12 typedef unsigned long long ull;
     13 const int MAX=1e4+5;
     14 set <int> x;
     15 int l[MAX],r[MAX];
     16 map<int,int> re;
     17 struct node
     18 {
     19     int left,right;
     20     int col;
     21 }st[200*MAX];
     22 int an=0;
     23 bool vi[MAX];
     24 void init(int l,int r,int k)
     25 {
     26     st[k].left=l;
     27     st[k].right=r;
     28     st[k].col=0;
     29     if(l+1==r)
     30         return ;
     31     init(l,(l+r)/2,2*k);
     32     init((l+r)/2,r,2*k+1);
     33 }
     34 void update(int l,int r,int k,int co)
     35 {
     36     if(st[k].left==l&&st[k].right==r)
     37     {
     38         st[k].col=co;
     39         return;
     40     }
     41     if(st[k].col)
     42     {
     43         st[2*k].col=st[2*k+1].col=st[k].col;
     44         st[k].col=0;
     45     }
     46     if(st[2*k].right<=l)
     47         update(l,r,2*k+1,co);
     48     else if(st[2*k].right>=r)
     49         update(l,r,2*k,co);
     50     else
     51     {
     52         update(l,st[2*k].right,2*k,co);
     53         update(st[2*k].right,r,2*k+1,co);
     54     }
     55 }
     56 void query(int k)
     57 {
     58     if(st[k].col)
     59     {
     60         if(!vi[st[k].col])
     61         {
     62             an++;
     63             vi[st[k].col]=true;
     64         }
     65         return;
     66     }
     67     if(st[k].left+1==st[k].right)
     68         return;
     69     query(2*k);
     70     query(2*k+1);
     71     return;
     72 
     73 }
     74 int main()
     75 {
     76     int c;
     77     scanf("%d",&c);
     78     while(c--)
     79     {
     80         int n;
     81         scanf("%d",&n);
     82         int i;
     83         for(i=1;i<=n;i++)
     84         {
     85             scanf("%d %d",&l[i],&r[i]);
     86             r[i]++;
     87             x.insert(l[i]);
     88             x.insert(r[i]);
     89         }
     90         i=1;
     91         while(!x.empty())
     92         {
     93             re[*x.begin()]=i;
     94             x.erase(x.begin());
     95             i++;
     96         }
     97         init(1,i,1);
     98         for(i=1;i<=n;i++)
     99             update(re[l[i]],re[r[i]],1,i);
    100         memset(vi,false,sizeof(vi));
    101         an=0;
    102         query(1);
    103         printf("%d
    ",an);
    104 
    105     }
    106 }
     
  • 相关阅读:
    Python 面向对象3-类变量与实例变量
    Python面向对象2-类和构造方法
    Linux LVM动态扩容
    Anslib 使用错误归纳
    Linux Centos7安装最新anslib
    Linux 配置最新的epel源
    Linux centos7安装python3并且不影响python2
    docker compose启动服务超时重启记录
    spring cloud学习(三)使用Ribbon实现客户端负载均衡
    spring cloud 学习(二)关于 Eureka 的学习笔记
  • 原文地址:https://www.cnblogs.com/quintessence/p/6418556.html
Copyright © 2011-2022 走看看