Mayor’s postersPOJ2528

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 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


n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) 。求出最后还能看见多少张海报。
Input
第一行: 样例个数T
第二行: 贴海报的人n
第三行: 每个人贴海报的范围
接下来n行: 每个人贴海报的范围
Output
对于每一个输入,输出最后可以看到的海报张数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<map>
#include<string.h>
using namespace std;
int Left[11111], Right[11111];
int tree[150000];
int existed[11111];
int answer;
map<int, int>mp;
void build(int temp, int left, int right)
{
tree[temp] = 0;
if (left == right)
{
return;
}
int mid = left + right >> 1;
build(temp << 1, left, mid);
build(temp << 1 | 1, mid + 1, right);
}

void update(int temp, int l, int r, int x, int y, int k)
{
if (tree[temp] != 0)
{
tree[temp << 1] = tree[temp << 1 | 1] = tree[temp];
tree[temp] = 0;
}
if (x<=l&&r <= y)
{
tree[temp] = k;
return;
}
int mid = l + r >> 1;
if (y <= mid)
{
update(temp << 1, l, mid, x, y, k);
}
else if (x > mid)
{
update(temp << 1 | 1, mid + 1, r, x, y, k);
}
else
{
update(temp << 1, l, mid, x, y, k);
update(temp << 1 | 1, mid + 1, r, x, y, k);
}
}
void query(int temp, int left, int right)
{
if (left == right || tree[temp] != 0)
{
if (existed[tree[temp]] == 0)
answer++;
existed[tree[temp]]++;
return;
}
int mid = left + right >> 1;
query(temp << 1, left, mid);
query(temp << 1 | 1, mid + 1, right);
}
int main()
{
int cases;
scanf("%d", &cases);
while (cases--)
{
memset(existed, 0, sizeof(existed));
mp.clear();
int n;
scanf("%d", &n);
int maxpoint = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d%d", &Left[i], &Right[i]);
mp[Left[i]]++;
mp[Right[i]]++;
}
map<int, int>::iterator it = mp.begin();
for ( int i=1; it != mp.end(); it++)
{
it->second = i++;
}
for (int i = 1; i <= n; i++)
{
Left[i] = mp[Left[i]];
Right[i] = mp[Right[i]];
maxpoint = max(Left[i], max(maxpoint, Right[i]));
}
build(1, 1, maxpoint);
for (int i = 1; i <= n; i++)
{
update(1, 1, maxpoint, Left[i], Right[i], i);
}
answer = 0;
query(1, 1, maxpoint);
printf("%d\n", answer);
}
}