Mobile phones

POJ - 1195

Suppose that the fourth generation mobile phone base stations in the Tampere area operate as follows. The area is divided into squares. The squares form an S * S matrix with the rows and columns numbered from 0 to S-1. Each square contains a base station. The number of active mobile phones inside a square can change because a phone is moved from a square to another or a phone is switched on or off. At times, each base station reports the change in the number of active phones to the main base station along with the row and the column of the matrix. 

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 input is read from standard input as integers and the answers to the queries are written to standard output as integers. The input is encoded as follows. Each input comes on a separate line, and consists of one instruction integer and a number of parameter integers according to the following table. 

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

Your program should not answer anything to lines with an instruction other than 2. If the instruction is 2, then your program is expected to answer the query by writing the answer as a single line containing a single integer to standard output.

Sample Input

  1. 0 4
  2. 1 1 2 3
  3. 2 0 0 2 2
  4. 1 1 1 2
  5. 1 1 2 -1
  6. 2 1 1 2 3
  7. 3

Sample Output

  1. 3
  2. 4
    ——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
    二维线段树,单点修改,区间查询,维护和。
    ______________________________________________________________________________________________________________________________________________

  1. 1 #include<cstdio>
  2. 2 #include<iostream>
  3. 3 #include<cstring>
  4. 4 #include<algorithm>
  5. 5 using namespace std;
  6. 6
  7. 7 const int maxn=1028;
  8. 8 struct LIE
  9. 9 {
  10. 10 int ll,lr,sum;
  11. 11 };
  12. 12 struct HANG
  13. 13 {
  14. 14 int hl,hr;
  15. 15 LIE lie[maxn<<2];
  16. 16 }hang[maxn<<2];
  17. 17 int op,n;
  18. 18 void readint(int &x)
  19. 19 {
  20. 20 char c=getchar();
  21. 21 int f=1;
  22. 22 for(;c<'0' || c>'9';c=getchar())if(c=='-')f=-f;
  23. 23 x=0;
  24. 24 for(;c<='9'&& c>='0';c=getchar())x=(x<<1)+(x<<3)+c-'0';
  25. 25 x*=f;
  26. 26 }
  27. 27 void buil(int pre,int cur,int ll,int lr)
  28. 28 {
  29. 29 hang[pre].lie[cur].ll=ll;hang[pre].lie[cur].lr=lr;
  30. 30 hang[pre].lie[cur].sum=0;
  31. 31 if(ll==lr)return ;
  32. 32 int mid=(ll+lr)>>1;
  33. 33 buil(pre,cur<<1,ll,mid);
  34. 34 buil(pre,cur<<1|1,mid+1,lr);
  35. 35 }
  36. 36 void build(int cur,int l,int r,int ll,int rr)
  37. 37 {
  38. 38 hang[cur].hl=l;hang[cur].hr=r;
  39. 39 buil(cur,1,ll,rr);
  40. 40 if(l==r)return ;
  41. 41 int mid=(l+r)>>1;
  42. 42 build(cur<<1,l,mid,ll,rr);
  43. 43 build(cur<<1|1,mid+1,r,ll,rr);
  44. 44 }
  45. 45 void upda(int pre,int cur,int hh,int lh,int dat)
  46. 46 {
  47. 47 hang[pre].lie[cur].sum+=dat;
  48. 48 if(hang[pre].lie[cur].ll==hang[pre].lie[cur].lr)return ;
  49. 49 int mid=(hang[pre].lie[cur].ll+hang[pre].lie[cur].lr)>>1;
  50. 50 if(lh<=mid) upda(pre,cur<<1,hh,lh,dat);
  51. 51 else upda(pre,cur<<1|1,hh,lh,dat);
  52. 52 }
  53. 53 void update(int cur,int x,int y,int dat)
  54. 54 {
  55. 55 upda(cur,1,x,y,dat);
  56. 56 if(hang[cur].hl==hang[cur].hr)return;
  57. 57 int mid=(hang[cur].hl+hang[cur].hr)>>1;
  58. 58 if(x<=mid)update(cur<<1,x,y,dat);
  59. 59 else update(cur<<1|1,x,y,dat);
  60. 60 }
  61. 61 int qure(int pre,int cur,int yl,int yr)
  62. 62 {
  63. 63 if(yl<=hang[pre].lie[cur].ll && hang[pre].lie[cur].lr<=yr)return hang[pre].lie[cur].sum;
  64. 64 int mid=(hang[pre].lie[cur].ll+hang[pre].lie[cur].lr)>>1;
  65. 65 int sum=0;
  66. 66 if(yl<=mid)sum+=qure(pre,cur<<1,yl,yr);
  67. 67 if(mid<yr)sum+=qure(pre,cur<<1|1,yl,yr);
  68. 68 return sum;
  69. 69 }
  70. 70 int query(int cur,int xl,int yl,int xr,int yr)
  71. 71 {
  72. 72 if(xl<=hang[cur].hl && hang[cur].hr<=xr)return qure(cur,1,yl,yr);
  73. 73 int mid=(hang[cur].hl+hang[cur].hr)>>1;
  74. 74 int sum=0;
  75. 75 if(xl<=mid)sum+=query(cur<<1,xl,yl,xr,yr);
  76. 76 if(mid<xr) sum+=query(cur<<1|1,xl,yl,xr,yr);
  77. 77 return sum;
  78. 78 }
  79. 79 int main()
  80. 80 {
  81. 81 readint(op);
  82. 82 while(op!=3)
  83. 83 {
  84. 84 if(!op)
  85. 85 {
  86. 86 readint(n);
  87. 87 build(1,0,n-1,0,n-1);
  88. 88 }
  89. 89 else if(op==1)
  90. 90 {
  91. 91 int x,y,dat;
  92. 92 readint(x);readint(y);readint(dat);
  93. 93 update(1,x,y,dat);
  94. 94 }
  95. 95 else if(op==2)
  96. 96 {
  97. 97 int xl,xr,yl,yr;
  98. 98 readint(xl);readint(yl);readint(xr);readint(yr);
  99. 99 printf("%d\n",query(1,xl,yl,xr,yr));
  100. 100 }
  101. 101 readint(op);
  102. 102 }
  103. 103 return 0;
  104. 104 }

