UVA 11992 - Fast Matrix Operations(段树)
UVA 11992 - Fast Matrix Operations
题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a。查询和
思路:因为最多20列,所以全然能够当作20个线段树来做,然后线段树是区间改动区间查询,利用延迟操作,开两个延迟值一个存放set操作。一个存放add操作
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lson(x) ((x<<1) + 1)
#define rson(x) ((x<<1) + 2)
#define INF 0x3f3f3f3f const int N = 1000005; int r, c, m; struct Node {
int l, r;
int sum, Max, Min, sumv, setv;
} node[4 * 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 = 0;
node[lson(x)].setv = node[rson(x)].setv = node[x].setv;
node[lson(x)].sum = (node[lson(x)].r - node[lson(x)].l + 1) * node[x].setv;
node[rson(x)].sum = (node[rson(x)].r - node[rson(x)].l + 1) * 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 = 0;
}
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 + 1) * node[x].sumv;
node[rson(x)].sum += (node[rson(x)].r - node[rson(x)].l + 1) * 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 = 0;
}
} 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 = 0;
return;
}
int mid = (l + r) / 2;
build(l, mid, lson(x));
build(mid + 1, r, rson(x));
pushup(x);
} void 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 + 1) * v;
node[x].Max += v;
node[x].Min += v;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) add(l, r, v, lson(x));
if (r > mid) add(l, r, v, rson(x));
pushup(x);
} void 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 + 1) * v;
node[x].Max = node[x].Min = v;
node[x].sumv = 0;
return;
}
pushdown(x);
int mid = (node[x].l + node[x].r) / 2;
if (l <= mid) set(l, r, v, lson(x));
if (r > mid) set(l, r, v, rson(x));
pushup(x);
} Node query(int l, int r, int x) {
Node ans; ans.sum = 0; ans.Max = 0; 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) / 2;
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(1, r * c, 0);
int q, x1, y1, x2, y2, v;
while (m--) {
scanf("%d", &q);
if (q == 3) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
x1--; x2--;
int sum = 0, Max = 0, Min = INF;
for (int i = x1; i <= x2; i++) {
Node ans = query(i * c + y1, i * c + y2, 0);
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);
x1--; x2--;
for (int i = x1; i <= x2; i++) {
if (q == 1) add(i * c + y1, i * c + y2, v, 0);
else set(i * c + y1, i * c + y2, v, 0);
}
}
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
UVA 11992 - Fast Matrix Operations(段树)的更多相关文章
- uva 11992 Fast Matrix Operations 线段树模板
注意 setsetset 和 addvaddvaddv 标记的下传. 我们可以控制懒惰标记的优先级. 由于 setsetset 操作的优先级高于 addaddadd 操作,当下传 setsetset ...
- UVA11992 - Fast Matrix Operations(段树部分的变化)
UVA11992 - Fast Matrix Operations(线段树区间改动) 题目链接 题目大意:给你个r*c的矩阵,初始化为0. 然后给你三种操作: 1 x1, y1, x2, y2, v ...
- 线段树(多维+双成段更新) UVA 11992 Fast Matrix Operations
题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...
- UVA 11992 Fast Matrix Operations(线段树:区间修改)
题目链接 2015-10-30 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=s ...
- UVA 11992 Fast Matrix Operations (二维线段树)
解法:因为至多20行,所以至多建20棵线段树,每行建一个.具体实现如下,有些复杂,慢慢看吧. #include <iostream> #include <cstdio> #in ...
- UVa 11992 Fast Matrix Operations (线段树,区间修改)
题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...
- uva 11992 - Fast Matrix Operations
简单的线段树的题: 有两种方法写这个题,目前用的熟是这种慢点的: 不过不知道怎么老是T: 感觉网上A过的人的时间度都好小,但他们都是用数组实现的 难道是指针比数组慢? 好吧,以后多用数组写写吧! 超时 ...
- UVA 11992 Fast Matrix Operations (降维)
题意:对一个矩阵进行子矩阵操作. 元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作. 一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测 ...
- 【UVA】11992 - Fast Matrix Operations(段树模板)
主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...
随机推荐
- mysql增量ID
启动值更改方法
在mysql很多朋友感到场AUTO_INCREMENT增量型ID值它不能被改变,其实这种认识是错误的,这里mysql增量ID开始值更改和设置. 设置自动递增字段的通常的方法: 格时加入: create ...
- R12 付款过程请求-功能和技术信息 (文档 ID 1537521.1)
In this Document Abstract History Details _afrLoop=2234450430619177&id=1537521.1&dis ...
- c#怎样获取excel单元格的RGB颜色
这段时间一直在做office的工作.前2天获取单元格的颜色的问题一直没搞明确. 開始我想用的就是Npoi.主要前一部分的工作都是用Npoi完毕的 row.GetCell(j).CellStyle.Fi ...
- U6Linux的文件权限与目录配置
1.ll查看文件信息:[权限][连接][所有者][用户组][文件容量][修改日期][文件名] 2.第一个字符代表文件的属性:若为[d]则是目录.若为[-]则是文件.若为[l]则为连接. 3.chgrp ...
- 堆栈帧的组织——C/C++内存管理必须掌握
程序栈 说到堆栈帧,你得先说说程序栈. 记忆功能程序堆栈区是支持操作,通常共享堆. 程序栈通常占领内存区域的下部,而堆用的是上部. 程序栈存放栈帧,栈帧有时候也称为活跃记录或活跃帧.栈帧存放函数參数和 ...
- Struts开发问题集锦
在struts2de 1.6以前版本,都是用<s:datepicker>标签来获取时间,1.8后可以用struts-dojo.plugin里的<sx:datetimepicker&g ...
- linux下安装cmake和mysql遇到的问题总结
首先是在安装cmake的过程中遇到的问题: 1.開始使用yum命令安装时,不知道为什么一直不行,然后就准备wget 来先下载压缩包,再手动编译. 因为网络限制,wget不能下载外网的东西一直显示con ...
- SQL SERVER IN参数化处理
方法一. CREATE TABLE [dbo].[Users] ( Id INTEGER IDENTITY(1, 1) PRIMARY KEY , Name NVARCHAR(50) NOT NULL ...
- 期望dp专题
一直不明白为什么概率是正推,期望是逆推. 现在题目做多了,慢慢好像有点明白了 poj2096 收集bug, 有n个种类的bug,和s个子系统. 每找到一个bug需要一天. 要我我们求找到n个种类的 ...
- 讨论JDK的File.equal()
我们一般比较两个文件中的对象是相同的文件,通常使用java.io.File.equal().这里,equal()是不是文件内容的比较结果为.象是否指向同一个文件. File的equal()方法.实际上 ...