【CodeForces

来源:互联网 发布:js 定义集合对象 编辑:程序博客网 时间:2024/06/03 14:41

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.

Example
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个区间,输出每个区间包含的区间数目。

对区间线段问题的处理方法有常见的几种,都可以尝试一下。
一看到区间线段问题,就要想到按照坐标排序( 或者按照每个点来排序同时还要标注好点的类型)。
这里我们可以用排序的方法,这样就可以固定一个方向,然后找到规律就可以了。
同时坐标的范围太大了,但是总共才有2e5个区域,即4e5个点,所以我们可以用离散化来处理。

代码

#include<bits/stdc++.h>using namespace std;typedef pair<int,int>pii;#define first fi#define second se#define  LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 2e5+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct BIT{    int n;    int c[MAXN<<1];    inline int lowbit(int x) { return x&(-x);}    void add(int x,int val){        for(int i=x;i<=n;i+=lowbit(i))            c[i]+=val;    }    int sum(int x){        int ans=0;        for(int i=x;i>0;i-=lowbit(i))             ans+=c[i];        return ans;    }}bit;  struct Node{    int s,e;    int id;    bool operator<(Node b){   // 按照这个规则来排序,然后从左向右枚举,这样就固定了一个方向。         if(e==b.e) return s>b.s;         return e<b.e;    }}Q[MAXN];int ans[MAXN];int X[MAXN<<1],size;int main(){    CLOSE();//  fread();//  fwrite();    int n;    while(scanf("%d",&n)!=EOF){        bit.n=n<<1;        memset(bit.c,0,sizeof(bit.c));        int a,b;size=0;        for(int i=1;i<=n;i++) {            scanf("%d%d",&a,&b);             Q[i].id=i; Q[i].s=a; Q[i].e=b;            X[size++]=a; X[size++]=b;        }        sort(X,X+size);        size=unique(X,X+size)-X;        sort(Q+1,Q+1+n);        for(int i=1;i<=n;i++){            int l=lower_bound(X,X+size,Q[i].s)-X+1;  // 最后加1是为了防止正好是第0个元素,但是树状数组是以1开始的             int r=lower_bound(X,X+size,Q[i].e)-X+1;            ans[Q[i].id]=bit.sum(r)-bit.sum(l-1);            bit.add(l,1);// 不断将左点插入到数组中         }        for(int i=1;i<=n;i++)             printf("%d\n",ans[i]);    }    return 0;}
原创粉丝点击