CodeForces - 652D Nested Segments
Nested Segments
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条线段,求线段包含情况,输出包含数。
思路:使用树状数组,数据太大,需要离散化。然后按左端点排序即可。
#include<bits/stdc++.h> using namespace std; const int maxn=400010; int a[maxn],tree[maxn],n,ans[maxn]; struct node{ int l,r,id; bool friend operator < (const node &u,const node &v){ if(u.l==v.l) return u.r<v.r; return u.l>v.l; } } p[maxn]; int lowbit(int k){ return k&(-k); } void updata(int k){ while(k<maxn){ tree[k]+=1; k+=lowbit(k); } } int sum(int k){ int ans=0; while(k){ ans+=tree[k]; k-=lowbit(k); } return ans; } int main(){ int cnt=0; scanf("%d",&n); for(int i=1; i<=n; i++){ scanf("%d %d",&p[i].l,&p[i].r); a[++cnt]=p[i].r; p[i].id=i; } sort(a+1,a+1+cnt); cnt=unique(a+1,a+cnt+1)-(a+1); for(int i=1; i<=n; i++) p[i].r=lower_bound(a+1,a+1+cnt,p[i].r)-a; sort(p+1,p+1+n); for(int i=1; i<=n; i++){ ans[p[i].id]=sum(p[i].r+1); updata(p[i].r+1); } for(int i=1; i<=n; i++) printf("%d\n",ans[i]); }
PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~