codeforces_652D. Nested Segments(树状数组、二分)

来源:互联网 发布:会展设计软件 编辑:程序博客网 时间:2024/05/09 04:37
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
41 82 34 75 6
output
3010
input
33 41 52 6
output
011

将点排序后,用二分找出线段两个端点在a数组的位置,将线段的末尾端点标1,用树状数组求出区间和,最后再按原先顺序输出。


#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define inf 0x3f3f3f3f#define maxn 200010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;struct node{    int l,r,ans,id;}p[2*maxn];int n,top=0;int c[2*maxn];int a[2*maxn];bool cmp1(node a,node b){    return a.l<b.l;}bool cmp2(node a,node b){    return a.id<b.id;}int update(int i,int x){    while(i<=2*n)    {        c[i]+=x;        i+=(i&(-i));    }}int Sum(int pos){    int sum=0;    while(pos>0)    {        sum+=c[pos];        pos-=(pos&(-pos));    }    return sum;}int main(){    Si(n);    mem(c,0);    int i;    for(i=1;i<=n;i++)    {        int l,r;        Si(l);Si(r);        a[top++]=l;        a[top++]=r;        p[i].l=l;        p[i].r=r;        p[i].id=i;    }    sort(a,a+top);    for(i=1;i<=n;i++)    {        p[i].l=upper_bound(a,a+top,p[i].l)-a;        p[i].r=upper_bound(a,a+top,p[i].r)-a;    }    sort(p+1,p+n+1,cmp1);    for(i=1;i<=n;i++)update(p[i].r,1);    for(i=1;i<=n;i++)    {        if(p[i].l+1>p[i].r-1)p[i].ans=0;        else        {            p[i].ans=Sum(p[i].r-1)-Sum(p[i].l+1);        }        update(p[i].r,-1);    }    sort(p+1,p+n+1,cmp2);    for(i=1;i<=n;i++)    Pi(p[i].ans);    return 0;}


0 0
原创粉丝点击