Block Art

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art

Description

The NeoCubist artistic movement has a very distinctive approach to art. It starts with a rectangle which is divided into a number of squares. Then multiple rounds of layering and scraping occur. In a layering round, a rectangular region of this canvas is selected and a layer of cubes, 1 cube deep, is added to the region. In a scraping round, a rectangular region of the canvas is selected, and a layer of cubes, again 1 cube deep, is removed.

The famous artist I.M. Blockhead seeks your help during the creation of this artwork. As he is creating his art, he is curious to know how many blocks are in different regions of the canvas.

Your task is to write a program that tracks the creation of a work of Pseudo-Cubist art, and answers I.M.’s periodic queries about the number of blocks that are on the canvas. Consider, for example, the following artwork created on a canvas with 5 rows and 15 columns or squares. The canvas starts without any blocks, like in the figure below. We label cells in the canvas based on the tuple (row,column), with the upper left corner being designated (1,1). The numbers in each cell represent the height of the blocks in that cell.

fig1.jpg

After adding a layer in blocks to the rectangle with upper left corner at (2,3) and a lower right corner of (4, 10), the canvas now looks like the following:

fig2.jpg

After adding a layer of blocks in the rectangle with upper left corner at (3,8) and a lower right corner of (5, 15), the canvas now looks like the following:

fig3.jpg

If Blockhead were to ask how many blocks are currently in the artwork in the rectangle with upper left corner (1,1) and lower right corner (5,15), you would tell him 48.

Now, if we remove a layer of blocks from the rectangle with upper left corner at (3,6) and a lower right corner of (4, 12), the canvas now looks like the following:

fig4.jpg

If Blockhead were to ask how many blocks are now in the artwork in the rectangle with upper left corner (3,5) and lower right corner (4,13), you would tell him 10.

“Beautiful!” exclaims Blockhead.

Input

The first line in each test case are two integers r and c, 1 <= r <= 12, 1 <= c <= 106, where r is the number of rows and c is the number of columns in the canvas.

The next line of input contains an integer n, 1 <= n <= 104.

The following n lines of input contain operations and queries done on the initially empty canvas. The operations will be in the following format:

[operation] [top left row] [top left column] [bottom right row] [bottom right column]

[operation] is a character, either “a” when a layer of blocks is being added, “r” when a layer of blocks is being removed, and “q” when Blockhead is asking you for the number of blocks in a region.

The remaining values on the line correspond to the top left and bottom right corners of the rectangle.

Note: You will never be asked to remove a block from a cell that has no blocks in it.

Output

For each “q” operation in the input, you should output, on a line by itself, the number of blocks in the region of interest.

Sample Input

5 15

5

a 2 3 4 10

a 3 8 5 15

q 1 1 5 15

r 3 6 4 12

q 3 5 4 13

Sample Output

48

10

Hint

题意

给你一个矩形,然后你需要维护三个操作

使得一个矩形区域都加1,使得一个矩形区域减一,查询一个矩形区域的和

题解

仔细观察可以知道,这个矩形的宽才12,所以直接暴力一维线段树就好了。

二维线段树会mle

所以我们对于每一行都单独处理就好了,这样就能把空间省下来。

代码

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e6+7;
  4. struct node{
  5. typedef int SgTreeDataType;
  6. struct treenode
  7. {
  8. int L , R ;
  9. SgTreeDataType sum , lazy;
  10. void update(SgTreeDataType v)
  11. {
  12. sum += (R-L+1)*v;
  13. lazy += v;
  14. }
  15. };
  16. treenode tree[maxn*4];
  17. inline void push_down(int o)
  18. {
  19. SgTreeDataType lazyval = tree[o].lazy;
  20. tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);
  21. tree[o].lazy = 0;
  22. }
  23. inline void push_up(int o)
  24. {
  25. tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;
  26. }
  27. inline void build_tree(int L , int R , int o)
  28. {
  29. tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy = 0;
  30. if (R > L)
  31. {
  32. int mid = (L+R) >> 1;
  33. build_tree(L,mid,o*2);
  34. build_tree(mid+1,R,o*2+1);
  35. }
  36. }
  37. inline void update(int QL,int QR,SgTreeDataType v,int o)
  38. {
  39. int L = tree[o].L , R = tree[o].R;
  40. if (QL <= L && R <= QR) tree[o].update(v);
  41. else
  42. {
  43. push_down(o);
  44. int mid = (L+R)>>1;
  45. if (QL <= mid) update(QL,QR,v,o*2);
  46. if (QR > mid) update(QL,QR,v,o*2+1);
  47. push_up(o);
  48. }
  49. }
  50. inline SgTreeDataType query(int QL,int QR,int o)
  51. {
  52. int L = tree[o].L , R = tree[o].R;
  53. if (QL <= L && R <= QR) return tree[o].sum;
  54. else
  55. {
  56. push_down(o);
  57. int mid = (L+R)>>1;
  58. SgTreeDataType res = 0;
  59. if (QL <= mid) res += query(QL,QR,2*o);
  60. if (QR > mid) res += query(QL,QR,2*o+1);
  61. push_up(o);
  62. return res;
  63. }
  64. }
  65. }T;
  66. int r,c;
  67. int ans[10005];
  68. string op[10005];
  69. int x1[10005],Y1[10005],x2[10005],y2[10005];
  70. int main()
  71. {
  72. scanf("%d%d",&r,&c);
  73. int m;scanf("%d",&m);
  74. for(int i=1;i<=m;i++)
  75. cin>>op[i],scanf("%d%d%d%d",&x1[i],&Y1[i],&x2[i],&y2[i]);
  76. for(int i=1;i<=r;i++){
  77. T.build_tree(1,c,1);
  78. for(int j=1;j<=m;j++){
  79. if(x1[j]<=i&&i<=x2[j]){
  80. if(op[j][0]=='q')ans[j]+=T.query(Y1[j],y2[j],1);
  81. if(op[j][0]=='a')T.update(Y1[j],y2[j],1,1);
  82. if(op[j][0]=='r')T.update(Y1[j],y2[j],-1,1);
  83. }
  84. }
  85. }
  86. for(int i=1;i<=m;i++)
  87. if(op[i][0]=='q')
  88. cout<<ans[i]<<endl;
  89. }

Xtreme9.0 - Block Art 线段树的更多相关文章

  1. HDU5023:A Corrupt Mayor's Performance Art(线段树区域更新+二进制)

    http://acm.hdu.edu.cn/showproblem.php?pid=5023 Problem Description Corrupt governors always find way ...

  2. hdu 5023 A Corrupt Mayor's Performance Art 线段树

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  3. hdu----(5023)A Corrupt Mayor's Performance Art(线段树区间更新以及区间查询)

    A Corrupt Mayor's Performance Art Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100 ...

  4. HDU 5023 A Corrupt Mayor's Performance Art 线段树区间更新+状态压缩

    Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5023 #include <cstdio> #include <cstring&g ...

  5. 线段树 poj 1436

    题目大意:给出n条垂直于x轴的线段的数据y1,y2,x,求出有几个三条线段一组的三元组并且他们兩兩能相见的.思路:对y轴建树,将x排序,然后按顺序边询问边擦入,用mark[i][j]表示j往左可以看到 ...

  6. zkw线段树详解

    转载自:http://blog.csdn.net/qq_18455665/article/details/50989113 前言 首先说说出处: 清华大学 张昆玮(zkw) - ppt <统计的 ...

  7. HDU4288 Coder(线段树)

    注意添加到集合中的数是升序的,先将数据读入,再离散化. sum[rt][i]表示此节点的区域位置对5取模为i的数的和,删除一个数则右边的数循环左移一位,添加一个数则右边数循环右移一位,相当于循环左移4 ...

  8. HDU4288:Coder(线段树单点更新版 && 暴力版)

    Problem Description In mathematics and computer science, an algorithm describes a set of procedures ...

  9. hdu4419 Colourful Rectangle 12年杭州网络赛 扫描线+线段树

    题意:给定n个矩形,每个矩形有一种颜色,RGB中的一种.相交的部分可能为RG,RB,GB,RGB,问这n个矩形覆盖的面积中,7种颜色的面积分别为多少 思路:把x轴离散化做扫描线,线段树维护一个扫描区间 ...

随机推荐

  1. ngx_lua_API 指令详解(六)ngx.thread.spawn、ngx.thread.wait、ngx.thread.kill介绍

    摘要:通过lua-nginx-module中的ngx.thread同时执行多个任务. ngx_lua中访问多个第三方服务 ngx_lua中提供了ngx.socket API,可以方便的访问第三方网络服 ...

  2. 20155303 2016-2017-2 《Java程序设计》第六周学习总结

    20155303 2016-2017-2 <Java程序设计>第六周学习总结 课堂笔记 高效学习法推荐 看视频学习(2h)→ 以代码为中心看课本,思考运行结果并验证(3h)→ 课后作业验证 ...

  3. js实现避免浏览器拦截弹出新页面的方法

    1 问题描述 点击button按钮,提交页面的form表单,后台执行完毕后返回参数,前台页面需要该参数实现跳转,如何实现保留该原来的页面,并在浏览器选项卡新建一个页面,且不被浏览器拦截? 2 方法及问 ...

  4. Linux 串口、usb转串口驱动分析(2-1) 【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186851 Linux 串口.usb转 ...

  5. linux服务器账号密码正确无法登录

    登录服务器时,发现密码错误,输入后还是错误不能登录 最后发现登录日志中有pam_tally2(sshd:auth): user root (0) tally 53, deny 6 less /var/ ...

  6. Oracle 数据库和监听器开机自启动两种实现方法

    数据库和监听器开机自启动   编辑oratab文件: 修改:orcl:/u01/app/oracle/product/11.2.0/db_1:N            orcl:/u01/app/or ...

  7. ORACLE数据库数据文件转移方法(不同于move方法)

    1) 手动拷贝要转移的数据数据文件'd:\OracleData\GWTABLE42.DBF'到新的位置'E:\OracleData\GWTABLE42.DBF'. 2) 把数据文件所属的表空间Offl ...

  8. 在Scrapy项目【内外】使用scrapy shell命令抓取 某网站首页的初步情况

    Windows 10家庭中文版,Python 3.6.3,Scrapy 1.5.0, 时隔一月,再次玩Scrapy项目,希望这次可以玩的更进一步. 本文展示使用在 Scrapy项目内.项目外scrap ...

  9. ubuntu eclipse 找不到jre文件

    一. 把jdk下的jre文件copy到eclipse安装目录 二. 打开eclipse 重新设计library和工作空间

  10. python过滤 Kubernetes api数据

    一.需求分析 Kubernetes endpoints api地址 http://ip地址:端口/api/v1/namespaces/default/endpoints services api地址 ...