int lowbit(int x)
{
return x&-x;
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
}
其中 x-lowbit(x)代表右子节点的父节点,x+lowbit(x)代表左子节点的父节点。
sum(i)用于求前i项的和,add(int x,int d)用于修改节点信息。
它的修改与求和都是O(logn)的,效率非常高。
典型应用
例题一
LA 4392 POJ
Description
Input
Every test case consists of N + 1 integers. The first integer is N, the number of players. Then N distinct integers a1, a2 ... aN follow, indicating the skill rank of each player, in the order of west to east. (1 <= ai <= 100000, i = 1 ... N).
Output
Sample Input
1 3 1 2 3 Sample Output
1
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int max_a=100000+1000;
const int max_num=20000+200;
typedef long long LL;
int a[20000+200];
LL cc[20000+200],dd[20000+200];
int n=max_a,C[max_a];
int sum(int x) {
int ret = 0;
while ( x>0 ) {
ret += C[x];
x -= lowbit(x);
}
return ret;
}
void add(int x, int d) {
while(x <= n) {
C[x] += d;
x += lowbit(x);
}
}
int main()
{
//freopen("input.txt","r",stdin);
int T;cin>>T;
int num;
for(int tt=1;tt<=T;tt++) {
scanf("%d",&num);
for(int i=0;i<num;i++)
scanf("%d",&a[i]);
memset(C,0,sizeof(C));
for(int i=0;i<num;i++) {
add(a[i],1);
cc[i]=sum(a[i])-1;//cout<<cc[i]<<endl;
}
memset(C,0,sizeof(C));
for(int i=num-1;i>=0;i--) {
add(a[i],1);
dd[i]=sum(a[i])-1;//cout<<cc[i]<<endl;
}
long long ans=0;
for(int i=1;i<num-1;i++) {
ans+=(cc[i])*(num-(1+i)-dd[i])+
(i-cc[i])*(dd[i]);
}
cout<<ans<<endl;
}
return 0;
}
其中
for(int i=0;i<num;i++) {
add(a[i],1);
cc[i]=sum(a[i])-1;//cout<<cc[i]<<endl;
}
可以求出比a[i]小的数的个数
for(int i=num-1;i>=0;i--) {
add(a[i],1);
dd[i]=sum(a[i])-1;//cout<<cc[i]<<endl;
}
可以求出比a[i]大的数的个数
例题二
POJ2352
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 27339 | Accepted: 11959 |
Description
![](http://p.qpimg.cn/cgi-bin/cgi_imgproxy?url=http%3A%2F%2Fpoj.org%2Fimages%2F2352_1.jpg&size=0)
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
12
1
1
0
题目大意
在一个坐标系内,给若干个点,每个点给定x、y坐标,定义点p的level为横纵坐标均不超过点p的点的个数,输出每个level包含的点的数量。
解题思路
这道题目和计数有关,是典型的树状数组应用的题目。题目给的数据按照y排序,相同的y按照x排序,这就给解题带来了很大的方便,我们可以按照由下至上、由左至右的顺序来处理数据,这样我们只利用x就可以进行计数了,因为当我们处理到任意点p的时候,所有的y小于p的点我们已经计数过了,只需要统计所有x不超过p的点的数量就可以了,而这一步利用树状数组强大的计数功能实现。
sum[i]表示x不大于i的点的数目,每次统计完i的数目后,要把sum[i]计数增加1。
代码
#include<stdio.h>
#include<string.h>
const int n=50000;
int c[50000];
int lowbit(int x)
{
return x&-x;
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
}
void main()
{
int m,cc[50000],x,y,i;
while(scanf("%d",&m)!=EOF)
{
memset(cc,0,sizeof(cc));
memset(c,0,sizeof(c));
for( i=0;i<m;i++)
{
scanf("%d%d",&x,&y);
cc[sum(x+1)]++;
add(x+1,1);
}
for(i=0;i<m;i++)
printf("%d\n",cc[i]);
}
}
例三
HDU 1556
Color the ball
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6279 Accepted Submission(s): 3334
当N = 0,输入结束。
#include<stdio.h>
#include<string.h>
const int n=100000;
int c[100000];
int lowbit(int x)
{
return x&-x;
}
int sum(int x)
{
int ret=0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int d)
{
while(x<=n)
{
c[x]+=d;
x+=lowbit(x);
}
}
void main()
{
int n1,n,x,y,i;
while(scanf("%d",&n),n)
{
memset(c,0,sizeof(c));
n1=n;
while(n--)
{
scanf("%d%d",&x,&y);
add(x,1);
add(y+1,-1);
}
for(i=1;i<n1;i++)
{
printf("%d ",sum(i));
}
printf("%d\n",sum(i));
}
}
本来主函数我是这样写的:
void main()
{
int n1,n,x,y,i; while(scanf("%d",&n),n)
{
memset(c,0,sizeof(c));
n1=n;
while(n--)
{
scanf("%d%d",&x,&y);
for(i=x;i<=y;i++)
add(i,1);
}
for(i=1;i<n1;i++)
{
printf("%d ",sum(i)-sum(i-1));
}
printf("%d\n",sum(i)-sum(i-1));
}
}
但是出现了超时的情况
Time Limit Exceeded
而
add(x,1);
add(y+1,-1);
可以代替for循环,是一个不错的技巧,嘿嘿,直接降到了390MS