Description

The citizens of bytetown, AB, cocould not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. the city councel 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 curous 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 <= LI <= RI <= 10000000. after the I-th poster is placed, it entirely covers all Wall segments numbered Li, 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 has strates the case of the sample input.

Sample Input

151 42 68 103 47 10

Sample output

4

Update and query the interval of the discretization and line segment tree.

#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h>#include <ctype.h>#include <iostream>#define lson o << 1, l, m#define rson o << 1|1, m+1, rusing namespace std;typedef long long LL;const int MAX = 200000000;const int maxn = 10010;int t, n, a, b, c, ans;int fu[maxn<<4], cnt[maxn<<4], vis[maxn<<2];struct C{ int x, y;} in[maxn];int bs(int v, int x, int y) { int m; while(x < y) { m = (x+y) >> 1; if(fu[m] >= v) y = m; else x = m+1; } return x;}void down(int o) { if(cnt[o] != -1) { cnt[o<<1] = cnt[o<<1|1] = cnt[o]; cnt[o] = -1; }}void update(int o, int l, int r) { if(a <= l && r <= b) { cnt[o] = c; return; } down(o); int m = (l+r) >> 1; if(a <= m) update(lson); if(m < b ) update(rson);}void query(int o, int l, int r) { if(cnt[o] != -1) { if(!vis[ cnt[o] ]) { ans ++; vis[ cnt[o] ] = 1; } return ; } if(l == r) return; int m = (l+r) >> 1; query(lson); query(rson);}int main(){ cin >> t; while(t--) { cin >> n; int k = 0; for(int i = 0; i < n; i++) { scanf("%d%d", &in[i].x, &in[i].y); fu[k++] = in[i].x; fu[k++] = in[i].y; } sort(fu, fu+k); int m = 1; for(int i = 1; i < k; i++) if(fu[i] != fu[i-1]) fu[m++] = fu[i]; for(int i = m-1; i >= 1; i--) if(fu[i] != fu[i-1] + 1) fu[m++] = fu[i-1]+1; sort(fu, fu+m); memset(cnt, -1, sizeof(cnt)); for(int i = 0; i < n; i++) { a = bs(in[i].x, 0, m-1); b = bs(in[i].y, 0, m-1); c = i; update(1, 0, m-1); } memset(vis, 0, sizeof(vis)); ans = 0; query(1, 0, m-1); printf("%d\n", ans); } return 0;}

Zookeeper