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

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

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+7;
struct node{typedef int SgTreeDataType;struct treenode{int L , R  ;SgTreeDataType sum , lazy;void update(SgTreeDataType v){sum += (R-L+1)*v;lazy += v;}};treenode tree[maxn*4];inline void push_down(int o){SgTreeDataType lazyval = tree[o].lazy;tree[2*o].update(lazyval) ; tree[2*o+1].update(lazyval);tree[o].lazy = 0;}inline void push_up(int o){tree[o].sum = tree[2*o].sum + tree[2*o+1].sum;}inline void build_tree(int L , int R , int o){tree[o].L = L , tree[o].R = R,tree[o].sum = tree[o].lazy = 0;if (R > L){int mid = (L+R) >> 1;build_tree(L,mid,o*2);build_tree(mid+1,R,o*2+1);}}inline void update(int QL,int QR,SgTreeDataType v,int o){int L = tree[o].L , R = tree[o].R;if (QL <= L && R <= QR) tree[o].update(v);else{push_down(o);int mid = (L+R)>>1;if (QL <= mid) update(QL,QR,v,o*2);if (QR >  mid) update(QL,QR,v,o*2+1);push_up(o);}}inline SgTreeDataType query(int QL,int QR,int o){int L = tree[o].L , R = tree[o].R;if (QL <= L && R <= QR) return tree[o].sum;else{push_down(o);int mid = (L+R)>>1;SgTreeDataType res = 0;if (QL <= mid) res += query(QL,QR,2*o);if (QR > mid) res += query(QL,QR,2*o+1);push_up(o);return res;}}
}T;
int r,c;
int ans[10005];
string op[10005];
int x1[10005],Y1[10005],x2[10005],y2[10005];
int main()
{scanf("%d%d",&r,&c);int m;scanf("%d",&m);for(int i=1;i<=m;i++)cin>>op[i],scanf("%d%d%d%d",&x1[i],&Y1[i],&x2[i],&y2[i]);for(int i=1;i<=r;i++){T.build_tree(1,c,1);for(int j=1;j<=m;j++){if(x1[j]<=i&&i<=x2[j]){if(op[j][0]=='q')ans[j]+=T.query(Y1[j],y2[j],1);if(op[j][0]=='a')T.update(Y1[j],y2[j],1,1);if(op[j][0]=='r')T.update(Y1[j],y2[j],-1,1);}}}for(int i=1;i<=m;i++)if(op[i][0]=='q')cout<<ans[i]<<endl;}

Xtreme9.0 - Block Art 线段树相关推荐

  1. [bzoj1582][Usaco2009 Hol]Holiday Painting 节日画画_线段树

    Holiday Painting 节日画画 bzoj-1582 Usaco-2009 Hol 题目大意:给定两个n*m的01网格图.q次操作,每次将第二个网格图的子矩阵全部变成0或1,问每一次操作后两 ...

  2. 线段树合并与分裂维护树上最长上升子序列 + 点分治删点 ---- 2021 牛客多校第一场 C - Cut the tree(详解)

    题目大意: 给你一个树,树上每个点都有一个权值valnodeval_{node}valnode​,路径(u,v)(u,v)(u,v) 上所有点按顺序有序序列,令f(u,v)f(u,v)f(u,v)是这 ...

  3. 2018.06.28 与或(线段树)

    #与或 描述 样例输入 5 8 1 3 2 5 4 3 1 3 2 1 1 5 3 1 3 1 1 4 6 2 3 4 1 3 2 3 2 2 3 4 3 1 5 **样例输出 ** 3 5 3 7 ...

  4. ACM入门之【线段树】

    线段树是算法竞赛中常用的用来维护 区间信息 的数据结构. 线段树可以在 O(logN)的时间复杂度内实现单点修改.区间修改.区间查询(区间求和,求区间最大值,求区间最小值)等操作. 线段树相对于树状数 ...

  5. BZOJ 3836 Codeforces 280D k-Maximum Subsequence Sum (模拟费用流、线段树)

    题目链接 (BZOJ) https://www.lydsy.com/JudgeOnline/problem.php?id=3836 (Codeforces) http://codeforces.com ...

  6. 线段树之延时标记(区间修改)及lazy思想

    暴力求解 1.最简单的方法是:在主函数中添加一个循环  进行 r-l+1次单点修改实现区间修改,对于单个元素修改时间复杂度为 O(log2(n)) 所以对于单个区间修改的时间复杂度为 O(n*log( ...

  7. poj 2352 线段树

    注意到题目中给的y是递增的,那个y没什么用,直接线段树维护一段1,x的区间. #include<string> #include<iostream> #include<c ...

  8. 一些奇妙的线段树操作

    学过数据结构和会做题完全是两个概念orz 各种各样的题目都应该见识一下 简单的目录: 最大连续长度 吉司机线段树 线段树合并/分裂 最大连续长度问题 典型题目:HDU 3911 ($Black$ $A ...

  9. 数据结构二之线段树Ⅱ——KiKi‘s K-Number,ball,The Child and Sequence,「雅礼集训 2017 Day1」市场,Atlantis

    值域线段树+势能线段树+扫描线 KiKi's K-Number ball The Child and Sequence 「雅礼集训 2017 Day1」市场 Atlantis KiKi's K-Num ...

最新文章

  1. Linux系统编程8:入门篇之简单明了说明如何在Linux中Git提交代码
  2. delimiter在mysql中的作用_细细研究MySql中delimiter起到的作用_MySQL
  3. 晶澳独家供货40兆瓦全球最大漂浮式太阳能电站
  4. pdfminer的安装
  5. 近世代数概论------有理数与域
  6. 51单片机驱动LCD1602显示原理及例程
  7. LQR,iLQR,DDP控制论经典算法(MBRL基础知识)
  8. 关于Springboot、SpringCloud以及SpringCloud-Alibaba Nacos依赖问题
  9. X64下进程隐藏实现与Debug
  10. 机器学习学习笔记(3)——量纲与无量纲,标准化、归一化、正则化
  11. 文献笔记:Contrast-Phys: Unsupervised Video-based Remote Physiological Measurement viaSpatiotemporal Con
  12. double转换为二进制
  13. 虹科干货 | 仅需3步!教你如何基于Windows系统操作使用RELY-TSN-KIT评估套件
  14. 关于openGL, openGL ES, openVG及android中2D调用关系的报告
  15. 海尔计算机无法装win7系统,海尔自带Win10系统如何改成Win7系统?海尔台式机装win7详细步骤...
  16. 一个简单的pingpong程序测试mpi消息通讯的开销及并行计算通讯启动时间测算
  17. 服务器系统整机拷贝,服务器主机整机拷贝
  18. 旭元数艺:数创未来,智攀高峰
  19. 查看指定端口的占用情况
  20. lambda表达式与6种方法引用格式

热门文章

  1. Servlet转发forward和重定向response.sendRedirect()区别
  2. 【体系结构】Oracle如何保证提交的数据不丢失
  3. 在线CSS代码压缩美化工具
  4. getResourceAsStream的3种路径配置
  5. 年终述职--常见问题分析解答
  6. Java面试题详解一:面向对象三大特性
  7. PHP执行耗时脚本实时输出内容
  8. PyQt5教程——组件(7)
  9. [学习windows/记录篇]使用tmg三向外围发布ssl安全的web网站
  10. ASP.NET实现增删改查等功能(Access版)系统之一