Educational Codeforces Round 10 D. Nested Segments

D. Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.

Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.

Examples
Input
4
1 8
2 3
4 7
5 6
Output
3
0
1
0
Input
3
3 4
1 5
2 6
Output
0
1
1





tips :   注意离散化就行,用右端点的id号来替换右端点的值,使1e9的空间降到1e5,之后用线段树来保存,降低时间复杂度;



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>

using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 200005;
int SUM[maxn<<3];

int max(int a,int b){if(a>b)return a;else return b;}
int min(int a,int b){if(a<b)return a;else return b;}


struct node
{
    int x;
    int y;
    int id;
}data[200005];


int cmp(node a, node b)
{
    if(a.x>b.x)
        return 1;
    else if(a.x==b.x)
    {
        return a.y>b.y;
    }
    return 0;
}

int cmp1(node a,node b)
{
    if(a.y==b.y)
        return a.x<b.x;
    return a.y<b.y;
}


void PushUP(int rt)
{
  SUM[rt] = SUM[rt<<1] + SUM[rt<<1|1];
}

void build(int l,int r,int rt) {
  if (l == r)
  {
    SUM[rt] = 0;
    //printf("mi = %d\n",MIN[rt]);
    //printf("ma = %d\n",MAX[rt]);
    return ;
  }
  int m = (l + r) >> 1;
  build(lson);
  build(rson);
  PushUP(rt);
}
void update1(int p,int add,int l,int r,int rt)
{
  if (l == r) {
    SUM[rt] = SUM[rt] + add;
    return ;
  }
  int m = (l + r) >> 1;
  if (p <= m) update1(p , add ,lson);
  else update1(p , add , rson);
  PushUP(rt);
}

int queryhe(int L,int R,int l,int r,int rt)
{
  if (L <= l && r <= R)
  {
    return SUM[rt];
  }
  int m = (l + r) >> 1;
  int ret = 0;
  if (L <= m) ret += queryhe(L , R , lson);
  if (R > m)  ret +=  queryhe(L , R , rson);
  return ret;
}


int ans[200005];


int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i;

        for(i=1;i<=n;i++)
        {
            scanf("%d%d",&data[i].x,&data[i].y);
            data[i].id=i;
        }

        build(1, n, 1);

        sort(data+1,data+1+n,cmp1);

        for(i=1;i<=n;i++)
        {
            data[i].y=i;
        }

        sort(data+1,data+1+n,cmp);
        for(i=1;i<=n;i++)
        {
           int xu=data[i].id;
           ans[xu]=queryhe(1,data[i].y,1,n,1);
           update1(data[i].y,1,1,n,1);
        }

        for(i=1;i<=n;i++)
            printf("%d\n",ans[i]);

    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值