话说这道题是一道不折不扣的二维树状数组的水题,可是对于我这种菜鸟中的vip来说,却还是纠结了很久很久。。留下做个模板吧。。。。。

题目:

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

ac代码:

#include <iostream>
#include <string.h>
#include <string>
#include <cmath>
#include <cstdio>
int aa[1005][1005];
bool visit[1005][1005];
using namespace std;
#define min(a,b)(a)<(b)?(a):(b);
#define max(a,b)(a)>(b)?(a):(b);
int lowbit(int x){return x&(-x);
}
void update(int x,int y,int num){int t=y;while(x<=1003){y=t;while(y<=1003){aa[x][y]+=num;y+=lowbit(y);}x+=lowbit(x);}
}
int find(int x,int y){int s=0,t=y;while(x>0){y=t;while(y>0){s+=aa[x][y];y-=lowbit(y);}x-=lowbit(x);}return s;
}
int main(){memset(aa,0,sizeof(aa));memset(visit,0,sizeof(visit));int n;scanf("%d",&n);getchar();char chh;int x1,y1,x2,y2;while(n--){scanf("%c",&chh);if(chh=='B'){scanf("%d%d",&x1,&y1);if(visit[x1+1][y1+1]==0){visit[x1+1][y1+1]=1;update(x1+1,y1+1,1);}}if(chh=='Q'){scanf("%d%d%d%d",&x1,&x2,&y1,&y2);int maxx,maxy,minx,miny;maxx=max(x1,x2);minx=min(x1,x2);maxy=max(y1,y2);miny=min(y1,y2);int a=find(maxx+1,maxy+1);int b=find(minx,miny);int c=find(maxx+1,miny);int d=find(minx,maxy+1);printf("%d\n",a+b-c-d);}if(chh=='D'){scanf("%d%d",&x1,&y1);if(visit[x1+1][y1+1]==1){visit[x1+1][y1+1]=0;update(x1+1,y1+1,-1);}}if(n>=1)getchar();}return 0;
}

转载于:https://www.cnblogs.com/javaspring/archive/2011/09/30/2656455.html

hduoj Stars 二维树状数组相关推荐

  1. hdu- 2642 Stars 二维树状数组

    注意树状数组下标 #include<stdio.h> #include<string.h> #include<iostream> #include<algor ...

  2. (二维树状数组)E - Stars

    E - Stars 题意:B表示点亮改点,D表示熄灭,Q查询区间内亮的个数 Sample Input 5 B 581 145 B 581 145 Q 0 600 0 200 D 581 145 Q 0 ...

  3. 二维树状数组 ----2021广东省赛 ----- K - Kera‘s line segment[区间转二维平面+树状数组维护前缀最小最大值]

    题目链接 题目大意: 就是一个一维的数轴上面有一堆线段用一个三元组(l,r,val)(l,r,val)(l,r,val)表示. 现在我们有两个操作: 就是往数轴上面添加线段 询问[L,R][L,R][ ...

  4. szu 寒训第二天 树状数组 二维树状数组详解,以及树状数组扩展应用【求逆序对,以及动态第k小数】

    树状数组(Binary Index Tree) 树状数组可以解决可以转化为前缀和问题的问题 这是一类用以解决动态前缀和的问题 (有点像线段树简版) 1.对于 a1 + a2 + a3 + - + an ...

  5. 【二维树状数组】See you~

    https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F [题意] 给定一个矩阵,每个格子的初始值为1.现在可以对矩阵有四种操作: A ...

  6. POJ_1195 Mobile phones 【二维树状数组】

    题目链接:http://poj.org/problem?id=1195 纯纯的二维树状数组,不解释,仅仅须要注意一点,由于题目中的数组从0開始计算,所以维护的时候须要加1.由于树状数组的下标是不能为1 ...

  7. 洛谷1527(bzoj2738)矩阵乘法——二维树状数组+整体二分

    题目:https://www.luogu.org/problemnew/show/P1527 不难想到(?)可以用二维树状数组.但维护什么?怎么查询是难点. 因为求第k小,可以考虑记权值树状数组,把比 ...

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

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

  9. HDU-4456 Crowd 二维树状数组+坐标转换

    题意:给定一个N*N的网格,现在M组操作,一种操作时改变网格上的某个单点的权值,另外一种操作是求到一点曼哈顿距离为小于等于k的所有的权值和,初始化网格所有点的权值为0. 解法:这题如果没有那些特定的条 ...

最新文章

  1. 如何开启PostGreSQL的远程访问端口?
  2. python --- re模块
  3. [原]我装Solaris的艰苦经历
  4. 使用CMD命令删除文件函数
  5. python模块:命名空间与重载模块
  6. 服务器搭建对讲机系统,世邦对讲配置步骤以及方法(支持跨服务器对讲).doc
  7. java 象棋 人工智能_【揭秘】人工智能大热,缘何不敢挑战中国象棋?
  8. 《未来编年史》——关于地球未来2000年的预言
  9. 1小时学会不打代码制作一个网页精美简历(1)
  10. 网页无法复制文字,一个插件解决问题!!!!
  11. 有趣的符号图画(颜文字)(I have a AC dream)(神兽护体)(保佑你次次Accepted)
  12. HTK中Vocab字典的结构
  13. 解决Firefox3下Flashgot的”AddRef”问题
  14. mysql benchmark tool_使用MYSQL benchmark - DBT2 Benchmark Tool遇到的问题
  15. 2021年上半年最接地气的Android面经,隔壁都馋哭了
  16. PD3.1 140W双C快充解决方案
  17. 劈开迷雾,蘑菇街电商搜索架构及搜索排序实现
  18. c#把word文档转换为html页面
  19. 笔试题之——栈问题(列车车厢编组问题)
  20. DSA签名简介及对其的攻击方式

热门文章

  1. 人人都在讲数据治理而不问业务,这很危险
  2. 帆软finereport决策系统自定义登录界面
  3. flex应用中如何关闭浏览器
  4. 张量/tensor--详解
  5. centos安装最新的visual studio code并设置中文
  6. 国行ps4服务器维护,不能登录其地区的PSN代表什么
  7. 找出1到n的守形数c语言,c语言循环语句训练题(6页)-原创力文档
  8. P11:经典卷积神经网络结构案例分析
  9. 计算机算法设计与分析 N后问题
  10. 算法导论 思考题6-2