zoukankan      html  css  js  c++  java
  • POJ 2528 Mayor's posters(线段树)

     

    题目大意

     

    贴海报。每张海报的高度都是一样的,唯独宽度不一样。每张海报只能占用整数倍的单位线段长度,贴了 n(n<=10000) 张海报之后,有几张能够看见(有一个角能看见这张海报也算被看见了)?海报的宽度最大可以达到 10^7

     

    做法分析

     

    一看就是线段树

    先不考虑线段的长度能够达到 10^7

    肯定是给每张海报一个颜色,然后在这张海报能够占领的区间中涂上这个颜色。每次更新的时候就直接区间操作,记得使用懒操作

    最后统计颜色就行了,统计颜色的时候用一个辅助数组,记录这个颜色是否出现过

    最后就是处理线段的长度了,显然离散化

    但是这里有一个小问题:题目所给的区间,例如是 [2, 4] ,表示的是 第 2 个到第 4 个长度为 1 的小区间。我们这里不能直接根据数的大小离散化

    排序后,假设当前数是 x,如果 x-1 出现过,那么 x 离散化后应该是 x-1 的标记 +1,否则应该是上一个的标记 +2

     

    参考代码

    POJ 2528
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 #include <map>
      6 #include <vector>
      7 
      8 using namespace std;
      9 
     10 const int N=20006;
     11 
     12 int ans;
     13 bool vs[N];
     14 
     15 struct interval_tree
     16 {
     17     struct data
     18     {
     19         int st, en, color;
     20         data() {}
     21         data(int a, int b, int c)
     22         {
     23             st=a, en=b, color=c;
     24         }
     25     }T[N<<3];
     26 
     27     void build(int id, int L, int R)
     28     {
     29         T[id]=data(L, R, 0);
     30         if(L==R) return;
     31         int mid=(L+R)>>1;
     32         build(id<<1, L, mid);
     33         build(id<<1|1, mid+1, R);
     34     }
     35 
     36     inline void pushDown(int id)
     37     {
     38         T[id<<1].color=T[id<<1|1].color=T[id].color;
     39         T[id].color=0;
     40     }
     41 
     42     void update(int id, int L, int R, int color)
     43     {
     44         if(L<=T[id].st && T[id].en<=R)
     45         {
     46             T[id].color=color;
     47             return;
     48         }
     49         if(T[id].color!=0) pushDown(id);
     50 
     51         int mid=(T[id].st+T[id].en)>>1;
     52         if(R<=mid) update(id<<1, L, R, color);
     53         else if(L>mid) update(id<<1|1, L, R, color);
     54         else update(id<<1, L, R, color), update(id<<1|1, L, R, color);
     55     }
     56 
     57     void query(int id)
     58     {
     59         if(T[id].color!=0 && !vs[T[id].color])
     60         {
     61             ans++, vs[T[id].color]=1;
     62             return;
     63         }
     64         if(T[id].st==T[id].en) return;
     65         if(T[id].color!=0) pushDown(id);
     66         query(id<<1), query(id<<1|1);
     67     }
     68 }tree;
     69 
     70 vector <int> temp;
     71 map <int, int> ihash;
     72 
     73 struct node
     74 {
     75     int L, R;
     76     node() {}
     77     node(int a, int b)
     78     {
     79         L=a, R=b;
     80     }
     81 }op[N];
     82 int n, t, tot;
     83 
     84 int main()
     85 {
     86     scanf("%d", &t);
     87     for(int ca=1; ca<=t; ca++)
     88     {
     89         scanf("%d", &n);
     90         temp.clear(), ihash.clear(), tot=0;
     91         for(int i=0, a, b; i<n; i++)
     92         {
     93             scanf("%d%d", &a, &b);
     94             op[i]=node(a, b);
     95             temp.push_back(a), temp.push_back(b);
     96         }
     97         sort(temp.begin(), temp.end());
     98         for(int i=0; i<2*n; i++)
     99         {
    100             int u=temp[i];
    101             if(ihash.find(u)==ihash.end())
    102             {
    103                 if(ihash.find(u-1)==ihash.end())
    104                     ihash.insert(make_pair(u, tot+1)), tot+=2;
    105                 else
    106                     ihash.insert(make_pair(u, tot)), tot++;
    107             }
    108         }
    109 
    110         tree.build(1, 1, tot+2);
    111         for(int i=0; i<n; i++)
    112             tree.update(1, ihash[op[i].L], ihash[op[i].R], i+1);
    113         for(int i=1; i<=n; i++) vs[i]=0;
    114         ans=0;
    115         tree.query(1);
    116         printf("%d\n", ans);
    117     }
    118     return 0;
    119 }

    AC通道

     

    POJ 2528 Mayor's posters

    南洋理工 第 9 题 posters

  • 相关阅读:
    一道Twitter面试题
    聊下并发和Tomcat线程数(错误更正)
    Entity Framework系列教程汇总
    SqlServer基础汇总
    .Net Core中间件和过滤器实现错误日志记录
    快速掌握mongoDB(六)——读写分离的副本集实现和Sharing介绍
    快速掌握mongoDB(五)——通过mongofiles和C#驱动操作GridFS
    快速掌握mongoDB(四)—— C#驱动MongoDB用法演示
    快速掌握mongoDB(三)——mongoDB的索引详解
    快速掌握mongoDB(二)——聚合管道和MapReduce
  • 原文地址:https://www.cnblogs.com/zhj5chengfeng/p/2982737.html
Copyright © 2011-2022 走看看