您的位置:首页 > 其它

Educational Codeforces Round 10 D.Nested Segments

2016-03-29 16:10 232 查看
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


题意:给你n条线段(n<=2*10^5),每条线段一个[l,r],代表他的左右区间,输出对于每条线段,有多少条线段能被他完全覆盖。

 

思路:对r进行离散化,然后按L对查询从大到小进行排序,对每个r查询小于等于他的r有多少个

(树状数组求前缀和)

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=200100;
int C[maxn],Hash[maxn*2],ans[maxn];
struct Point{
int x,y,id;
}a[maxn];
int n;

bool cmp(Point u,Point v){
if(u.x!=v.x)
return u.x>v.x;
return u.y>v.y;
}

void add(int x){
while(x<=n){
C[x]+=1;
x+=(x&-x);
}
}

int sum(int x){
int ret=0;
while(x>0){
ret+=C[x];
x-=(x&-x);
}
return ret;
}

int main(){
int tot=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
Hash[++tot]=a[i].y;
}
sort(Hash+1,Hash+tot+1);
int m=unique(Hash+1,Hash+tot+1)-Hash;
for(int i=1;i<=n;i++)
a[i].y=lower_bound(Hash+1,Hash+m,a[i].y)-Hash;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
ans[a[i].id]=sum(a[i].y);
add(a[i].y);
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: