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

题目链接:http://poj.org/problem?id=2528

给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板。

很明显的线段树区间更新问题,每次放置木板就更新区间里的值。由于l和r范围比较大,内存就不够了,所以就用离散化的技巧 比如将1 4化为1 2,范围缩小,但是不影响答案。

写了这题之后对区间更新的理解有点加深了,重点在覆盖的理解(更新左右两个孩子节点,然后值清空),还是要多做做题目。

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <map>
  6 using namespace std;
  7 const int MAXN = 2e4 + 5;
  8 struct data {
  9     int l , r , sum;
 10 }T[MAXN << 2];
 11 map <int , int> mp;
 12 int x[MAXN] , y[MAXN] , cont , id[MAXN];
 13
 14 void init(int p , int l , int r) {
 15     int mid = (l + r) >> 1;
 16     T[p].l = l , T[p].r = r;
 17     if(l == r) {
 18         T[p].sum = 0;
 19         return ;
 20     }
 21     init(p << 1 , l , mid);
 22     init((p << 1)|1 , mid + 1 , r);
 23     T[p].sum = T[p << 1].sum + T[(p << 1)|1].sum;
 24 }
 25
 26 int query(int index , int p) {
 27     int mid = (T[p].l + T[p].r) >> 1;
 28     if(index == T[p].l && T[p].r == index) {
 29         return T[p].sum;
 30     }
 31     if(T[p].sum) {
 32         T[p << 1].sum = T[(p << 1)|1].sum = T[p].sum;
 33         T[p].sum = 0;
 34     }
 35     if(index <= mid) {
 36         query(index , p << 1);
 37     }
 38     else {
 39         query(index , (p << 1)|1);
 40     }
 41 }
 42
 43 void updata(int p , int l , int r , int val) {
 44     int mid = (T[p].l + T[p].r) >> 1;
 45     if(T[p].l >= l && T[p].r <= r) { //找到了区间,更新这个区间
 46         T[p].sum = val;
 47         return ;
 48     }
 49     if(T[p].sum) {  //重点注意,如果这个区间被访问了,并且这个区间要更新,就要将这个区间的值更新到其左右孩子的节点,并且要将这个区间的值清空,这样才能算是覆盖
 50         T[p << 1].sum = T[(p << 1)|1].sum = T[p].sum;
 51         T[p].sum = 0;
 52     }
 53     if(r <= mid) {
 54         updata(p << 1 , l , r , val);
 55     }
 56     else if(l > mid) {
 57         updata((p << 1)|1 , l , r , val);
 58     }
 59     else {
 60         updata(p << 1 , l , mid , val);
 61         updata((p << 1)|1 , mid + 1 , r , val);
 62     }
 63 }
 64
 65 int main()
 66 {
 67     int t , n;
 68     scanf("%d" , &t);
 69     while(t--) {
 70         scanf("%d" , &n);
 71         mp.clear();
 72         cont = 0;
 73         for(int i = 1 ; i <= n ; i++) {
 74             scanf("%d %d" , x + i , y + i);
 75             if(!mp[x[i]]) {
 76                 id[++cont] = x[i];
 77                 mp[x[i]]++;
 78             }
 79             if(!mp[y[i]]) {
 80                 id[++cont] = y[i];
 81                 mp[y[i]]++;
 82             }
 83         }
 84         sort(id + 1 , id + cont + 1);
 85         int len = -1; //离散化之后的最大的数
 86         for(int i = 1 ; i <= n ; i++) { //离散化
 87             x[i] = lower_bound(id + 1 , id + cont + 1 , x[i]) - id;
 88             len = max(x[i] , len);
 89             y[i] = lower_bound(id + 1 , id + cont + 1 , y[i]) - id;
 90             len = max(y[i] , len);
 91         }
 92         init(1 , 1 , len);
 93         for(int i = 1 ; i <= n ; i++) {
 94             updata(1 , x[i] , y[i] , i);
 95         }
 96         mp.clear();
 97         int res = 0;
 98         for(int i = 1 ; i <= len ; i++) {
 99             int temp = query(i , 1);
100             if(!mp[temp] && temp) {
101                 res++;
102                 mp[temp]++;
103             }
104         }
105         printf("%d\n" , res);
106     }
107 }

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

时间: 2024-08-02 14:43:06

POJ 2528 Mayor's posters (线段树区间更新+离散化)的相关文章

poj 2528 Mayor&#39;s posters 线段树区间更新

Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at al

POJ 2528 Mayor&#39;s posters(线段树,区间覆盖,单点查询)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45703   Accepted: 13239 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

Poj 2528 Mayor&#39;s posters (线段树+离散化)

题目连接: http://poj.org/problem?id=2528 题目大意: 有10000000块瓷砖,n张海报需要贴在墙上,每张海报所占的宽度和瓷砖宽度一样,长度是瓷砖长度的整数倍,问按照所给海报顺序向瓷砖上贴海报,最后有几张海报是可见的? 解题思路: 因为瓷砖块数和海报张数多,首选线段树,如果按照常规的建树方式,把瓷砖当做数的节点,肯定会MTL......... 所以我们可以用海报的起点和终点当做树的节点,这样树的节点才有20000个,但是这样建树的话,求海报覆盖了那些节点会很复杂,

poj 2528 Mayor&#39;s posters(线段树)

题目链接:http://poj.org/problem?id=2528 思路分析:线段树处理区间覆盖问题,也可以看做每次给一段区间染不同的颜色,最后求在整段区间上含有的所有颜色种类数: 注意由于区间太大,所以需要离散化: 区间更新:对于线段树的每个结点,标记颜色,初始时没有颜色,标记为0:当更新时,使用延迟标记,需要标记传递到子节点: 区间查询:使用深度优先查询线段树,当某个子节点的颜色不为0时,即停止深度优先搜索,并在map中查询是否已经记录该段区间的颜色: 代码如下: #include <i

POJ 2528 Mayor&#39;s posters 线段树成段更新+离散化

题目来源:POJ 2528 Mayor's posters 题意:很多张海报贴在墙上 求可以看到几张海报 看那两张图就行了 第一张俯视图 思路:最多2W个不同的数 离散化一下 然后成段更新 a[rt] = i代表这个区间是第i张报纸 更新玩之后一次query cover[i]=1代表可以看到第i张报纸 #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const

poj-----(2528)Mayor&#39;s posters(线段树区间更新及区间统计+离散化)

Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 43507   Accepted: 12693 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral post

Mayor&#39;s posters 线段树区间覆盖

题目链接 http://poj.org/problem?id=2528 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 dec

POJ 2777 Count Color (线段树区间更新加查询)

Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. There is a very long board with length L centimeter, L is a positive integer, so we can evenly d

poj 2777 Count Color (线段树区间更新)

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37647   Accepted: 11315 Description Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem.