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 ...
随机推荐
- 166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- 表单重置reset
最近一直在做各种页面的“增删改查”,只是在做新增功能的时候,自己一直在使用 reset来清空form表单,原以为这样子的清空方法是万无一失的,可惜最终还是在进行“修改”操作完了之后再“新增”的时候,就 ...
- 在浏览器中将表格导入到本地的EXCEL文件,注意控制内存
if ($export_flag == 1) { $rr = $this->mdl->test($test); header("Content-Type: application ...
- 修改WAMPServer中MySql中文乱码的方法
修改MySql的配置文件my.ini,在[client]段落增加:default-character-set=utf8;在[mysqld]段落增加:character_set_server=utf8; ...
- Struts+Spring+Hibernate整合入门详解
Java 5.0 Struts 2.0.9 Spring 2.0.6 Hibernate 3.2.4 作者: Liu Liu 转载请注明出处 基本概念和典型实用例子. 一.基本概念 St ...
- js基础之ajax
必须搞懂的几个问题: 1.如何创建ajax对象? 2.如何连接服务器? 3.如何发送请求? 4.监控请求状态的事件是什么?分几个阶段?如何获取返回值? 答:onreadystatechange事件:一 ...
- javascript photo http://www.cnblogs.com/5ishare/tag/javascript/
- python 操作json
认识 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - Dece ...
- Windows系统安装时间
http://www.45it.com/windowszh/201206/30693.htm 修改系统安装时间 开始" - "运行" - 输入"regedit& ...
- C#操作Access数据库(创建&修改结构)
本文转自:http://www.cnblogs.com/liyugang/archive/2012/11/17/2775393.html 想要在程序中控制Access,不是数据,而是Access数据库 ...