树状数组-HDU1541-Stars一维树状数组 POJ1195-Mobile phones-二维树状数组
树状数组,学长很早之前讲过,最近才重视起来,enmmmm。。。
树状数组(Binary Indexed Tree(B.I.T), Fenwick Tree)是一个查询和修改复杂度都为log(n)的数据结构。主要用于查询任意两位之间的所有元素之和,但是每次只能修改一个元素的值;经过简单修改可以在log(n)的复杂度下进行范围修改,但是这时只能查询其中一个元素的值(如果加入多个辅助数组则可以实现区间修改与区间查询)。
树状数组和线段树很像,但能用树状数组解决的问题,基本上都能用线段树解决,而线段树能解决的树状数组不一定能解决。相比较而言,树状数组效率要高很多。

int lowbit(int x)
{
return x&(-x);
}
x&(-x)就是整数x与其相反数(负号取反)的按位与:相同位的两个数字都为1,则为1;若有一个不为1,则为0,即:1&1=1,0&1=0,0&0=0;
计算机中负数使用对应正数的补码来表示。
-x就是x对应的二进制数先各位取反,0变成1,1变成0。然后最低位加1。
举个栗子,4对应二进制为100;-4对应的为011+1=100,所以为(100)&(100)所以为100。
知道这个又能干嘛呀,就可以进行区间查询啦。
区间查询利用C[i]求A数组前i个的和;
//代码1:
int SUM(int n)
{
int s=;
while(n>){
s+=c[n];
n-=lowbit(n);
}
return s;
}
//代码2:
int SUM(int n)
{
int s=;
for(int i=n;i>;i-=lowbit(i))
s+=c[i];
return s;
}
两个代码哪个好理解理解哪个。
接着刚刚举的栗子4,求A数组前4个数的和;
lowbit(4)得出100;然后100就是4,所以为s+=c[4];此时i=4,4-lowbit(4)=100-100=0;结束。
再举个栗子7,求前7个数的和,就是s+=A[1]+A[2]+A[3]+A[4]+A[5]+A[6]+A[7];
因为
//代码1:
void add(int x)
{
while(x<=N){
++a[x];
x+=lowbit(x);
}
}
//代码2:
void add(int x,int y)
{
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=y;
}
更新过程仔细想一想就是查询过程的逆过程;
举个栗子,更新A1,还要继续更新C[1],C[2],C[4],C[8];
就是C[001],C[010],C[100],C[1000];
i=1;C[1]+=A[1];
lowbit(1)=(001)&(001)=001;此时i=1,所以为1+lowbit(1)=001+001=010,010就是2;C[2]+=A[1];
lowbit(2)=(010)&(110)=010;此时i=2,所以为2+lowbit(2)=010+010=100,100就是4;C[4]+=A[1];
lowbit(4)=(100)&(100)=100;此时i=4,所以为4+lowbit(4)=100+100=1000,1000就是8;C[8]+=A[1];结束。
好了,理解了树状数组就开始贴代码了。(;´д`)ゞ
HDU1541-Stars
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10611 Accepted Submission(s): 4240

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.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int a[maxn],ans[maxn]; int lowbit(int x)
{
return x&(-x);
} int getsum(int n)
{
int ans=;
for(int i=n;i>;i-=lowbit(i))
ans+=a[i];
return ans;
} void add(int x)
{
for(int i=x;i<=maxn;i+=lowbit(i))
++a[i];
} int main(){
int n,x,y;
while(~scanf("%d",&n)){
memset(a,,sizeof(a));
memset(ans,,sizeof(ans));
for(int i=;i<n;i++){
scanf("%d%d",&x,&y);
x++;
ans[getsum(x)]++;
add(x);
}
for(int i=;i<n;i++)
printf("%d\n",ans[i]);
}
return ;
}
溜了溜了。。。
树状数组-HDU1541-Stars一维树状数组 POJ1195-Mobile phones-二维树状数组的更多相关文章
- poj 1195 Mobile phones(二维树状数组)
树状数组支持两种操作: Add(x, d)操作: 让a[x]增加d. Query(L,R): 计算 a[L]+a[L+1]……a[R]. 当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一 ...
- POJ 1195:Mobile phones 二维树状数组
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16893 Accepted: 7789 De ...
- 【poj1195】Mobile phones(二维树状数组)
题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- Mobile phones_二维树状数组
[题意]给你一个矩阵(初始化为0)和一些操作,1 x y a表示在arr[x][y]加上a,2 l b r t 表示求左上角为(l,b),右下角为(r,t)的矩阵的和. [思路]帮助更好理解树状数组. ...
- poj1195Mobile phones(二维树状数组)
http://poj.org/problem?id=1195 模版题 i写成k了 找了一个多小时没找出来.. #include <iostream> #include<cstring ...
- poj_1195Mobile phones,二维树状数组
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- SCOI2014 bzoj3594 方伯伯的玉米田(二维树状数组+dp)
3594: [Scoi2014]方伯伯的玉米田 Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 1971 Solved: 961[Submit][St ...
- Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Submiss ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
随机推荐
- iOS 友盟推送,应用内推送启动图推送闪动黑屏,插屏推送方法报错
以前都是用的极光推送,应公司需求要求使用友盟推送,为了以后是有分享都适用,,, 友盟推送文档,下载demo 感觉比极光用着要简单顺手 一切就绪后,开始发送消息测试,,,,,搞了半天没有发过来消息 原来 ...
- MySQL datetime的更新,删除网上的一些老概念
网上的老概念 第一点:是以前的MySQL的datetime的最小值是:'1000-01-01 00:00:00'(貌似),但是最新的MySQL测试datetime的最小值可以是:'0000-00-00 ...
- ZooKeeper如何保证单一视图
由于ZooKeeper的数据模型简单且全部在内存中,ZooKeeper的速度非常快.它提供了一系列保证: • 顺序一致性 • 原子性 • 单一视图 • 可靠性 • 实时性 下面将结合源码(3.4.10 ...
- lesson - 9 Linux系统软件包管理
1. rpm工具rpm Redhat Package Manager, 设计理念是开放的,不仅仅是在RedHat平台上,在SUSE上也是可以使用的. rpm包名字构成由-和.分成了若干部分,如abrt ...
- [数据清洗]- Pandas 清洗“脏”数据(三)
预览数据 这次我们使用 Artworks.csv ,我们选取 100 行数据来完成本次内容.具体步骤: 导入 Pandas 读取 csv 数据到 DataFrame(要确保数据已经下载到指定路径) D ...
- MySQL数据库学习: 01 —— 数据库的概述
壹 概述 一 了解SQL 1.1 数据库基础 1.1.1 什么是数据库 数据库(database)保存有组织的数据的容器(通常是一个文件或一组文件). 易混淆:人们常常用“数据库”这个词语来代表他们使 ...
- shell的含义
shell:壳,是操作linux最直接的方式,通过shell中输入命令和linux系统进行交互. shell是一个小盒子,每一个有独立的命名空间,登录后的操作就是一个shell(有可能是bash,zs ...
- [转]Linux网络配置命令ifconfig输出信息解析
eth0 Link encap:Ethernet HWaddr 00:1e:4f:e9:c2:84 inet addr:128.224.163.153 Bcast:128.224.163 ...
- jQuery 遍历函数(八)
函数 描述 .add() 将元素添加到匹配元素的集合中. .andSelf() 把堆栈中之前的元素集添加到当前集合中. .children() 获得匹配元素集合中每个元素的所有子元素. .closes ...
- angular4.0项目build发布后,刷新页面报错404
angular4.0项目执行npm run build后,进入页面正常显示. 但是当刷新页面时,报错404,页面未找到. 出现这个问题的原因,应该是找不到路由地址导致的,然后找到了下面的解决方案. 找 ...