标签:des com http blog class style div img code java size
Time Limit: 2000/1000 MS
(Java/Others) Memory Limit: 65536/32768 K
(Java/Others)
Total Submission(s): 4052 Accepted
Submission(s): 1592
int lowbit(int x)
{
return x & -x;
}
int sum(int a[],int x) //求出第x个元素之前的和
{
int ans = 0;
while(x>0){
ans+=a[x];
x -= lowbit(x); //向左上爬
}
return ans;
}
void add(int a[],int x,int d,int n) //将编号为x的数加d
{
while(x<=n){
a[x]+=d;
x+=lowbit(x);
}
}
代码:
1 #include <stdio.h>
2 #include <iostream>
3 using namespace std;
4 int lowbit(int x)
5 {
6 return x & -x;
7 }
8 int sum(int a[],int x) //求出第x个元素之前的和
9 {
10 int ans = 0;
11 while(x>0){
12 ans+=a[x];
13 x -= lowbit(x); //向左上爬
14 }
15 return ans;
16 }
17 void add(int a[],int x,int d,int n) //将编号为x的数加d
18 {
19 while(x<=n){
20 a[x]+=d;
21 x+=lowbit(x);
22 }
23 }
24 int main()
25 {
26 int i,n;
27 while(scanf("%d",&n)!=EOF){
28 int level[15010]={0};
29 int x[32010]={0};
30 for(i=1;i<=n;i++){ //输入,计算当前星星前面的总星数(level),对应等级值+1,更新x[]数组
31 int a,b,num=0;
32 scanf("%d%d",&a,&b);
33 level[sum(x,a+1)]++;
34 add(x,a+1,1,32010); //这个位置的星星数+1
35 }
36 for(i=0;i<n;i++) //输出
37 printf("%d\n",level[i]);
38 }
39 return 0;
40 }
Freecode : www.cnblogs.com/yym2013
hdu 1541/poj 2352:Stars(树状数组,经典题),布布扣,bubuko.com
hdu 1541/poj 2352:Stars(树状数组,经典题)
标签:des com http blog class style div img code java size
原文地址:http://www.cnblogs.com/yym2013/p/3697288.html