Mobile phones_二维树状数组
【题意】给你一个矩阵(初始化为0)和一些操作,1 x y a表示在arr[x][y]加上a,2 l b r t 表示求左上角为(l,b),右下角为(r,t)的矩阵的和。
【思路】帮助更好理解树状数组。
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=;
int c[N][N];
int s;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int y,int a)
{
for(int i=x;i<=s;i+=lowbit(i))
{
for(int j=y;j<=s;j+=lowbit(j))
{
c[i][j]+=a;
}
}
}
int get_sum(int x,int y)
{
int sum=;
for(int i=x;i>;i-=lowbit(i))
{
for(int j=y;j>;j-=lowbit(j))
{
sum+=c[i][j];
}
}
return sum;
}
int main()
{
int ins;
int x,y,a;
int l,b,r,t;
while(scanf("%d",&ins))
{
if(ins==)
{
scanf("%d",&s);
memset(c,,sizeof(c));
}
else if(ins==)
{
scanf("%d%d%d",&x,&y,&a);
update(x+,y+,a);
}
else if(ins==)
{
scanf("%d%d%d%d",&l,&b,&r,&t);
l++,b++,t++,r++;
printf("%d\n",get_sum(r,t)+get_sum(l-,b-)-get_sum(r,b-)-get_sum(l-,t));
}
else break;
}
return ;
}
Mobile phones_二维树状数组的更多相关文章
- 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 ...
- 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: 14489 Accepted: 6735 De ...
- 【POJ1195】【二维树状数组】Mobile phones
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- (简单) POJ 1195 Mobile phones,二维树状数组。
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- POJ 1195 Mobile phones (二维树状数组)
Description Suppose that the fourth generation mobile phone base stations in the Tampere area operat ...
- POJ 1195 Mobile phones【二维树状数组】
<题目链接> 题目大意: 一个由数字构成的大矩阵,开始是全0,能进行两种操作1) 对矩阵里的某个数加上一个整数(可正可负)2) 查询某个子矩阵里所有数字的和要求对每次查询,输出结果 解题分 ...
- POJ_1195 Mobile phones 【二维树状数组】
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u013912596/article/details/33802561 题目链接:id=1195&qu ...
随机推荐
- Hibernate映射之实体映射
1.使用@注解配置实体类 实体类一般有ID.普通属性.集合属性等,分别对应数据库的主键.普通列.外键.@注解配置中,实体类用@Entity注解,用@Table指定对应的数据表,用@Id配置主键,用@C ...
- java arrayCopy
int[] dest = new int[0]; int[] value = {1,2}; if(value != null && value.length>0) { int[] ...
- 继承多态绕点 Java篇
上一篇把C#语言的继承,多态里的特殊的情况做了一下总结,其实那一部分代码都是从Java翻译过去的,今天来总结一下Java在这种情况下是怎么调用的. 上一篇我们说的是:1.多态,只在多态系里方法调用,很 ...
- 继承多态绕点 C#篇
最近在看博客的时候看到一块很绕的地方,有点类似于以前学习C语言是的i++,++i组合到一起使用的情况,很坑b的,绝对会比i++,++i这种情况更有用,虽然实际代码里面确实很少出现. 面对象像三大特点不 ...
- Mysql date_sub函数使用
mysql中内置函数date_add和date_sub能对指定的时间进行增加或减少一个指定的时间间隔,语法如下: DATE_ADD(date,INTERVAL expr type) DATE_SUB( ...
- JDE910笔记2--OMW项目建立及简单使用[转]
1.打开JDE的OBJECT MANAGEMENT WORKBENCH.在工作区中选择ADD,建立项目并选择OMW PROJECT,添加相关信息,如下图所示 其中,ProjectID可以对应不同的数据 ...
- js判断手机系统和微信
//判断手机浏览器 var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OS\s([\d_]+)/), isIphone = !ipa ...
- The APR based Apache Tomcat Native library 异常解决办法
tomat在linux服务器上启动报The APR based Apache Tomcat Native library which allows optimal performance in pro ...
- Future 模式介绍
假设一个任务执行需要花费一些时间,为了省去不必要的等待时间,可以先获取一个提货单,即future,然后继续处理别的任务,知道货物到达,即任务完成得到结果,此时可以使用提货单提货,即通过future得到 ...
- easy_install jinja2 mac linux
error: can't create or remove files in install directory The following error occurred while trying t ...