HDUOJ---2642Stars(二维树状数组)
Stars
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 975 Accepted Submission(s): 420
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
Author
teddy
Source
树状数组,用到二维,当时总的的来说,还算简单。。。
题目要求求矩形里星星中的个数//
给定某两个坐标对角坐标,球该矩形的星星个数..
对于x1,x2,y1,y2.。我们不知道其大小,所以需要进行比较
得到大小之后,我们就可以求该巨型的了,像下面的图一样..
由此贴出代码吧:
1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 #define maxn 1005
5 #define lowbit(x) ((x)&(-x))
6 int aa[maxn][maxn];
7 bool bb[maxn][maxn];
8
9 void ope(int x,int y,int val)
10 {
11 int j;
12 if(val==1)
13 {
14 if(bb[x][y]) return ;
15 bb[x][y]=true;
16 }
17 else
18 {
19 if(bb[x][y]==false)
20 return ;
21 bb[x][y]=false;
22 }
23 while(x<maxn){
24 j=y;
25 while(j<maxn){
26 aa[x][j]+=val;
27 j+=lowbit(j);
28 }
29 x+=lowbit(x);
30 }
31 }
32 int sum(int x,int y)
33 {
34 int ans=0 ,j;
35 while(x>0){
36 j=y;
37 while(j>0){
38 ans+=aa[x][j];
39 j-=lowbit(j);
40 }
41 x-=lowbit(x);
42 }
43 return ans;
44 }
45 struct node
46 {
47 int x;
48 int y;
49 };
50 int main()
51 {
52 int test,res;
53 char str[2];
54 node a,b;
55 memset(aa,0,sizeof(aa));
56 memset(bb,0,sizeof(bb));
57 scanf("%d",&test);
58 while(test--)
59 {
60 scanf("%s",str);
61 if(str[0]=='Q')
62 {
63 scanf("%d%d%d%d",&a.x,&b.x,&a.y,&b.y);
64 if(a.x>b.x){
65 a.x^=b.x;
66 b.x^=a.x;
67 a.x^=b.x;
68 }
69 if(a.y>b.y){
70 a.y^=b.y;
71 b.y^=a.y;
72 a.y^=b.y;
73 }
74 b.x++;
75 b.y++;
76 res=sum(b.x,b.y)-sum(a.x,b.y)+sum(a.x,a.y)-sum(b.x,a.y);
77 printf("%dn",res);
78 }
79 else
80 {
81 scanf("%d%d",&a.x,&a.y);
82 a.x++; //ÓÒÒÆһλ
83 a.y++;
84 if(str[0]=='B')
85 ope(a.x,a.y,1);
86 else
87 ope(a.x,a.y,-1);
88 }
89 }
90 return 0;
91 }
- 我的第一次ChIP-seq实践
- SparkStreaming窗口操作
- 史上最全Git使用手册
- 我所理解的 PHP Trait
- 算法 | 数据结构常见的八大排序算法
- 高通量数据下载还能这样操作?
- ofbiz实体引擎(八) 创建表
- ofbiz实体引擎(七) 检查数据源
- ofbiz实体引擎(六) GenericHelper的初始化创建
- ofbiz实体引擎(五) ModelGroupReader
- HBitcoin:C#高级比特币钱包库 - 保护您的财产安全
- ofbiz实体引擎(四) ModelReader的作用
- ofbiz实体引擎(三) GenericDelegator实例化的具体过程
- 机器学习实战 | 第五章:模型保存(持久化)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ubuntu系统theano和keras的安装方法
- Linux安装Jenkins步骤及各种问题解决(页面访问初始化密码)
- 解决Ubuntu19 安装Theano问题
- centos7 esxi6.7模板实际应用详解
- Centos8搭建本地Web服务器的实现步骤
- 总结Linux 6种日志查看方法
- Ubuntu18.04一次性升级Python所有库的方法步骤
- linux下php安装xml扩展的详细步骤
- 查看linux文件的命令详解
- 解决Linux+Apache服务器URL区分大小写问题
- Centos8(最小化安装)全新安装Python3.8+pip的方法教程
- 使用 Linux seq 命令生成数字序列(推荐)
- CentOS 7上为PHP5安装suPHP的方法(彭哥)
- Linux修改主机名命令详解
- 在CentOS 7.2上安装SuPHP的详细方法