树状数组支持两种操作:

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. ASP.NET Web – AJAX 回送

    使用UpdatePanel时要一起使用的控件是ScriptManager.ScriptManager类加载了包含几个功能的JavaScript函数.也可以使用这个类加载自己定制脚本.ScriptMan ...

  2. 团队博客作业Week1 Team Homework #3软件工程在北航

    这次我们采访了一位大四的学姐,让她简单地谈了谈去年学习软件工程的经历和感受. 在完成软件工程大作业的过程中,由于计划安排与实际脱节,导致时间前松后紧,平均每周花在这门课上的时间大约有8个小时. 项目完 ...

  3. 将Xml字符串转换成(DataTable || DataSet || XML)对象

    今天用到一个功能:就是把从数据库读出来的内容转换成XML字符串流格式,并输出给一个功能函数.在写的过程,为方便以后的使用,我对这一功能进行分装.该类的具体格式如下:XmlConvert类命名空间:Ni ...

  4. [转载]C# FTP操作工具类

    本文转载自<C# Ftp操作工具类>,仅对原文格式进行了整理. 介绍了几种FTP操作的函数,供后期编程时查阅. 参考一: using System; using System.Collec ...

  5. How to use Android Activity's finish(), onDestory() and System.exit(0) methods

    Activity.finish() Calling this method will let the system know that the programmer wants the current ...

  6. Eclipse中的Web项目自动部署到Tomcat(转)

    转自:http://www.cnblogs.com/ywl925/p/3815173.html 问题: 这里就有个问题,是怎么把Eclipse中的网站项目自动部署到tomcat中.在Eclipse中做 ...

  7. hdu 1754 线段树入门

    线段树点修改  区间最大值查询 #include <cstdio> #include <cstdlib> #include <cmath> #include < ...

  8. Using command-line Subversion to access project source files

    Help index About source code version control with Software Configuration Management (Subversion) Usi ...

  9. Asp.net MVC4.0自定义Html辅助方法

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  10. SDUT1586 计算组合数(组合数)

    这个题数据量小,不容易超时. #include<stdio.h> long long fac(int n) { ; ; i <= n ; i++) { m = i*m; } retu ...