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了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...
随机推荐
- LNK1207: incompatible PDB format in********
LNK1207: incompatible PDB format in******** VC中错误:LINK : fatal error LNK1207: incompatible PDB forma ...
- 【译】ASP.NET MVC 5 教程 - 5:使用 SQL 服务器 LocalDB 创建连接字符串
原文:[译]ASP.NET MVC 5 教程 - 5:使用 SQL 服务器 LocalDB 创建连接字符串 在上一节中,我们创建了MovieDBContext 类来连接数据库.处理Movie 对象和数 ...
- 谈Web应用系统的可维护性
每一个软件开发人员都十分清楚, 当软件构建得越来越复杂时, 可维护性就成了一个很突出的问题. 如何在构造软件系统的过程中始终保持可控制的可维护性呢? 一. 整体组织 ...
- JDBC数据库编程常用接口(转)
JDBC的全称是Java DataBase Connectivity,是一套面向对象的应用程序接口(API),制定了统一的访问各种关系数据库的标准接口,为各个数据库厂商提供了标准接口的实现.这东西能够 ...
- TCP closing a connection
client closes socket: clientSocket.close(); step1 :client sends TCP FIN control segment to server st ...
- [置顶] quartznet任务调度和消息调度(JAVA与C#版对比)
quartznet任务调度和消息调度 1. 作用 自动执行任务. 2. 下载地址 NET版本 JAVA版本 1下载 http://quartznet.sourceforge.net/downloa ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...
- 域名注册查询接口(API)的说明
1.域名查询 接口采用HTTP,POST,GET协议: 调用URL:http://panda.www.net.cn/cgi-bin/check.cgi 参数名称:area_domain 值为标准域名, ...
- nginx subrequest演示示例程序
只有简单subrequest应用演示示例. nginx.conf文件: #user nobody; worker_processes 1; #error_log logs/error.log; #er ...
- 悟道—位IT高管20年的职场心经(读书笔记五)
悟道--一位IT高管20年的职场心经 第五章 搞定老板 "老板就是老板" 这一点,你可能会忘了,他一定不会忘: "老板不会总是老板" 这一点,他可能会忘,你最好 ...