438
技術社區[雲棲]
HDU 1541 樹狀數組
題目意思就是說,給你一張star maps,每個star有自己的level,level是這樣計算的:(Let the level of a star be an amount of the stars that are not higher and not to the right of the given star.)統計這個star左下角star的個數,就是這個star的level。現在要你總計圖中level從0到N-1的star分別有多少個。
如入是按照X Y Y的遞增順序輸入的 Y相同時按照X的遞增順序 所以隻要求X之前小於等於X的星星的個數就可以了 也就是按照X的值建樹
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 32005
int tree[maxn];
int n;
int ans[15005];
inline int Lowbit(int x)
{
return x & (-x);
}
inline void Update(int pos, int val)
{
while (pos <= maxn)
{
tree[pos] += val;
pos += Lowbit(pos);
}
}
inline int Query(int x)
{
int sum = 0;
while (x > 0)
{
sum += tree[x];
x -= Lowbit(x);
}
return sum;
}
int main()
{
int x,y;
while(~scanf("%d",&n))
{
memset(tree,0,sizeof(tree));
memset(ans,0,sizeof(ans));
for(int i=0; i<n; i++)
{
scanf("%d%d",&x,&y);
ans[Query(x+1)]++;
Update(x+1,1);
}
for(int i=0; i<n; i++)
printf("%d\n",ans[i]);
}
return 0;
}
最後更新:2017-04-04 07:03:41