poj 2528 Mayor's posters（线段树 离散化 区间更新 贴海报）

这个题目本来对大神来说可能是水题， 对我就不行了，昨晚非折腾到下半夜一点 搞定， 并且可以总结出 ，只有把问题想清楚，或着看人家解题报告自己把问题和代码思路

是不是发现给的墙面宽度特别宽？所以我学会了离散化思想，把离散的点先按照大小关系排序，同时有必要记住每个点对（左右端点）的所属第几张海报，然后按照大小关系依次编号，这样确实能够维系每个点对之间的原来的关系

Mayor's posters
 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 50053 Accepted: 14536

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

/*=============================================================================
#
#      Author: liangshu - cbam
#
#      QQ : 756029571
#
#      School : 哈尔滨理工大学
#
#
#     Filename: B.cpp
#
#     Description:
#        The people who are crazy enough to think they can change the world, are the ones who do !
=============================================================================*/

#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 10010;
struct Tree
{
int l, r, c;
} tree[INF * 14];

struct Node
{
int val,num;
} node[INF<<2];

set<int>cnt;

int cmp(Node a, Node b)
{
return a.val < b.val;
}

void create(int t, int l, int r)
{
tree[t].l = l;
tree[t].r = r;
tree[t].c = 0;
if(l == r)
return ;
int a, b, mid = (l + r)>>1;
create(t<<1, l, mid);
create(t<<1|1, mid + 1, r);
}

void update(int t, int val, int l, int r)
{

if(tree[t].l >= l && tree[t].r <= r)
{
tree[t].c = val;

return ;
}

if(tree[t].c > 0)
{
tree[t<<1].c = tree[t].c;
tree[t<<1|1].c =tree[t].c;
tree[t].c = 0;

}
if(tree[t].l == tree[t].r)
return ;
int mid = (tree[t].l + tree[t].r) >>1;
//    if(l <= mid)
//    {
//        update(t<<1, val, l, r);
//    }
//    if(mid < r)
//    {
//        update(t<<1|1, val, l, r);
//    }

if( l > mid)update(t<<1 | 1, val, l, r);
else if(r <= mid)
update(t<<1, val, l, r);
else
{
update(t<<1,val, l, mid);
update(t<<1|1,val, mid + 1, r);
}
}

int flag[INF<<2];
int coun = 0;
void cal(int t)
{
if(tree[t].c > 0)
{
if(!flag[tree[t].c])
{
coun++;
flag[tree[t].c] = 1;
}
return ;
}
if(tree[t].l == tree[t].r)
return ;
cal(t<<1);
cal(t<<1|1);
}

int main()
{
int dict[INF][3];
int t;
cin>>t;
int n,tx;
while(t--)
{
memset(flag, 0, sizeof(flag));
memset(dict, 0 ,sizeof(dict));
coun = 0;
tx = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d", &dict[i][0], &dict[i][1]);
node[2 * i - 1]. val = dict[i][0];
node[2 * i - 1].num = i;
node[2 * i].val = dict[i][1];
node[2 * i].num = -1 * i;
}
sort(node + 1, node + 2 * n  + 1 , cmp);
if(n >= 2)
{
for(int i = 2; i <= 2 * n ; i += 1)
{
if(node[i].val - node[i-1].val > 1)
{
node[2 * n  + tx].val = node[i].val - 1;
node[2 * n + 1 + tx].num = INF<<3;
tx++;
}
}
}
sort(node + 1, node + 2 * n  + tx  , cmp);

int x = 1;
dict[abs(node[1].num)][node[1].num > 0 ? 0 : 1] = x;
for(int i = 2; i <= 2 *n +tx -1 ; i++)
{
if(node[i].val != node[i-1].val)
{
if(node[i].num == INF<<3)
{
x++;
continue;
}
x++;
}
if(node[i].num > 0)
dict[node[i].num][0] = x;
else
dict[-1 * node[i].num][1] = x;
}
create(1, 1, x);
for(int i = 1; i<= n; i++)
{
update(1, i, dict[i][0], dict[i][1]);
}
cal(1);
printf("%d\n",coun );

}
return 0;

}



4