uva11992-Fast Matrix Operations(区间增值、改值)
题意:
r行c列的全0矩阵 有三种操作 1 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素增加v
2 x1 y1 x2 y2 v子矩阵(x1,y1,x2,y2)所有元素设为v
3 x1 y1 x2 y2 查询子矩阵元素的和、最小值、最大值
分析:线段树的区间更新 、把矩阵化为一维。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)|1)
#define INF 0x3f3f3f3f
const int N = ;
int r, c, m;
struct Node {
int l, r;
int sum, Max, Min, sumv, setv;
} node[ * N];
void pushup(int x) {
node[x].sum = node[lson(x)].sum + node[rson(x)].sum;
node[x].Max = max(node[lson(x)].Max, node[rson(x)].Max);
node[x].Min = min(node[lson(x)].Min, node[rson(x)].Min);
} void pushdown(int x) {
if (node[x].setv) {
node[lson(x)].sumv = node[rson(x)].sumv = ;
node[lson(x)].setv = node[rson(x)].setv = node[x].setv;
node[lson(x)].sum = (node[lson(x)].r - node[lson(x)].l + ) * node[x].setv;
node[rson(x)].sum = (node[rson(x)].r - node[rson(x)].l + ) * node[x].setv;
node[lson(x)].Max = node[lson(x)].Min = node[x].setv;
node[rson(x)].Max = node[rson(x)].Min = node[x].setv;
node[x].setv = ;
}
if (node[x].sumv) {
node[lson(x)].sumv += node[x].sumv;
node[rson(x)].sumv += node[x].sumv;
node[lson(x)].sum += (node[lson(x)].r - node[lson(x)].l + ) * node[x].sumv;
node[rson(x)].sum += (node[rson(x)].r - node[rson(x)].l + ) * node[x].sumv;
node[lson(x)].Max += node[x].sumv;
node[lson(x)].Min += node[x].sumv;
node[rson(x)].Max += node[x].sumv;
node[rson(x)].Min += node[x].sumv;
node[x].sumv = ;
}
} void build(int l, int r, int x) {
node[x].l = l; node[x].r = r;
if (l == r) {
node[x].sum = node[x].Max = node[x].Min = node[x].sumv = node[x].setv = ;
return;
}
int mid = (l + r) / ;
build(l, mid, lson(x));
build(mid + , r, rson(x));
pushup(x);
} void update_add(int l, int r, int v, int x) {
if (node[x].l >= l && node[x].r <= r) {
node[x].sumv += v;
node[x].sum += (node[x].r - node[x].l + ) * v;
node[x].Max += v;
node[x].Min += v;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / ;
if (l <= mid) update_add(l, r, v, lson(x));
if (r > mid) update_add(l, r, v, rson(x));
pushup(x);
} void update_set(int l, int r, int v, int x) {
if (node[x].l >= l && node[x].r <= r) {
node[x].setv = v;
node[x].sum = (node[x].r - node[x].l + ) * v;
node[x].Max = node[x].Min = v;
node[x].sumv = ;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / ;
if (l <= mid) update_set(l, r, v, lson(x));
if (r > mid) update_set(l, r, v, rson(x));
pushup(x);
} Node query(int l, int r, int x) {
Node ans; ans.sum = ; ans.Max = ; ans.Min = INF;
if (node[x].l >= l && node[x].r <= r) {
ans.sum = node[x].sum;
ans.Max = node[x].Max;
ans.Min = node[x].Min;
return ans;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / ;
if (l <= mid) {
Node tmp = query(l, r, lson(x));
ans.sum += tmp.sum;
ans.Max = max(ans.Max, tmp.Max);
ans.Min = min(ans.Min, tmp.Min);
}
if (r > mid) {
Node tmp = query(l, r, rson(x));
ans.sum += tmp.sum;
ans.Max = max(ans.Max, tmp.Max);
ans.Min = min(ans.Min, tmp.Min);
}
return ans;
} int main() {
while (~scanf("%d%d%d", &r, &c, &m)) {
build(, r * c, );
int q, x1, y1, x2, y2, v;
while (m--) {
scanf("%d", &q);
if (q == ) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
int sum = , Max = , Min = INF;
for (int i = x1; i <= x2; i++) {
Node ans = query((i-)* c + y1, (i-) * c + y2, );
sum += ans.sum;
Max = max(Max, ans.Max);
Min = min(Min, ans.Min);
}
printf("%d %d %d\n", sum, Min, Max);
}
else {
scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &v);
for (int i = x1; i <= x2; i++) {
if (q == ) update_add((i-) * c + y1, (i-) * c + y2, v, );
else update_set((i-) * c + y1, (i-) * c + y2, v, );
}
}
}
}
return ;
}
uva11992-Fast Matrix Operations(区间增值、改值)的更多相关文章
- UVA11992 - Fast Matrix Operations(段树部分的变化)
UVA11992 - Fast Matrix Operations(线段树区间改动) 题目链接 题目大意:给你个r*c的矩阵,初始化为0. 然后给你三种操作: 1 x1, y1, x2, y2, v ...
- [uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)
题目链接:https://vjudge.net/problem/UVA-11992 题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询.查询返回子矩阵和.最小值.最大值 n很小 ...
- UVA11992 Fast Matrix Operations
思路 注意到最多20行,拆成20颗线段树即可 注意set标记清空左右儿子的add,不要清空自己的add,因为这个set操作之后可能还有add存在这个节点上 代码 #include <cstdio ...
- Fast Matrix Operations
A Simple Problem with Integers 每次将区间向下更新,或是用之前的方法,统计当前节点到父节点处的覆盖数目. #include <cstdio> #include ...
- UVA 11992 - Fast Matrix Operations(段树)
UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- uva 11992 Fast Matrix Operations 线段树模板
注意 setsetset 和 addvaddvaddv 标记的下传. 我们可以控制懒惰标记的优先级. 由于 setsetset 操作的优先级高于 addaddadd 操作,当下传 setsetset ...
- luogu题解 UVA11992 【Fast Matrix Operations】
题目链接: https://www.luogu.org/problemnew/show/UVA11992 题目大意: 一个r*c的矩阵,一开始元素都是0,然后给你m次三种操作,分别是将一个子矩阵中所有 ...
- UVa 11992 (线段树 区间修改) Fast Matrix Operations
比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...
- UVa 11992 Fast Matrix Operations (线段树,区间修改)
题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...
随机推荐
- How to open MS word document from the SharePoint 2010 using Microsoft.Office.Interop.dll
or this you must change the identity of word component inC:\windows\System32\comexp.mscto be interac ...
- (转)GDT与LDT
网址:http://blog.csdn.net/billpig/article/details/5833980 保护模式下的段寄存器 由 16位的选择器 与 64位的段描述符寄存器 构成段描述符寄存器 ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- webstorm 11 安装配置 grunt 时遇到的问题及解决办法
想学grunt的可以看看这篇文章,写的很有意思,教程之类的我就不写了,网上很多资料,我就记录下我遇到的问题和解决办法. http://yujiangshui.com/grunt-basic-tutor ...
- Extjs4.2布局——Ext.container.ViewportView
先贴出官方文档的关于此布局的描述:“ 一个专门的容器用于可视应用领域(浏览器窗口). Viewport渲染自身到网页的documet body区域, 并自动将自己调整到适合浏览器窗口的大小,在窗口大小 ...
- select框的text与value值的获取(实用版)
function def(){ var key = document.getElementById ('selectarea'); //select list var value = docum ...
- tomcat安全设置
1.关闭服务器端口:server.xml默认有下面一行: <Server port="8005" shutdown="SHUTDOWN"> 这样允许 ...
- Android 图片旋转(使用Matrix.setRotate方法)
imageView2 = (ImageView) findViewById(R.id.img2); Bitmap bitmap = BitmapFactory.decodeResource(getRe ...
- android dialog点击其他区域消失
只需调用dialog对象的setCanceledOnTouchOutside方法,传入参数为true即可. 如下代码实现: //点击其他区域dialog消失 menuDialog.setCancele ...
- C/C++语言参数传递----函数/方法 参数的指针引用传递
int m_value = 1; void func(int *p) { p = &m_value; } int main(int argc, char *argv[]) { int n = ...