Stars

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)
Total Submission(s): 1628    Accepted Submission(s):
683

Problem Description
Yifenfei is a romantic guy and he likes to count the
stars in the sky.
To make the problem easier,we considerate the sky is a
two-dimension plane.Sometimes the star will be bright and sometimes the star
will be dim.At first,there is no bright star in the sky,then some information
will be given as "B x y" where 'B' represent bright and x represent the X
coordinate and y represent the Y coordinate means the star at (x,y) is
bright,And the 'D' in "D x y" mean the star at(x,y) is dim.When get a query as
"Q X1 X2 Y1 Y2",you should tell Yifenfei how many bright stars there are in the
region correspond X1,X2,Y1,Y2.

There is only one case.

 
Input
The first line contain a M(M <= 100000), then M line
followed.
each line start with a operational character.
if the character
is B or D,then two integer X,Y (0 <=X,Y<= 1000)followed.
if the
character is Q then four integer X1,X2,Y1,Y2(0 <=X1,X2,Y1,Y2<= 1000)
followed.
 
Output
For each query,output the number of bright stars in one
line.
 
Sample Input
5
B 581 145
B 581 145
Q 0 600 0 200
D 581 145
Q 0 600 0 200
 
Sample Output
1
0
 
初学树状数组,看了一些文章,算是有了初步的认识。
自己的理解:用二进制表示的方法来实现二分。一个二进制数c[k]表示的是从a[k]开始往前的lowbit(k)个数的和(lowbit(k)为k的最低位,也即将k的除最低位外的所有为置为0)。
更新:
void add(int k,int x)
{
for(int i=k;i<MAXN;i+=lowbit(i))
c[i]+=x;
}

lowbit():

int lowbit(int x)   //取最低位
{
return x&(-x);
}

查询:

int get_sum(int k)
{
int res=;
for(int i=k;i>;i-=lowbit(i))
res+=c[i];
return res;
}

这道题是一道二维树状数组,原理其实也就是这样。

注意:题目中坐标从0开始,可能对一颗star做两次同样的操作。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
#include<stdlib.h>
#include<stack>
#include<vector>
using namespace std; const int MAXN=;
int a[MAXN][MAXN];
bool b[MAXN][MAXN]; int lowbit(int x)
{
return x&(-x);
} void modify(int x,int y,int data)
{
for(int i=x; i<MAXN; i+=lowbit(i))
for(int j=y; j<MAXN; j+=lowbit(j))
a[i][j]+=data;
} int getsum(int x,int y)
{
int res=;
for(int i=x; i>; i-=lowbit(i))
for(int j=y; j>; j-=lowbit(j))
res+=a[i][j];
return res;
} int main()
{
int n,x,y,x1,y1;
char str[];
memset(a,,sizeof(a));
memset(b,,sizeof(b));
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
if(str[]=='B')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]) continue;
modify(x,y,);
b[x][y]=;
}
else if(str[]=='D')
{
scanf("%d%d",&x,&y);
x++;
y++;
if(b[x][y]==) continue;
modify(x,y,-);
b[x][y]=;
}
else
{
scanf("%d%d%d%d",&x,&x1,&y,&y1);
x++;x1++;y++;y1++;
if(x>x1) swap(x,x1);
if(y>y1) swap(y,y1);
int ans=getsum(x1,y1)-getsum(x-,y1)-getsum(x1,y-)+getsum(x-,y-);
printf("%d\n",ans);
}
}
return ;
}

HDU_2642_二维树状数组的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  4. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  5. 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 ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  8. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

随机推荐

  1. 切换到android studio遇到的svn问题

    眼下的android studio 2.0(包括preview版)都已经把git.svn,cvs 等工具集成进来了.所以,我们仅仅须要依据代码server选择使用就可以. 这里以开源中国上的git私有 ...

  2. 阿里2016实习offer五面经验与总结

    前言 眼下楼主已经拿到阿里实习offer,一共经历了5次面试,当中4轮技术面.1轮HR面试.在这里分享一下自己的面试经验和学习总结.写这篇面经主要是希望可以帮助很多其它的小伙伴.我本科毕业于中南大学信 ...

  3. 寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器

    之前总结过 Swift 的语言攻略,这里就不做赘述了,如今做一个实例计算器项目来介绍一下 Swift 的应用.(凝视已经全然.直接上代码) 先看一下效果图: 以下是详细的代码和解释: 分享快乐.开源中 ...

  4. c# POST和GET方式通过server地址提交数据

    1:POST方式提交: <strong><span style="font-size:14px;">private static string HttpPo ...

  5. LeetCode OJ 之 Valid Anagram

    题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...

  6. Openwrt中luci配置页面cbi小记

    先看看network的配置文件: config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr  ...

  7. Photon + Unity3D 线上游戏开发 学习笔记(四)

    这一节 我们建立 photon Server 端的框架 一个最简单的Photon框架 就包括一个 Applocation 类 和 一个 peer 类,作用例如以下: *  Application 类是 ...

  8. Dubbo实战(一)高速入门

    Dubbo是什么? Dubbo[]是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案. 其核心部分包含: 远程通讯: 提供对多种基于长连接的NIO框架抽象封 ...

  9. 粗结MySql数据库基础知识点之一

    首先弄什么是数据库? 数据库就是用来存储和管理数据的仓库. 数据库存储数据的优点: 1.可存储大量的数据  2.方便检索  3.保持数据的一致性,完整性  4.安全 可共享  5.通过组合分析,可以产 ...

  10. [POI 2008] BLO

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1123 [算法] 首先,如果一个点不是割点,那么,去掉该点后不连通的有序点对就为 : ...