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)。
更新:
  1. void add(int k,int x)
  2. {
  3. for(int i=k;i<MAXN;i+=lowbit(i))
  4. c[i]+=x;
  5. }

lowbit():

  1. int lowbit(int x) //取最低位
  2. {
  3. return x&(-x);
  4. }

查询:

  1. int get_sum(int k)
  2. {
  3. int res=;
  4. for(int i=k;i>;i-=lowbit(i))
  5. res+=c[i];
  6. return res;
  7. }

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<queue>
  6. #include<algorithm>
  7. #include<stdlib.h>
  8. #include<stack>
  9. #include<vector>
  10. using namespace std;
  11.  
  12. const int MAXN=;
  13. int a[MAXN][MAXN];
  14. bool b[MAXN][MAXN];
  15.  
  16. int lowbit(int x)
  17. {
  18. return x&(-x);
  19. }
  20.  
  21. void modify(int x,int y,int data)
  22. {
  23. for(int i=x; i<MAXN; i+=lowbit(i))
  24. for(int j=y; j<MAXN; j+=lowbit(j))
  25. a[i][j]+=data;
  26. }
  27.  
  28. int getsum(int x,int y)
  29. {
  30. int res=;
  31. for(int i=x; i>; i-=lowbit(i))
  32. for(int j=y; j>; j-=lowbit(j))
  33. res+=a[i][j];
  34. return res;
  35. }
  36.  
  37. int main()
  38. {
  39. int n,x,y,x1,y1;
  40. char str[];
  41. memset(a,,sizeof(a));
  42. memset(b,,sizeof(b));
  43. scanf("%d",&n);
  44. while(n--)
  45. {
  46. scanf("%s",str);
  47. if(str[]=='B')
  48. {
  49. scanf("%d%d",&x,&y);
  50. x++;
  51. y++;
  52. if(b[x][y]) continue;
  53. modify(x,y,);
  54. b[x][y]=;
  55. }
  56. else if(str[]=='D')
  57. {
  58. scanf("%d%d",&x,&y);
  59. x++;
  60. y++;
  61. if(b[x][y]==) continue;
  62. modify(x,y,-);
  63. b[x][y]=;
  64. }
  65. else
  66. {
  67. scanf("%d%d%d%d",&x,&x1,&y,&y1);
  68. x++;x1++;y++;y1++;
  69. if(x>x1) swap(x,x1);
  70. if(y>y1) swap(y,y1);
  71. int ans=getsum(x1,y1)-getsum(x-,y1)-getsum(x1,y-)+getsum(x-,y-);
  72. printf("%d\n",ans);
  73. }
  74. }
  75. return ;
  76. }

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. Debug : array type has incomplete element type

    array type has incomplete element type extern   struct  SoundReport SoundList[32];     ///// 多写了  st ...

  2. 【ACM】hdu_zs3_1008_Train Problem I_201308100835

    Train Problem I Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Tota ...

  3. [bzoj2654]tree_二分_kruskal

    tree bzoj-2654 题目大意:给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树.题目保证有解. 注释:$1\le V\le 5\cdot 10^4 ...

  4. 络谷 P1363 幻想迷宫

    P1363 幻想迷宫 题目描述 背景 Background (喵星人LHX和WD同心协力击退了汪星人的入侵,不幸的是,汪星人撤退之前给它们制造了一片幻象迷宫.) WD:呜呜,肿么办啊…… LHX:mo ...

  5. propagation属性的7个传播行为

    关于propagation属性的7个传播行为 REQUIRED:指定当前方法必需在事务环境中运行,如果当前有事务环境就加入当前正在执行的事务环境,如果当前没有事务,就新建一个事务.这是默认值.即有事务 ...

  6. Retrofit网络框架入门使用

    1.简单介绍 retrofit事实上就是对okhttp做了进一步一层封装优化. 我们仅仅须要通过简单的配置就能使用retrofit来进行网络请求了. Retrofit能够直接返回Bean对象,比如假设 ...

  7. 【转】storm 开发系列一 第一个程序

    原文: http://blog.csdn.net/csfreebird/article/details/49104777 --------------------------------------- ...

  8. js 推断字符串是否包括某字符串

    var Cts = "bblText"; if(Cts.indexOf("Text") > 0 ) { alert('Cts中包括Text字符串'); } ...

  9. Struts 配置文件

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="htt ...

  10. B1800 [Ahoi2009]fly 飞行棋 数学模拟

    20分钟一遍AC,大水题,我的算法比较复杂,但是好理解,就是找可以凑出来一半周长的点来暴力枚举就行了. 题干: Description 给出圆周上的若干个点,已知点与点之间的弧长,其值均为正整数,并依 ...