二维树状数组(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)上的数全部 ...
随机推荐
- HDU 3306 - Another kind of Fibonacci
给你 A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2). 求 S(N) = A(0) 2 +A(1) 2+……+ ...
- linux学习笔记之文件类型,及目录介绍
引用A:http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/20/3033131.html 引用B:http://www.cnblogs.c ...
- silverlight中鼠标放在对象的提示事件
1.xaml 中实现 <Rectangle x:Name="toolTip" Grid.Column="0" Grid.Row="1" ...
- HAProxy+Keepalived+PXC负载均衡和高可用的PXC环境
HAProxy介绍 反向代理服务器,支持双机热备支持虚拟主机,但其配置简单,拥有非常不错的服务器健康检查功能,当其代理的后端服务器出现故障, HAProxy会自动将该服务器摘除,故障恢复后再自动将该服 ...
- ASCII码排序,hdu-2000
Problem Description 输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符. Input 输入数据有多组,每组占一行,有三个字符组成,之间无空格. Output ...
- Nutch配置
http://www.linuxidc.com/Linux/2011-12/48782.htm http://wiki.apache.org/nutch/NutchHadoopTutorial htt ...
- NFC通信的模式选择
原帖请参照:http://www.nfcchina.org/forum.php?mod=viewthread&tid=68&extra=page%3D1 1.nfc 怎么选择操作模式的 ...
- extjs让按钮可用或者不可用
Ext.getCmp(‘按钮ID’).enable();//设置为可用Ext.getCmp(‘按钮ID’).disable();//设置为不可用
- Easyui几种布局方式的使用
1.通过标记创建layout. 记得添加"easyui-layout"样式给div标记. <div id="cc" class="easyui ...
- MS Sql 查询数据库连接数
SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID] IN (SELECT [DBID]FROM [Master].[dbo].[SYSDA ...