区间信息的查询与维护(一)树状树组
来源:互联网 发布:康熙王朝 知乎 编辑:程序博客网 时间:2024/06/05 23:03
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 3Sample 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
Description
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
- 区间信息的查询与维护(一)树状树组
- 区间信息的维护与查询1(树状数组)
- 【转载】区间信息的维护与查询(一)——二叉索引树(Fenwick树、树状数组)
- 区间信息的维护和查询系列算法-树状数组
- 区间信息的维护与查询
- 区间信息的维护与查询_1 2016.4.24
- Splay 区间信息的维护与查询 静态模板
- 区间信息维护与查询 2016.10.13
- 树状数组区间维护及单点查询
- 【小结】树状数组的区间修改与区间查询
- 树状数组的区间修改与区间查询
- 树状数组的应用(区间修改+区间查询)
- 数据结构----树状数组----二维区间的修改与查询
- 树状数组(区间更新区间查询)
- poj3468 树状数组解法(树状数组维护区间更新)
- 树状数组的应用(区间修改,区间查询,多维树状数组)
- poj3468-树状数组 一维 区间更新 区间查询
- codevs 1082 一维树状数组区间修改区间查询
- 分享一个类似QQ屏保解锁的代码类
- SVN钩子(hooks)的经典使用
- 西安商务快巴年底开通 空调车取消K字标以作区别
- 有关路径的详细的说明
- 那两年炼就的Android内功修养 ( 老罗 )
- 区间信息的查询与维护(一)树状树组
- 【分享】一位技术人员成长历程
- win8下找到计算器并转换为程序员模式
- asp.net 生成随机密码的具体代码
- js来获取嵌套的子页面iframe里面的html和form表单中元素值
- 字典树(Trie查找单词再也不用担心超时了)
- Android 移植oprofil性能监测工具
- ZOJ 3687
- usaco 4.3.4 letter game