【POJ】2528 Mayor's posters ——离散化+线段树

Mayor's posters

Time Limit:  1000MS     Memory Limit:  65536K
 

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 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. 
【POJ】2528 Mayor's posters ——离散化+线段树

Sample Input

1

5

1 4

2 6

8 10

3 4

7 10

Sample Output

4

Source

 
题解:离散化+线段树。一看数据就知道不离散肯定会MLE。离散化的具体方法可以参考这篇: 《浅谈数据的离散化》
AC代码:
 
  1 #include <cstdio>

  2 #include <cstring>

  3 #include <algorithm>

  4 using namespace std;

  5 

  6 const int HLEN = 10000010;

  7 const int LEN = 20020;

  8 

  9 struct line

 10 {

 11     int left;

 12     int right;

 13     int value;

 14 }line[LEN*4];

 15 

 16 struct input

 17 {

 18     int a, b;

 19 }in[LEN>>1];  //用于记录输入的数据

 20 int sub[LEN]; //用于离散化

 21 

 22 short hash[HLEN]; //用于储存离散后的映射关系

 23 int ans = 0;

 24 bool poster[LEN];

 25 

 26 void buildt(int l, int r, int step)

 27 {

 28     line[step].left = l;

 29     line[step].right = r;

 30     line[step].value = 0;

 31     if (l == r)

 32         return;

 33     int mid = (l + r)>>1;

 34     buildt(l, mid, step<<1);

 35     buildt(mid+1, r, step<<1|1);

 36 }

 37 

 38 void pushdown(int step)

 39 {

 40     line[step<<1].value = line[step<<1|1].value = line[step].value;

 41     line[step].value = 0;

 42 }

 43 

 44 void update(int l, int r, int value, int step)

 45 {

 46     if (line[step].left == l && line[step].right == r){

 47         line[step].value = value;

 48         return;

 49     }

 50     if (line[step].value != 0 && line[step].value != value) //如果当前位置已被涂过色,则向下更新

 51         pushdown(step);

 52     int mid = (line[step].left + line[step].right)>>1;

 53     if (r <= mid)

 54         update(l, r, value, step<<1);

 55     else if (l > mid)

 56         update(l, r, value, step<<1|1);

 57     else{

 58         update(l, mid, value, step<<1);

 59         update(mid+1, r, value, step<<1|1);

 60     }

 61 }

 62 

 63 void query(int l, int r, int step)

 64 {

 65     if (line[step].value != 0){

 66         if (!poster[line[step].value]){

 67             poster[line[step].value] = true;

 68             ans++;

 69         }

 70         return;

 71     }

 72     if (line[step].left == line[step].right)

 73         return;

 74     int mid = (line[step].left + line[step].right)>>1;

 75     if (r <= mid)

 76         query(l, r, step<<1);

 77     else if (l > mid)

 78         query(l, r, step<<1|1);

 79     else{

 80         query(l, mid, step<<1);

 81         query(mid+1, r, step<<1|1);

 82     }

 83 }

 84 

 85 int main()

 86 {

 87     int T;

 88     scanf("%d", &T);

 89     while(T--){

 90         memset(poster, 0, sizeof(poster));

 91         int n;

 92         int num = 0;

 93         scanf("%d", &n);

 94         for(int i = 1; i <= n; i++){

 95             int a, b;

 96             scanf("%d %d", &a, &b);

 97             sub[num++] = a;

 98             sub[num++] = b;

 99             in[i].a = a;

100             in[i].b = b;

101         }

102         

103         sort(sub, sub+num);

104         int size = unique(sub, sub+num) - sub; //删除重复数据并使用哈希表映射

105         for(int i = 0; i < size; i++)

106             hash[sub[i]] = i+1;

107             

108         buildt(1, size, 1); //离散后的值建树

109 

110         for(int i = 1; i <= n; i++)

111             update(hash[ in[i].a], hash[ in[i].b], i, 1); //用离散后的值更新

112         ans = 0;

113         query(1, size, 1);

114         printf("%d\n", ans);

115     }

116     return 0;

117 }

 

你可能感兴趣的:(post)