二维树状数组(HD2642)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std; #define BITMAX 1002 //数组大小
typedef int valueType; //元素类型定义
valueType BITree[BITMAX][BITMAX]; //二维树状数组,初始化
/* 2^k k表示节点编号 x 二进制末尾 0 的个数 */
inline int lowbit(int x)
{
return x & (-x);
} /* 节点编号不能为 0 */
/* 二维 C[x][y] = sum(A[i][j]) 其中,x-lowbit[x]+1 <= i<=x 且 y-lowbit[y]+1 <= j <=y */
inline void addPoint(int x, int y, valueType add, int n)
{
for (int i = x; i <= n; i += lowbit(i))
for (int j = y; j <= n; j += lowbit(j))
BITree[i][j] += add;
}
inline valueType readSum(int x, int y)
{
valueType sum = ;
for (int i = x; i > ; i -= lowbit(i))
for (int j = y; j > ; j -= lowbit(j))
sum += BITree[i][j];
return sum;
} bool f[BITMAX][BITMAX];
int main()
{
int m;
char cc[];
int a, b, c, d;
scanf("%d", &m);
memset(BITree, , sizeof(BITree));
memset(f, false, sizeof(f));
while (m--){
scanf("%s", &cc);
switch (cc[])
{
case 'B':
scanf("%d %d", &a, &b);
++a; ++b;
if (f[a][b]) break;
addPoint(a, b, , BITMAX);
f[a][b] = true;
break;
case 'D':
scanf("%d %d", &a, &b);
++a; ++b;
if (f[a][b]) addPoint(a, b, -, BITMAX);
f[a][b] = false;
break;
case 'Q':
scanf("%d %d %d %d", &a, &b, &c, &d);
++a; ++b; ++c; ++d;
if (a > b) swap(a, b);
if (c > d) swap(c, d);
printf("%d\n", readSum(b, d) - readSum(b, c - ) - readSum(a - , d) + readSum(a - , c - ));
break;
default:
break;
}
}
}
二维树状数组(HD2642)的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- 第一个XAML程序
创建win8程序的默认Xaml文件是MainPage.Xaml文件,文件的内容如下所示: <Page x:Class="App1.MainPage" xmlns=" ...
- [转载]ACM(访问控制模型),Security Identifiers(SID),Security Descriptors(安全描述符),ACL(访问控制列表),Access Tokens(访问令牌)
对于<windows核心编程>中的只言片语无法驱散心中的疑惑.就让MSDN中的解释给我们一盏明灯吧.如果要很详细的介绍,还是到MSDN仔细的看吧,我只是大体用容易理解的语言描述一下. wi ...
- slf4j 和 log4j使用案例
以Maven项目为例: 步骤: 1.在Maven的pom.xml文件中添加dependency: 之后就会添加3个jar包: 2.在项目下添加log4j.properties 3.log4j.pro ...
- 浅谈AngularJS启动引导过程
我们都知道AngularJS默认会执行app.js来启动整个angular项目,但你知道angular具体执行过程吗? 一.自动引导启动框架 例如我们有如下代码,我们想要完成一个指令功能: <h ...
- [Linux Kernel]查看CentOS版本方法
查看CentOS版本方法 有以下命令可以查看: # lsb_release -a LSB Version: :core-3.1-ia32:core-3.1-noarch:graphics- ...
- vmware设置centos虚拟机nat联网(转)
今天在vmware虚拟主机中安装hearbeat,为了使用最新的版本,选用编译安装了.在编译过程中,需要连接被墙的网站下载文件,那只能用vpn,但我使用的是桥接方式联网,使用不了真实主机的vpn,于是 ...
- Http和Socket连接
转自http://hi.baidu.com/%D2%B9%D1%A9%B3%E6/blog/item/d6a72d2bbf467cf2e7cd406d.html 相信不少初学手机联网开发的朋友都想知道 ...
- Centos学习
Centos学习 ---恢复内容开始--- Centos学习手册by RuffianFish; 痞子鱼 近日闲的无聊,而最近又在搞Centos决定写个Centos详细的学习手册,以便自己在长时间没摸C ...
- ASP.NET C# 有程序集加不了解决办法
在项目中添加app.config 获取在 web.config 添加 <?xml version="1.0"?> <configuration> <s ...
- Outlook2007、2010和Foxmail的簽名設計
由於個人習慣問題公司大部分人採用第三方郵件工具,對與郵件的通訊設置大家完全可以通過嚮導完成,但是郵件的簽名設計往往隐藏了起来,现在就由我来带大家进行个性签名设计. Outlook2007 第一步: 点 ...