树状数组支持两种操作:

Add(x, d)操作:   让a[x]增加d。

Query(L,R): 计算 a[L]+a[L+1]……a[R]。

当要频繁的对数组元素进行修改,同时又要频繁的查询数组内任一区间元素之和的时候,可以考虑使用树状数组. 
通常对一维数组最直接的算法可以在O(1)时间内完成一次修改,但是需要O(n)时间来进行一次查询.而树状数组的修改和查询均可在O(log(n))的时间内完成.

在二维情况下:数组A[][]的树状数组定义为:

C[x][y] = ∑ a[i][j], 其中, 
    x-lowbit(x) + 1 <= i <= x, 
    y-lowbit(y) + 1 <= j <= y.

学习网站:http://www.java3z.com/cwbwebhome/article/article1/1369.html?id=4804

题目:http://poj.org/problem?id=1195

题意:输入3的时候,结束。输入1对(x,y)加A....

输入3的时候,输出子矩阵从(L,B)到 (R,T)的和。。。

题目下标是从0开始的,所有要加1.。。

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = ;
int n,c[maxn][maxn]; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int d)
{
int i,j;
for(i = x; i <= n; i += lowbit(i))
for(j = y; j <= n; j += lowbit(j))
c[i][j] += d;
}
int sum(int x,int y)
{
int i,j,ret = ;
for(i = x; i > ; i -=lowbit(i))
for(j = y; j > ; j -= lowbit(j))
ret += c[i][j];
return ret;
} int main()
{
int X,Y,A,t;
int L,B,R,T;
while(cin>>T)
{
cin>>n;
memset(c,,sizeof());
while(cin>>t)
{
if(t == )break;
else if(t == )
{
scanf("%d%d%d",&X,&Y,&A);
add(X + ,Y + ,A);
}
else if(t == )
{
scanf("%d%d%d%d",&L,&B,&R,&T);
printf("%d\n",sum(R+,T+)-sum(R+,B)-sum(L,T+)+sum(L,B));
}
}
}
return ;
}

模板:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = ;
int n,c[maxn][maxn]; int lowbit(int x)
{
return x&(-x);
}
void add(int x,int y,int d)
{
int i,j;
for(i = x; i <= n; i += lowbit(i))
for(j = y; j <= n; j += lowbit(j))
c[i][j] += d;
}
int sum(int x,int y)
{
int i,j,ret = ;
for(i = x; i > ; i -=lowbit(i))
for(j = y; j > ; j -= lowbit(j))
ret += c[i][j];
return ret;
} int main()
{
int i,j,x;
int a,b,c,d;
cin>>n;
for(i = ; i <= n; i++)
{
for(j = ; j <= n; j++)
{
cin>>x;
add(i,j,x);
}
} while()
{
cin>>a>>b>>c>>d;
cout<<(sum(c,d) - sum(c,b-) - sum(a-,d) + sum(a-,b-))<<endl;
} return ;
}

poj 1195 Mobile phones(二维树状数组)的更多相关文章

  1. POJ 1195:Mobile phones 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 16893   Accepted: 7789 De ...

  2. 【poj1195】Mobile phones(二维树状数组)

    题目链接:http://poj.org/problem?id=1195 [题意] 给出一个全0的矩阵,然后一些操作 0 S:初始化矩阵,维数是S*S,值全为0,这个操作只有最开始出现一次 1 X Y ...

  3. POJ 2155 Matrix【二维树状数组+YY(区间计数)】

    题目链接:http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  4. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  5. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  6. POJ 2155 Matrix 【二维树状数组】(二维单点查询经典题)

    <题目链接> 题目大意: 给出一个初始值全为0的矩阵,对其进行两个操作. 1.给出一个子矩阵的左上角和右上角坐标,这两个坐标所代表的矩阵内0变成1,1变成0. 2.查询某个坐标的点的值. ...

  7. POJ 2155 Matrix (二维树状数组)题解

    思路: 没想到二维树状数组和一维的比只差了一行,update单点更新,query求和 这里的函数用法和平时不一样,query直接算出来就是某点的值,怎么做到的呢? 我们在更新的时候不止更新一个点,而是 ...

  8. POJ 2155:Matrix 二维树状数组

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 21757   Accepted: 8141 Descripti ...

  9. POJ 2155 Matrix(二维树状数组)

    与以往不同的是,这个树状数组是二维的,仅此而已 #include <iostream> #include <cstdio> #include <cstring> # ...

随机推荐

  1. Spiral Matrix II

    Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...

  2. Interview-Increasing Sequence with Length 3.

    Given an array, determine whether there are three elements A[i],A[j],A[k], such that A[i]<A[j]< ...

  3. 父<IFRAME>获取子页属性以及子页中<IFRAME>的方法

    例子如下: 1.父页index.jsp <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "ht ...

  4. c++ 哪些自定义的数据类型

    http://www.cnblogs.com/ShaneZhang/archive/2013/06/21/3147648.html 这些数据类型是 C99 中定义的,具体定义在:/usr/includ ...

  5. hadoop 技巧

    通过--config指定不同的集群 bin/hadoop --config ./conf_time/ dfs -ls /user/rd/*/for_*/ip_table/output/ rd下是都读写 ...

  6. ASP文件操作(FSO)详解

    实例一:写入文件 语法 object.CreateTextFile([要建立的文件],[如存在,是否替代]) <% Set fs =Server.CreateObject("Scrip ...

  7. android 关于InputDispatcher出现Consumer错误的解决办法

    原地址:http://www.educity.cn/wenda/158744.html android 关于InputDispatcher出现Consumer异常的解决方法10-23 03:24:46 ...

  8. Follow Path -》 Unity3d通用脚本

    PathDefinition.cs using UnityEngine; using System.Collections; using System.Collections.Generic; usi ...

  9. Unity3D角色攻击范围判定和攻击判定

    原地址:http://www.unity蛮牛.com/blog-1801-479.html 第一种方法:运用点乘 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  10. Sublime Text 3配置与vim模式(待完整)

    Sublime Text 3通过设置默认值与用户值的方式,来进行配置.默认值不允许更改,用户值是用户进行配置.同一属性,当用户值存在时,默认值就无效.打开Preference,如图: 先贴下我的Set ...