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.
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.
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
4 1 8 2 3 4 7 5 6
3 0 1 0
3 3 4 1 5 2 6
0 1 1
tips : 注意离散化就行,用右端点的id号来替换右端点的值,使1e9的空间降到1e5,之后用线段树来保存,降低时间复杂度;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 200005;
int SUM[maxn<<3];
int max(int a,int b){if(a>b)return a;else return b;}
int min(int a,int b){if(a<b)return a;else return b;}
struct node
{
int x;
int y;
int id;
}data[200005];
int cmp(node a, node b)
{
if(a.x>b.x)
return 1;
else if(a.x==b.x)
{
return a.y>b.y;
}
return 0;
}
int cmp1(node a,node b)
{
if(a.y==b.y)
return a.x<b.x;
return a.y<b.y;
}
void PushUP(int rt)
{
SUM[rt] = SUM[rt<<1] + SUM[rt<<1|1];
}
void build(int l,int r,int rt) {
if (l == r)
{
SUM[rt] = 0;
//printf("mi = %d\n",MIN[rt]);
//printf("ma = %d\n",MAX[rt]);
return ;
}
int m = (l + r) >> 1;
build(lson);
build(rson);
PushUP(rt);
}
void update1(int p,int add,int l,int r,int rt)
{
if (l == r) {
SUM[rt] = SUM[rt] + add;
return ;
}
int m = (l + r) >> 1;
if (p <= m) update1(p , add ,lson);
else update1(p , add , rson);
PushUP(rt);
}
int queryhe(int L,int R,int l,int r,int rt)
{
if (L <= l && r <= R)
{
return SUM[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if (L <= m) ret += queryhe(L , R , lson);
if (R > m) ret += queryhe(L , R , rson);
return ret;
}
int ans[200005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i;
for(i=1;i<=n;i++)
{
scanf("%d%d",&data[i].x,&data[i].y);
data[i].id=i;
}
build(1, n, 1);
sort(data+1,data+1+n,cmp1);
for(i=1;i<=n;i++)
{
data[i].y=i;
}
sort(data+1,data+1+n,cmp);
for(i=1;i<=n;i++)
{
int xu=data[i].id;
ans[xu]=queryhe(1,data[i].y,1,n,1);
update1(data[i].y,1,1,n,1);
}
for(i=1;i<=n;i++)
printf("%d\n",ans[i]);
}
return 0;
}