F - History

Polycarpus likes studying at school a lot and he is always diligent about his homework. Polycarpus has never had any problems with natural sciences as his great-great-grandfather was the great physicist Seinstein. On the other hand though, Polycarpus has never had an easy time with history.

Everybody knows that the World history encompasses exactly n events: the i-th event had continued from the year ai to the year biinclusive (ai < bi). Polycarpus easily learned the dates when each of n events started and ended (Polycarpus inherited excellent memory from his great-great-granddad). But the teacher gave him a more complicated task: Polycaprus should know when all events began and ended and he should also find out for each event whether it includes another event. Polycarpus' teacher thinks that an event j includes an event i if aj < ai and bi < bj. Your task is simpler: find the number of events that are included in some other event.

Input

The first input line contains integer n (1 ≤ n ≤ 105) which represents the number of events. Next n lines contain descriptions of the historical events, one event per line. The i + 1 line contains two integers ai and bi (1 ≤ ai < bi ≤ 109) — the beginning and the end of the i-th event. No two events start or finish in the same year, that is, ai ≠ aj, ai ≠ bj, bi ≠ aj, bi ≠ bj for all ij (where i ≠ j). Events are given in arbitrary order.

Output

Print the only integer — the answer to the problem.

Sample Input

Input
5
1 10
2 9
3 8
4 7
5 6
Output
4
Input
5
1 100
2 50
51 99
52 98
10 60
Output
4
Input
1
1 1000000000
Output
0

Hint

In the first example the fifth event is contained in the fourth. Similarly, the fourth event is contained in the third, the third — in the second and the second — in the first.

In the second example all events except the first one are contained in the first.

In the third example only one event, so the answer is 0.


新学的知识。。结构体排序,啥也不说了、就觉得太帅了!!=>、、


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
    int a,b;
}F[505050];
int cmp(node x ,node y)
{
    return x.a<y.a;
}
int main()
{
    int n;
    int t;
    int i;
    while(scanf("%d",&n)!=EOF)
    {
        t=0;
        int m;
        for(i=0;i<n;i++)
        {
            scanf("%d%d",&F[i].a,&F[i].b);
        }
        sort(F,F+n,cmp);
        m = F[0].b;
        for(i=1;i<n;i++)
        {

            if(F[i].b<m)
            {
                t++;
            }
            else
            {
                m = F[i].b;
            }
        }
        printf("%d\n",t);

    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值