题意:

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 = 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 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 + 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) 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 + 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) 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 = 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, 1);int q, x1, y1, x2, y2, v;while (m--) {scanf("%d", &q);if (q == 3) {scanf("%d%d%d%d", &x1, &y1, &x2, &y2);int sum = 0, Max = 0, Min = INF;for (int i = x1; i <= x2; i++) {Node ans = query((i-1)* c + y1, (i-1) * c + y2, 1);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 == 1) update_add((i-1) * c + y1, (i-1) * c + y2, v, 1);else update_set((i-1) * c + y1, (i-1) * c + y2, v, 1);}}}}return 0;
}

转载于:https://www.cnblogs.com/zsf123/p/4716361.html

uva11992-Fast Matrix Operations(区间增值、改值)相关推荐

  1. UVA 11992 - Fast Matrix Operations(段树)

    UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...

  2. UVa 11992 (线段树 区间修改) Fast Matrix Operations

    比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...

  3. Fast Matrix Operations

    uva11992:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_prob ...

  4. uva 11992 Fast Matrix Operations

    这道题很狗血啊 赋值的时候那个v是大于等于0来着,主要考察区间赋值和更新,pushdown同时要有两个操作.代码如下: 1 #include<cstdio> 2 #include<c ...

  5. 线段树(多维+双成段更新) UVA 11992 Fast Matrix Operations

    题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...

  6. 【UVA】11992 - Fast Matrix Operations(段树模板)

    主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...

  7. uva 11992 - Fast Matrix Operations

    简单的线段树的题: 有两种方法写这个题,目前用的熟是这种慢点的: 不过不知道怎么老是T: 感觉网上A过的人的时间度都好小,但他们都是用数组实现的 难道是指针比数组慢? 好吧,以后多用数组写写吧! 超时 ...

  8. 数学基础 - 矩阵的基本运算(Matrix Operations)

    矩阵的基本运算(Matrix Operations) 目录 矩阵的基本运算(Matrix Operations) 目录 三个初等行(列)变换 加法(Plus) 乘法(Multiply) 与数的乘法 与 ...

  9. Matrix operations

    本文为<Linear algebra and its applications>的读书笔记 目录 Sums and Scalar Multiples Matrix Multiplicati ...

  10. Fast Matrix Factorization for Online Recommendation with Implicit Feedback论文代码分析

    1 数据结构 userCount:用户数 itemCount:项目数 user_ratings:ArrayList<ArrayList>, 问:此处为什么要用二维数组? 答:第1维是用户, ...

最新文章

  1. 游戏开发论坛_OPPO开发者大会前瞻:见证OPPO智能服务新生态
  2. java线程数翻倍性能翻倍_术业专攻 | 如何让Java Web性能翻倍?
  3. 各路券商会盟互联网金融 敢问路在何方
  4. boost::hana::string_c用法的测试程序
  5. css动画(transition/transform/animation)
  6. Spring学习大杂烩(待续)
  7. 【华为云技术分享】如何做一个优秀软件-可扩展的架构,良好的编码,可信的过程
  8. Wannafly挑战赛14 F
  9. lua定时器与定时任务的接口设计
  10. HTML5:web socket 和 web worker
  11. 数字电子技术基础(十):SR锁存器
  12. 2021-05-11
  13. 常用度量--MAE(平均绝对误差)和RMSE(均方根误差)
  14. css的外链写法,纯CSS代码为外链增加图标
  15. chrome 下载 中断_如何在Google Chrome浏览器中恢复中断的下载
  16. Java开发个人总结
  17. C++获取数组的长度
  18. 车载诊断数据库ODX——ODX参数解析类型(上)
  19. android烧号工具,让你“爱恨交加”的
  20. CSDN为什么学生认证有些人显示学校名称,有些显示高校学生

热门文章

  1. python学习之--内置函数:
  2. Android应用程序版本号管理(官方文档中文版) 2011-08-07 22:03:36
  3. thrift (转)
  4. solr5.5.3+tomcat8部署
  5. [转]Android 超高仿微信图片选择器 图片该这么加载
  6. Elastic Search Java Api 创建索引结构,添加索引
  7. FireFox 插件SQLite Manager 学习
  8. 56.Linux/Unix 系统编程手册(下) -- SOCKET 介绍
  9. 25.TCP/IP 详解卷1 --- SNMP:简单网络管理协议
  10. 41.MySQL 主从复制, 双主热备