POJ1195 二维线段树的更多相关文章

  1. POJ1195 Mobile phones 【二维线段树】

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14291   Accepted: 6644 De ...

  2. UVA 11297 线段树套线段树(二维线段树)

    题目大意: 就是在二维的空间内进行单个的修改,或者进行整块矩形区域的最大最小值查询 二维线段树树,要注意的是第一维上不是叶子形成的第二维线段树和叶子形成的第二维线段树要  不同的处理方式,非叶子形成的 ...

  3. POJ2155 Matrix二维线段树经典题

    题目链接 二维树状数组 #include<iostream> #include<math.h> #include<algorithm> #include<st ...

  4. HDU 1823 Luck and Love(二维线段树)

    之前只知道这个东西的大概概念,没具体去写,最近呵呵,今补上. 二维线段树 -- 点更段查 #include <cstdio> #include <cstring> #inclu ...

  5. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  6. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  7. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...

  8. HDU 4819 Mosaic (二维线段树)

    Mosaic Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)Total S ...

  9. HDU 4819 Mosaic --二维线段树(树套树)

    题意: 给一个矩阵,每次查询一个子矩阵内的最大最小值,然后更新子矩阵中心点为(Max+Min)/2. 解法: 由于是矩阵,且要求区间最大最小和更新单点,很容易想到二维的线段树,可是因为之前没写过二维的 ...

随机推荐

  1. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  2. C#自定义控件的应用(数据绑定,属性等)

    刚刚开始程序设计的码农生涯,也许一些开发工具上的控件可以满足我们的需求,但是随之时间的迁移,我们对控件的呈现形式需求越来越多样化,这个时候就需要我们来自定义控件,我是一个刚刚入职没多久的菜鸟,接触软件 ...

  3. hbase:ERROR: org.apache.hadoop.hbase.ipc.ServerNotRunningYetException: Server is not running yet

    hbase连接deug:DEBUG [main-SendThread(bigdata.server1:2181)] - Reading reply sessionid:0x16f764e7f6e000 ...

  4. 风炫安全WEB安全学习第二十七节课 XSS的防御措施

    风炫安全WEB安全学习第二十七节课 XSS的防御措施 XSS防御措施 总的原则 控制好输入/输出 过滤:根据业务需求进行过滤,对email,手机号码这样的输入框进行验证. 转义:所有输出到前端的数据都 ...

  5. 第12章 DOM操作

    目录 *1. 向DOM中注入HTML 1.1 将HTNL字符串转换成DOM 预处理HTML源字符串 包装HTML 1.2 将DOM元素插入到文档中 2. DOM的特性和属性 通过DOM方法和属性访问特 ...

  6. JavaScript高级程序设计(第4版)知识点总结

    介绍 JavaScript高级程序设计 第四版,在第三版的基础上添加了ES6相关的内容.如let.const关键字,Fetch API.工作者线程.模块.Promise 等.适合具有一定编程经验的 W ...

  7. 【Java】一个简单的Java应用程序

    简单记录,Java 核心技术卷I 基础知识(原书第10 版) 一个简单的Java应用程序"Hello, World!" Hello, World! Goodbye,World! 一 ...

  8. 【ORA】ORA-00371: not enough shared pool memory

    今天rac中有一个节点asm实例起不来包了ora-000371的错误,错误贴在下面: [oracle@rac2 dbs]$ srvctl start asm -n rac2 PRKS-1009 : F ...

  9. 【RAC】通过命令查看当前数据库是不是rac

    SQL> show parameter  cluster_database 如果参数中显示的是 NAME                                 TYPE        ...

  10. 洛谷P1972 [SDOI2009]HH的项链(树状数组)

    题目链接: https://www.luogu.org/problemnew/show/P1972 题目描述: HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后 ...