树状数组模板+poj1195(二维树状数组)

2023-11-09 15:08

本文主要是介绍树状数组模板+poj1195(二维树状数组),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

感谢学长的博客~~http://blog.csdn.net/lin375691011/article/details/21247409

在数组长度为n的树状数组中:

寻找下一个需要添加的数的下标:

int lowbit(int x)  
{  return x&(-x);  
}  
一维树状数组更新是这样的:

void add(int x,int val)  
{  for(;x<=n;x+=lowbit(x))  {  num[x]+=val;  }  
}  
二维树状数组更新是这样的:

void  add(int x,int y,int val)  
{  for(int i=x;i<=s;i+=lowbit(i))  {  for(int j=y;j<=s;j+=lowbit(j))  {  num[i][j]+=val;  }  }  
}  

一维树状数组查询是这样的:

int query(int x)  
{  int ans=0;  for(;x>0;x-=lowbit(x))  {  ans+=c[i];  }  return ans;  
}  
二维树状数组查询是这样的

int query(int x,int y)  
{  int ans=0;  for(int i=x;i>0;i-=lowbit(i))  {  for(int j=y;j>0;j-=lowbit(j))  {  ans+=num[i][j];  }  }  return ans;  
}  

下面看一个纯二维的树状数组模板题


Mobile phones
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 18744 Accepted: 8647

Description

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area. 

Input

The input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3. 

Table size: 1 * 1 <= S * S <= 1024 * 1024 
Cell value V at any time: 0 <= V <= 32767 
Update amount: -32768 <= A <= 32767 
No of instructions in input: 3 <= U <= 60002 
Maximum number of phones in the whole table: M= 2^30 

Output

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

0 4
1 1 2 3
2 0 0 2 2 
1 1 1 2
1 1 2 -1
2 1 1 2 3 
3

Sample Output

3
4

Source

题目大意:

给定矩阵大小,可以更新矩阵中的某些数,要求输出某个范围内的所有数之和。

#include<stdio.h>
#include<string.h>
#define lowbit(x) x&(-x)
#define maxn 1025
int c[maxn][maxn];
int n;
void add(int x,int y,int a)
{for( int i = x; i <=n; i += lowbit(i)){for( int j = y; j <=n; j += lowbit(j)){c[i][j]+=a;}}
}
int sum( int l,int r )
{int ans = 0;for( int i = l; i > 0; i -= lowbit(i) ){for( int j = r; j > 0; j -=lowbit(j)){ans+=c[i][j];}}return ans;
}
int main()
{int i,j,k;scanf("%d%d",&k,&n);memset(c,0,sizeof(c));int op;while(~scanf("%d",&op)){if(op>=3) break;if(op == 1){int x,y,a;scanf("%d%d%d",&x,&y,&a);add(x+1,y+1,a);}else if(op == 2){int l,r,b,t;scanf("%d%d%d%d",&l,&b,&r,&t);l++,b++,r++,t++;printf("%d\n",sum(r,t) - sum(r,b-1) -  sum(l-1, t) + sum(l-1, b-1));}}
}








这篇关于树状数组模板+poj1195(二维树状数组)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/376868

相关文章

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

uva 1342 欧拉定理(计算几何模板)

题意: 给几个点,把这几个点用直线连起来,求这些直线把平面分成了几个。 解析: 欧拉定理: 顶点数 + 面数 - 边数= 2。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#inc

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al