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 ...
随机推荐
- hdu5879 Cure
题目链接:hdu5879 Cure 题解:用字符串输入.n很大时答案趋近与(π^2)/6. #include<cstdio> #include<algorithm> #incl ...
- enum使用总结
enum的一般使用方法是它会占用最大的成员长度 然后我忘记的是enum还可以这样使用 enum ExctState { START, SUCCEED, FAILURE=6, REJECT, }; 这样 ...
- matlab(数组、矩阵)
- Mysql多表关联删除操作
直接看Sql即可: ;
- 位运算取第一个非0的位 r & (~(r-1))
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- Storm(1) - Setting Up Development Environment
Setting up your development environment 1. download j2se 6 SDK from http://www.oracle.com/technetwor ...
- 转。管理Gearman
通常,Gearman被用来分发任务,以便实现异步操作.下面捋捋如何管理Gearman. 说明:请自行安装好Gearman和PHP PECL Gearman. 准备 我们先来创建一个Worker,实现一 ...
- svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits svynsync:E170009
svn 备份后双机同步热备失效,提示 W200007 target server does not support atomic revision property edits; consider u ...
- FZU 1914 Funny Positive Sequence
题目链接:Funny Positive Sequence 题意:给出一个数列,如果它的前i(1<=i<=n)项和都是正的,那么这个数列是正的,问这个数列的这n种变换里, A(0): a1, ...
- useradd/du/df/passwd/usermod命令
一.useradd命令 useradd命令-M -u -s -g 常用 -c:加上备注文字,备注文字保存在passwd的备注栏中. -d:指定用户登入时的启始目录. -D:变更预设值.(修改默认配置 ...