POJ1195 二维线段树
Mobile phones
Write a program, which receives these reports and answers queries about the current total number of active mobile phones in any rectangle-shaped area.
Input
The values will always be in range, so there is no need to check them. In particular, if A is negative, it can be assumed that it will not reduce the square value below zero. The indexing starts at 0, e.g. for a table of size 4 * 4, we have 0 <= X <= 3 and 0 <= Y <= 3.
Table size: 1 * 1 <= S * S <= 1024 * 1024
Cell value V at any time: 0 <= V <= 32767
Update amount: -32768 <= A <= 32767
No of instructions in input: 3 <= U <= 60002
Maximum number of phones in the whole table: M= 2^30
Output
Sample Input
0 4
1 1 2 3
2 0 0 2 2
1 1 1 2
1 1 2 -1
2 1 1 2 3
3
Sample Output
3
4
——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
二维线段树,单点修改,区间查询,维护和。
______________________________________________________________________________________________________________________________________________
1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4 #include<algorithm>
5 using namespace std;
6
7 const int maxn=1028;
8 struct LIE
9 {
10 int ll,lr,sum;
11 };
12 struct HANG
13 {
14 int hl,hr;
15 LIE lie[maxn<<2];
16 }hang[maxn<<2];
17 int op,n;
18 void readint(int &x)
19 {
20 char c=getchar();
21 int f=1;
22 for(;c<'0' || c>'9';c=getchar())if(c=='-')f=-f;
23 x=0;
24 for(;c<='9'&& c>='0';c=getchar())x=(x<<1)+(x<<3)+c-'0';
25 x*=f;
26 }
27 void buil(int pre,int cur,int ll,int lr)
28 {
29 hang[pre].lie[cur].ll=ll;hang[pre].lie[cur].lr=lr;
30 hang[pre].lie[cur].sum=0;
31 if(ll==lr)return ;
32 int mid=(ll+lr)>>1;
33 buil(pre,cur<<1,ll,mid);
34 buil(pre,cur<<1|1,mid+1,lr);
35 }
36 void build(int cur,int l,int r,int ll,int rr)
37 {
38 hang[cur].hl=l;hang[cur].hr=r;
39 buil(cur,1,ll,rr);
40 if(l==r)return ;
41 int mid=(l+r)>>1;
42 build(cur<<1,l,mid,ll,rr);
43 build(cur<<1|1,mid+1,r,ll,rr);
44 }
45 void upda(int pre,int cur,int hh,int lh,int dat)
46 {
47 hang[pre].lie[cur].sum+=dat;
48 if(hang[pre].lie[cur].ll==hang[pre].lie[cur].lr)return ;
49 int mid=(hang[pre].lie[cur].ll+hang[pre].lie[cur].lr)>>1;
50 if(lh<=mid) upda(pre,cur<<1,hh,lh,dat);
51 else upda(pre,cur<<1|1,hh,lh,dat);
52 }
53 void update(int cur,int x,int y,int dat)
54 {
55 upda(cur,1,x,y,dat);
56 if(hang[cur].hl==hang[cur].hr)return;
57 int mid=(hang[cur].hl+hang[cur].hr)>>1;
58 if(x<=mid)update(cur<<1,x,y,dat);
59 else update(cur<<1|1,x,y,dat);
60 }
61 int qure(int pre,int cur,int yl,int yr)
62 {
63 if(yl<=hang[pre].lie[cur].ll && hang[pre].lie[cur].lr<=yr)return hang[pre].lie[cur].sum;
64 int mid=(hang[pre].lie[cur].ll+hang[pre].lie[cur].lr)>>1;
65 int sum=0;
66 if(yl<=mid)sum+=qure(pre,cur<<1,yl,yr);
67 if(mid<yr)sum+=qure(pre,cur<<1|1,yl,yr);
68 return sum;
69 }
70 int query(int cur,int xl,int yl,int xr,int yr)
71 {
72 if(xl<=hang[cur].hl && hang[cur].hr<=xr)return qure(cur,1,yl,yr);
73 int mid=(hang[cur].hl+hang[cur].hr)>>1;
74 int sum=0;
75 if(xl<=mid)sum+=query(cur<<1,xl,yl,xr,yr);
76 if(mid<xr) sum+=query(cur<<1|1,xl,yl,xr,yr);
77 return sum;
78 }
79 int main()
80 {
81 readint(op);
82 while(op!=3)
83 {
84 if(!op)
85 {
86 readint(n);
87 build(1,0,n-1,0,n-1);
88 }
89 else if(op==1)
90 {
91 int x,y,dat;
92 readint(x);readint(y);readint(dat);
93 update(1,x,y,dat);
94 }
95 else if(op==2)
96 {
97 int xl,xr,yl,yr;
98 readint(xl);readint(yl);readint(xr);readint(yr);
99 printf("%d\n",query(1,xl,yl,xr,yr));
100 }
101 readint(op);
102 }
103 return 0;
104 }
POJ1195 二维线段树的更多相关文章
- POJ1195 Mobile phones 【二维线段树】
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14291 Accepted: 6644 De ...
- UVA 11297 线段树套线段树(二维线段树)
题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要 不同的处理方式,非叶子形成的 ...
- POJ2155 Matrix二维线段树经典题
题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...
- HDU 1823 Luck and Love(二维线段树)
之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- poj 1195:Mobile phones(二维线段树,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14391 Accepted: 6685 De ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- HDU 4819 Mosaic (二维线段树)
Mosaic Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/102400 K (Java/Others)Total S ...
- HDU 4819 Mosaic --二维线段树(树套树)
题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...
随机推荐
- 96. Unique Binary Search Trees1和2
/* 这道题的关键是:动态表尽量的选取,知道二叉搜索树中左子树的点都比根节点小,右子树的点都比根节点大 所以当i为根节点,左子树有i-1个点,右子树有n-i个点,左右子树就可以开始递归构建,过程和一开 ...
- JDBC学习(错误反思)
注意拼写错误!!! 注意拼写错误!!! 注意拼写错误!!! 文档注释快捷键 alt+shift+J
- java 深拷贝与浅拷贝机制详解
概要: 在Java中,拷贝分为深拷贝和浅拷贝两种.java在公共超类Object中实现了一种叫做clone的方法,这种方法clone出来的新对象为浅拷贝,而通过自己定义的clone方法为深拷贝. (一 ...
- .NET 云原生架构师训练营(模块二 基础巩固 MongoDB 更新和删除)--学习笔记
2.5.4 MongoDB -- 更新和删除 整体更新 更新字段 字段操作 数组操作 删除 https://docs.mongodb.com/manual/reference/operator/upd ...
- 第十一章节 BJROBOT PS3 手柄控制【ROS全开源阿克曼转向智能网联无人驾驶车】
1.把小车架空平放在地板上. 2.用 USB 线将 PS3 蓝牙手柄连接至小车主控端,初次连接手柄上的 4 个红色指示灯会同时闪烁; 3.按下手柄中间的圆形配对键,然后等待红灯闪烁至停止. 4.此 ...
- mysql源码分析-启动过程
mysql源码分析-启动过程 概要 # sql/mysqld.cc, 不包含psi的初始化过程 mysqld_main: // 加载my.cnf和my.cnf.d,还有命令行参数 if (load_d ...
- 九、kafka伪分布式和集群搭建
伪分布式: 1.先将zk启动,如果是在伪分布式下,kafka已经集成了zk nohup /kafka_2.11-0.10.0.1/bin/zookeeper-server-start.sh /kafk ...
- cornerstoneTools 作用,用法,api使用心得
一.cornerstoneTools的用途 1.作用可以响应一些事件,例如鼠标按下的事件,鼠标滚轮的事件或按键或触摸事件 2.可以对视口进行缩放平移 3.可以在图像上绘制图形 4.可以在图像上绘制文本 ...
- spark知识点_RDD
来自官网的Spark Programming Guide,包括个人理解的东西. 这里有一个疑惑点,pyspark是否支持Python内置函数(list.tuple.dictionary相关操作)?思考 ...
- 在md里画流程图
可以使用名为mermaid的代码块,即 ```mermaid``` 需要md解析器能解析mermaid mermaid使用详情参见