当前位置: 代码迷 >> 综合 >> poj-2352-Stars-线段树
  详细解决方案

poj-2352-Stars-线段树

热度:28   发布时间:2023-12-19 10:50:16.0

很裸的单点更新线段树

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
#define maxn 110000
struct list
{int l,r;int x;
}node[maxn*6];
struct listt
{int x,y;bool friend operator < (const listt a, const listt b){if(a.x!=b.x)return a.x<b.x;return a.y<b.y;}
}point[maxn];
void build(int l,int r,int st)
{node[st].l=l;node[st].r=r;node[st].x=0;if(l!=r){build(l,(l+r)/2,st*2);build((l+r)/2+1,r,st*2+1);}
}
void insert(int x,int st)
{int l=node[st].l;int r=node[st].r;if(l>x||r<x)return;if(l==r){node[st].x++;return ;}insert(x,st*2);insert(x,st*2+1);node[st].x=node[st*2].x+node[st*2+1].x;
}
int search(int l,int r,int st)
{int ll=node[st].l;int rr=node[st].r;if(ll>r||rr<l)return 0;if(l<=ll&&rr<=r)return node[st].x;return search(l,r,st*2)+search(l,r,st*2+1);
}
int lvl[maxn];
int main()
{int i,n;while(~scanf("%d",&n)){memset(lvl,0,sizeof(lvl));for(i=0;i<n;i++){scanf("%d%d",&point[i].x,&point[i].y);}sort(point,point+n);build(0,40000,1);for(i=0;i<n;i++){int t=search(0,point[i].y,1);lvl[t]++;insert(point[i].y,1);}for(i=0;i<=n-1;i++){printf("%d\n",lvl[i]);}}return 0;
}