zoukankan      html  css  js  c++  java
  • 【SDOJ 3741】 【poj2528】 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 li 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 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+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


    太令人窒息了!!!!!查错两小时!!!!!
    很容易看得出来这是个线段树,每次贴一张就相当于一次区间修改,完了之后刷一遍看有多少种.....

    but....
    仅仅这样是不够的,数据范围疯狂暗示我们它想要离散化
    然后就完了
    一定要注意不要写错板子啊啊啊啊啊啊啊
      1 #include<iostream>
      2 #include<cstdio>
      3 #include<algorithm>
      4 #include<cmath>
      5 #include<cstring>
      6 #define N 200010
      7 #define lc p<<1
      8 #define rc p<<1|1
      9 using namespace std;
     10 int n,t,m,tot;
     11 int ll[N],rr[N],a[N],col[N],ans;
     12 bool vis[N];
     13 struct tree
     14 {
     15     int l,r;
     16     int lazy,c;
     17 }T[N*4];
     18 inline void pushnow(int p,int c)
     19 {
     20     T[p].lazy=c;
     21     T[p].c=c;
     22 }
     23 inline void pushup(int p)//
     24 {
     25     if(!T[lc].c||!T[rc].c||T[lc].c!=T[rc].c) T[p].c=0;
     26     else T[p].c=T[rc].c;
     27 }
     28 inline void pushdown(int p)
     29 {
     30     if(T[p].lazy!=-1)
     31     {
     32         pushnow(lc,T[p].lazy);
     33         pushnow(rc,T[p].lazy);
     34         T[p].lazy=-1;
     35     }
     36 }
     37 void build(int p,int l,int r)//
     38 {
     39     T[p].l=l; T[p].r=r;
     40     if(l==r)
     41     {
     42         T[p].c=-1;
     43         T[p].lazy=-1;
     44         return;
     45     }
     46     int mid=(T[p].l+T[p].r)>>1;
     47     build(lc,l,mid); build(rc,mid+1,r);
     48     pushup(p);
     49 }
     50 void update(int p,int ql,int qr,int v)
     51 {
     52     if(ql<=T[p].l&&T[p].r<=qr)//!!!!!!!!!!!!!!!!!!
     53     {
     54         pushnow(p,v);
     55         return;
     56     }
     57     pushdown(p);
     58     int mid=(T[p].l+T[p].r)>>1;
     59     if(ql<=mid) update(lc,ql,qr,v);
     60     if(qr>mid) update(rc,ql,qr,v);
     61     pushup(p);
     62 }
     63 
     64 void query(int p,int ql,int qr)
     65 {
     66     if(T[p].c==-1) return;
     67     else if(T[p].c>0)
     68     {
     69         col[T[p].c]=1;
     70         return;
     71     }
     72     int mid=(T[p].l+T[p].r)>>1;
     73     pushdown(p);
     74     if(ql<=mid) query(lc,ql,qr);
     75     if(qr>mid) query(rc,ql,qr);
     76 }
     77 int main()
     78 {
     79     scanf("%d",&t);
     80     while(t--)
     81     {
     82         int ans=0;
     83         scanf("%d",&n);
     84         for(int i=1;i<=n;i++)
     85         {
     86             int pl,pr;
     87             scanf("%d%d",&pl,&pr);
     88             ll[i]=pl; rr[i]=pr;
     89             a[i*2-1]=pl;a[i*2]=pr;
     90         }
     91         sort(a+1,a+1+2*n);
     92         m=unique(a+1,a+1+2*n)-(a+1);
     93         tot=m;
     94         for(int i=1;i<m;i++)
     95             if(a[i]+1<a[i+1])
     96                 a[++tot]=a[i]+1;
     97         sort(a+1,a+1+tot);
     98         build(1,1,tot);
     99         memset(col,0,sizeof(col));
    100         for(int i=1;i<=n;i++)
    101         {
    102             int x=lower_bound(a+1,a+1+tot,ll[i])-a;
    103             int y=lower_bound(a+1,a+1+tot,rr[i])-a;
    104             //cout<<x<<" "<<y<<endl;
    105             update(1,x,y,i);
    106         }
    107         query(1,1,tot);
    108         for(int i=1;i<=n;i++)
    109             if(col[i]) ans++;
    110         printf("%d
    ",ans);
    111     }
    112     return 0;
    113 }
    这是一篇代码
     
  • 相关阅读:
    delphi中屏蔽浏览器控件右键菜单
    书目:一些
    数据库ADONETDataAdapter对象参考
    数据库ADONET排序、搜索和筛选
    易语言数据类型及其长度
    易语言数据类型的初始值
    数据库ADONET使用DataAdapter对象
    ADONET使用DataSet处理脱机数据
    数据库ADONETOleDbParameter对象参考
    在项目中添加新数据集
  • 原文地址:https://www.cnblogs.com/kylara/p/9450363.html
Copyright © 2011-2022 走看看