POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】

任意门:http://poj.org/problem?id=2528

Mayor's posters

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 77814   Accepted: 22404

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

Source

题目大意:

贴海报,给出每张海报的起点和终点,问最后能看见多少张海报(后来的海报会把前面的覆盖);

解题思路:

海报的数据范围太大了,如果按照海报的数据范围开线段树会放不下而且浪费内存。所以考虑离散化,但是这不同于单点离散化,这是区间离散化,意思就是说离散化原来数据的同时还要保留他们的区间的性质。(大神的栗子:原数据“1-10, 1-4,6-10”,常规单点离散化之后“1-4,1-2,3-4”,原本不相邻的点变成邻居了,如果继续按照这种错误的数据建树,那么后果是直接失去了区间的性质) 如果我们想缩小这两点的数据范围,但又想保留不相邻两点表示一个区间的性质,区间离散化要这样搞:缩小数据范围就是按原本数据大小进行排序,他们的先后性保持不变,新下标暂时替代他们,而保留不相邻两点的性质嘛,小脑洞:在他们之间加一个值把他们隔离,这样他们排序之后就不会相邻啦。

离散化之后就还是常规的线段树区间更新,但最后求解的区间查询操作如果暴力单点查询找出有几种不同的海报这样太委屈线段树了,这里的查询只要这张海报类型出现了,那么我们的答案肯定就要加加,然后这张海报我们就可以book一下,下次如果见到有book的海报类型也不需要再更新答案了,这些判重的操作可以直接在Query()里面做,这样才能更好的发挥线段树的本领。

AC code:

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <algorithm>
 4 #include <cstring>
 5 #define INF 0x3f3f3f3f
 6 #define LL long long int
 7 #define lson l, mid, root<<1
 8 #define rson mid+1, r, root<<1|1
 9 using namespace std;
10 const int MAXN = 1e4+10;
11 struct date
12 {
13     int L, R;
14 }node[MAXN];
15 int num[MAXN<<3];
16 int lazy_tag[MAXN<<4];
17 bool book[MAXN<<1];
18 int res, cnt;
19 int read()
20 {
21     int f=1, res=0;char s=getchar();
22     while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
23     while(s>='0'&&s<='9'){res=res*10+s-'0';s=getchar();}
24     res*=f;
25     return res;
26 }
27 void PushD(int root)
28 {
29     if(lazy_tag[root]){
30         lazy_tag[root<<1] = lazy_tag[root];
31         lazy_tag[root<<1|1] = lazy_tag[root];
32         lazy_tag[root] = 0;
33     }
34 }
35 
36 void Update(int st, int ed, int l, int r, int root, int val)
37 {
38     if(st <= l && ed >= r){lazy_tag[root] = val; return;}
39     PushD(root);
40     int mid = (l+r)>>1;
41     if(st <= mid) Update(st, ed, lson, val);
42     if(ed > mid)  Update(st, ed, rson, val);
43 }
44 
45 void Query(int l, int r, int root)
46 {
47     if(lazy_tag[root]){
48         if(!book[lazy_tag[root]]) res++;
49         book[lazy_tag[root]] = true;
50         return;
51     }
52     if(l == r) return;
53     int mid = (l+r)>>1;
54     Query(lson); Query(rson);
55 }
56 void init()
57 {
58     cnt = 0; res = 0;
59     memset(lazy_tag, 0, sizeof(lazy_tag));
60     memset(book, false, sizeof(book));
61 }
62 int main()
63 {
64     int T_case, N;
65     T_case = read();
66     while(T_case--)
67     {
68         init();
69         N = read();
70         for(int i = 0; i < N; i++){
71             node[i].L = read();node[i].R = read();
72             num[cnt++] = node[i].L;
73             num[cnt++] = node[i].R;
74         }
75         sort(num, num+cnt);
76         cnt = unique(num, num+cnt)-num;
77         int top = cnt;
78         for(int i = 0; i < top-1; i++){
79             if(num[i+1]!=num[i]+1) num[cnt++] = num[i]+1;
80         }
81 
82         sort(num, num+cnt);
83         /*
84         for(int i = 0; i < cnt; i++)
85             printf("%d ", num[i]);
86         puts("");
87         */
88         for(int i = 0; i < N; i++){
89             int st = lower_bound(num, num+cnt, node[i].L)-num;
90             int ed = lower_bound(num, num+cnt, node[i].R)-num;
91             Update(st+1, ed+1, 1, cnt, 1, i+1);
92         }
93         Query(1, cnt, 1);
94         printf("%d
", res);
95     }
96     return 0;
97 }
View Code
原文地址:https://www.cnblogs.com/ymzjj/p/9556464.html