思路:用线段树维护一下每一个横坐标出现的次数。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=15000+10;
struct node
{
int x;
int y;
}q[MAXN<<2];
int sum[MAXN<<4];
int cnt[MAXN<<4];
int cmp(struct node a,struct node b)
{
if(a.y!=b.y)
return a.y<b.y;
else if(a.y==b.y)
return a.x<b.x;
}
void PushUp(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void Update(int p,int l,int r,int rt)
{
if(l==r){
sum[rt]++;
return ;
}
int mid=(l+r)>>1;
if(p<=mid)
Update(p,lson);
else
Update(p,rson);
PushUp(rt);
}
int Query(int ll,int rr,int l,int r,int rt)
{
if(ll<=l&&rr>=r){
return sum[rt];
}
int mid=(l+r)>>1;
int ans=0;
if(ll<=mid)
ans+=Query(ll,rr,lson);
if(rr>mid)
ans+=Query(ll,rr,rson);
return ans;
}
int main()
{
int n,i;
scanf("%d",&n);
int maxx=-inf;
memset(sum,0,sizeof(sum));
for(i=0;i<n;i++){
scanf("%d %d",&q[i].x,&q[i].y);
maxx=max(maxx,q[i].x);
}
sort(q,q+n,cmp);
memset(cnt,0,sizeof(cnt));
int ans;
for(i=0;i<n;i++){
ans=Query(0,q[i].x,0,maxx,1);
Update(q[i].x,0,maxx,1);
cnt[ans]++;
}
for(i=0;i<n;i++){
printf("%d\n",cnt[i]);
}
return 0;
}