CodeForces 652D Nested Segments (树状数组)

题意:给你n个区间,问每个区间包含了多少个区间

思路:先离散化一波,然后就是显然的树状数组瞎搞了...和POJ2481差不多


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200000+100
#define LL long long
int cas=1,T;
int c[maxn];
int ans[maxn];
struct Node
{
	int l,r,id;
}node[maxn];
bool cmp(Node x,Node y)
{
	if (x.r==y.r) return x.l<y.l;
	return x.r<y.r;
}
bool cmp1(Node x,Node y)
{
	if (x.l==y.l)
		return x.r<y.r;
	return x.l>y.l;
}
int lowbit(int x)
{
	return x&(-x);
}
void add(int x,int v)
{
	while (x<=maxn)
	{
		c[x]+=v;
		x+=lowbit(x);
	}
}
int sum(int x)
{
	int ans = 0;
	while (x)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	int n;
	scanf("%d",&n);
	for (int i = 1;i<=n;i++)
	{
		scanf("%d%d",&node[i].l,&node[i].r);
		node[i].id=i;
	}
	sort(node+1,node+n+1,cmp);
    for (int i = 1;i<=n;i++)
	{
		node[i].r=i;
	}
	sort(node+1,node+1+n,cmp1);
	for (int i = 1;i<=n;i++)
	{
		ans[node[i].id]=sum(node[i].r);
		add(node[i].r,1);
	}
    for (int i = 1;i<=n;i++)
		printf("%d\n",ans[i]);
	//freopen("in","r",stdin);
	//scanf("%d",&T);
	//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
	return 0;
}

Description

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.

Sample Input

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值