[uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)
题目链接:https://vjudge.net/problem/UVA-11992
题意:n*m的矩阵,每次对一个子矩阵操作,有三种操作:加x,设置为x,查询。查询返回子矩阵和、最小值、最大值
n很小(<=20),所以可以开20棵线段树,每次操作按行更新。
特别小心put和add两个延迟标记,坑老惨了。
put初始化-1最简单的坑,略过。
build的时候要每一个节点都要clear,不能只clear叶子。因为会有直接差没操作的子矩阵(因为初始化都是0)。
数组开大。。。
add的话,什么都不用管。
put的话,在update的时候要把那时候的add标记清空,不要忘记在向下传递的时候,也要清空!
上述几点都注意到,这题就能A。感觉写了一道模拟题。
#include <bits/stdc++.h>
using namespace std; #define lrt rt << 1
#define rrt rt << 1 | 1
typedef struct Node {
int lo, hi, sum;
int add, put;
}Node;
const int maxn = ;
const int maxm = ;
Node seg[maxn][maxm<<];
int n, m, q; void pushup(Node* seg, int rt) {
seg[rt].sum = seg[lrt].sum + seg[rrt].sum;
seg[rt].lo = min(seg[lrt].lo, seg[rrt].lo);
seg[rt].hi = max(seg[lrt].hi, seg[rrt].hi);
} void pushdown(Node* seg, int rt, int l, int r) {
int mid = (l + r) >> ;
if(seg[rt].put != -) {
seg[lrt].sum = (mid - l + ) * seg[rt].put;
seg[rrt].sum = (r - mid) * seg[rt].put;
seg[lrt].lo = seg[lrt].hi = seg[rt].put;
seg[rrt].lo = seg[rrt].hi = seg[rt].put;
seg[lrt].put = seg[rrt].put = seg[rt].put;
seg[lrt].add = seg[rrt].add = ;
seg[rt].put = -;
}
if(seg[rt].add) {
seg[lrt].sum += (mid - l + ) * seg[rt].add;
seg[rrt].sum += (r - mid) * seg[rt].add;
seg[lrt].lo += seg[rt].add;
seg[lrt].hi += seg[rt].add;
seg[rrt].lo += seg[rt].add;
seg[rrt].hi += seg[rt].add;
seg[lrt].add += seg[rt].add;
seg[rrt].add += seg[rt].add;
seg[rt].add = ;
}
} void build(Node* seg, int l, int r, int rt) {
seg[rt].lo = seg[rt].hi = seg[rt].sum = seg[rt].add = ;
seg[rt].put = -;
if(l == r) return;
int mid = (l + r) >> ;
build(seg, l, mid, lrt);
build(seg, mid+, r, rrt);
} void update(Node* seg, int L, int R, int l, int r, int rt, int val, int type) {
if(L <= l && r <= R) {
if(type == ) {
seg[rt].lo += val;
seg[rt].hi += val;
seg[rt].add += val;
seg[rt].sum += (r - l + ) * val;
}
else if(type == ) {
seg[rt].lo = val;
seg[rt].hi = val;
seg[rt].put = val;
seg[rt].sum = (r - l + ) * val;
seg[rt].add = ;
}
return;
}
pushdown(seg, rt, l, r);
int mid = (l + r) >> ;
if(L <= mid) update(seg, L, R, l, mid, lrt, val, type);
if(mid < R) update(seg, L, R, mid+, r, rrt, val, type);
pushup(seg, rt);
} Node query(Node* seg, int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) return seg[rt];
pushdown(seg, rt, l, r);
int mid = (l + r) >> ;
Node ret;
ret.lo = 0x7f7f7f7f, ret.hi = , ret.sum = ;
if(L <= mid) {
Node tmp = query(seg, L, R, l, mid, lrt);
ret.lo = min(ret.lo, tmp.lo);
ret.hi = max(ret.hi, tmp.hi);
ret.sum += tmp.sum;
}
if(mid < R) {
Node tmp = query(seg, L, R, mid+, r, rrt);
ret.lo = min(ret.lo, tmp.lo);
ret.hi = max(ret.hi, tmp.hi);
ret.sum += tmp.sum;
}
pushup(seg, rt);
return ret;
} int main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
int type, x1, y1, x2, y2, val;
while(~scanf("%d%d%d",&n,&m,&q)) {
for(int i = ; i <= n; i++) {
build(seg[i], , m, );
}
while(q--) {
scanf("%d%d%d%d%d",&type,&x1,&y1,&x2,&y2);
if(type == ) {
Node ret, tmp;
ret.lo = 0x7f7f7f7f, ret.hi = , ret.sum = ;
for(int i = x1; i <= x2; i++) {
tmp = query(seg[i], y1, y2, , m, );
ret.lo = min(ret.lo, tmp.lo);
ret.hi = max(ret.hi, tmp.hi);
ret.sum += tmp.sum;
}
printf("%d %d %d\n", ret.sum, ret.lo, ret.hi);
}
else {
scanf("%d", &val);
for(int i = x1; i <= x2; i++) {
update(seg[i], y1, y2, , m, , val, type);
}
}
}
}
return ;
}
[uva11992]Fast Matrix Operations(多延迟标记,二维线段树,区间更新)的更多相关文章
- HDU 4819 Mosaic (二维线段树&区间最值)题解
思路: 二维线段树模板题,马克一下,以后当模板用 代码: #include<cstdio> #include<cmath> #include<cstring> #i ...
- HDU 1823 Luck and Love (二维线段树&区间最值)题解
思路: 树套树,先维护x树,再维护y树,多练练应该就能懂了 代码: #include<cstdio> #include<cmath> #include<cstring&g ...
- UVA11992 - Fast Matrix Operations(段树部分的变化)
UVA11992 - Fast Matrix Operations(线段树区间改动) 题目链接 题目大意:给你个r*c的矩阵,初始化为0. 然后给你三种操作: 1 x1, y1, x2, y2, v ...
- poj 2155:Matrix(二维线段树,矩阵取反,好题)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17880 Accepted: 6709 Descripti ...
- POJ 2155 Matrix (二维线段树)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17226 Accepted: 6461 Descripti ...
- POJ 2155 Matrix (二维线段树入门,成段更新,单点查询 / 二维树状数组,区间更新,单点查询)
题意: 有一个n*n的矩阵,初始化全部为0.有2中操作: 1.给一个子矩阵,将这个子矩阵里面所有的0变成1,1变成0:2.询问某点的值 方法一:二维线段树 参考链接: http://blog.csdn ...
- ZOJ 1859 Matrix Searching(二维线段树)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...
- poj 2155 matrix 二维线段树 线段树套线段树
题意 一个$n*n$矩阵,初始全为0,每次翻转一个子矩阵,然后单点查找 题解 任意一种能维护二维平面的数据结构都可以 我这里写的是二维线段树,因为四分树的写法复杂度可能会退化,因此考虑用树套树实现二维 ...
- 洛谷P3437 [POI2006]TET-Tetris 3D(二维线段树 标记永久化)
题意 题目链接 Sol 二维线段树空间复杂度是多少啊qwqqq 为啥这题全网空间都是\(n^2\)还有人硬要说是\(nlog^2n\)呀.. 对于这题来说,因为有修改操作,我们需要在外层线段树上也打标 ...
- bzoj 1513 POI2006 Tet-Tetris 3D 二维线段树+标记永久化
1511: [POI2006]OKR-Periods of Words Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 351 Solved: 220[S ...
随机推荐
- 关于如何介绍spring框架。
一.介绍Spring 1.Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架. 2.概念:轻量级的IOC(控制反转或者依赖注入).AOP(面向切面或者面向方面) ...
- Dubbo java.io.IOException: Can not lock the registry cache file
跑单测用例的时候,以前执行成功的用例,运行时控制台仍然会报 dubbo 相关的错误: Failed to save registry store file, cause: Can not lock t ...
- windows phone 8.1开发SQlite数据库引用安装
原文出自:http://www.bcmeng.com/windows-phone-sqlite/ windows phone 8.1开发SQlite数据库引用安装 第一步: 安装SQlite forw ...
- Cesium原理篇:3D Tiles(3)个人总结
个人结论:目前,在演示层面,3D Tiles问题不大,但项目应用上就不够成熟了,所以问问自己,你是想吃瓜呢还是想吃螃蟹? 好的方面 数据规范 我非常喜欢glTF的整体设计,概括有四点:第一,数据块(B ...
- windos环境apache+mysql+php+Discuz的安装配置
首先是相关软件的下载:PHP.Apache和Mysql软件以及VC库.相关软件可到我的百度网盘下载,百度网盘:http://pan.baidu.com/s/1o6DYcMu 相关软件的直接下载地址: ...
- 区分 点操作符+属性名 和 getAttribute()
在用DOM操作控制HTML时,很多初学者会把 点操作符+属性名 与getAttribute("属性名") 混淆,误以为这两种方法是等价的. 实际上, 通过getAttribute( ...
- Error--解决使用Application Loader提交ipa包审核时的报错:ERROR ITMS-90168: "The binary you uploaded was invalid."
在提交iTunes Connect审核时,使用Application Loader提交ipa包时报错:ERROR ITMS-90168: "The binary you uploaded w ...
- SearchBar简单展示
import UIKit class SearchViewController: UIViewController,UISearchBarDelegate { let SCREEN_WIDTH = U ...
- POPTEST老李分享session,cookie的安全性以及区别 3
如何查看服务器端输送到我们电脑中的这些Cookie信息: 点开IE浏览器或其他浏览器,在菜单栏中有工具选项,点开有InterNet选项: Cookie名称.来源.文件格式( ...
- 手机自动化测试:appium源码分析之bootstrap十七
手机自动化测试:appium源码分析之bootstrap十七 poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣 ...