POJ2352 Stars

树状数组裸题

题目很给力,告诉我们输入是有序的,所以每个新输入的节点就只会对他之后输入并且x大于他的点造成影响,然后我们就可以树状数组单点加and区间求和就行了

代码

//By AcerMo
#include
#include
#include
#include
#include
#define lowbit(x) x&(-x)
using namespace std;
const int M=200500;
const int h=32050;
int n;
int s[M],ans[M]; 
inline int read()
{
	int x=0;char ch=getchar();
	while (ch>'9'||ch<'0') ch=getchar();
	while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x;
}
inline void add(int p,int x)
{
	for (int i=p;i<=h;i+=lowbit(i)) s[i]+=x;
	return ;
}
inline int query(int p)
{
	int ans=0;
	for (int i=p;i>0;i-=lowbit(i)) ans+=s[i];
	return ans;
}
signed main()
{
	while (~scanf("%d",&n))
	{
		for (int i=1;i<=n;i++)
		{
			int x=read(),y=read();
			add(++x,1);ans[query(x)]++;
		}
		for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
	}
	return 0;
}

 

你可能感兴趣的:(数据结构-树状数组)