UVA 11992 Fast Matrix Operations (降维)
题意:对一个矩阵进行子矩阵操作。
元素最多有1e6个,树套树不好开(我不会),把二维坐标化成一维的,一个子矩阵操作分解成多条线段的操作。
一次操作的复杂度是RlogC,很容易找到极端的数据(OJ上实测没有),如果判断一下然后启发式建树复杂度是min(RlogC,ClogR)。
代码中结点没有保存l和r,而且询问是保存在全局变量中,这样做比较省空间。但是也有缺点,比如推区间结点数量的时候会麻烦一点。
#include<bits/stdc++.h>
using namespace std; const int maxn = 1e6+;
int R,C; #define lid (id<<1)
#define rid (id<<1|1)
struct Seg
{
int add,setv;
int Max,Min,sum;
}tr[maxn<<]; #define OP1(id,val)\
tr[id].add += val; tr[id].Max += val; tr[id].Min += val; tr[id].sum += (r-l+)*val;
#define OP2(id,val)\
tr[id].Max = tr[id].setv = tr[id].Min = val; tr[id].add = ; tr[id].sum = val*(r-l+); inline void push_down(int id,int l,int r)
{
int lc = lid, rc = rid, mid = (l+r)>>;
if(tr[id].setv>=){
int &t = tr[id].setv;
swap(r,mid);
OP2(lc,t);
swap(l,r); l++; swap(mid,r);
OP2(rc,t);
l--; swap(mid,l);
t = -;
}
if(tr[id].add>){
int &t = tr[id].add;
swap(r,mid);
OP1(lc,t);
swap(l,r); l++; swap(mid,r);
OP1(rc,t);
l--; swap(mid,l);
t = ;
}
} inline void maintain(int id)
{
int lc = lid, rc = rid;
tr[id].sum = tr[lc].sum + tr[rc].sum;
tr[id].Max = max(tr[lc].Max,tr[rc].Max);
tr[id].Min = min(tr[lc].Min,tr[rc].Min);
} int ql,qr,val;
void add1D(int l = ,int r = R*C-,int id = )
{
if(ql<=l&&r<=qr) { OP1(id,val) return; }
int mid = (l+r)>>, lc = lid, rc = rid;
push_down(id,l,r);
if(ql<=mid) add1D(l,mid,lc);
if(qr>mid) add1D(mid+,r,rc);
maintain(id);
} void set1D(int l = ,int r = R*C-,int id = )
{
if(ql<=l&&r<=qr) { OP2(id,val) return; }
int mid = (l+r)>>, lc = lid, rc = rid;
push_down(id,l,r);
if(ql<=mid) set1D(l,mid,lc);
if(qr>mid) set1D(mid+,r,rc);
maintain(id);
} int queryMax1D(int l = ,int r = R*C-,int id = )
{
if(ql<=l&&r<=qr) { return tr[id].Max; }
int mid = (l+r)>>, lc = lid, rc = rid;
push_down(id,l,r);
int ret = ;
if(ql<=mid) ret = max(ret,queryMax1D(l,mid,lc));
if(qr>mid) ret = max(ret,queryMax1D(mid+,r,rc));
return ret;
} const int INF = 0x3f3f3f3f; int queryMin1D(int l = ,int r = R*C-,int id = )
{
if(ql<=l&&r<=qr) { return tr[id].Min; }
int mid = (l+r)>>, lc = lid, rc = rid;
push_down(id,l,r);
int ret = INF;
if(ql<=mid) ret = min(ret,queryMin1D(l,mid,lc));
if(qr>mid) ret = min(ret,queryMin1D(mid+,r,rc));
return ret;
} int querySum1D(int l = ,int r = R*C-,int id = )
{
if(ql<=l&&r<=qr) { return tr[id].sum; }
int mid = (l+r)>>, lc = lid, rc = rid;
push_down(id,l,r);
int ret = ;
if(ql<=mid) ret += querySum1D(l,mid,lc);
if(qr>mid) ret += querySum1D(mid+,r,rc);
return ret;
} //[0,r)
void add2D(int x1,int y1,int x2,int y2,int v)
{
val = v;
int st = x1*C+y1, len = y2-y1;
for(int x = x1; x <= x2; x++){
ql = st; qr = st+len;
add1D();
st += C;
}
} void set2D(int x1,int y1,int x2,int y2,int v)
{
val = v;
int st = x1*C+y1, len = y2-y1;
for(int x = x1; x <= x2; x++){
ql = st; qr = st+len;
set1D();
st += C;
}
} int querySum2D(int x1,int y1,int x2,int y2)
{
int ret = ;
int st = x1*C+y1, len = y2-y1;
for(int x = x1; x <= x2; x++){
ql = st; qr = st+len;
ret += querySum1D();
st += C;
}
return ret;
} int queryMax2D(int x1,int y1,int x2,int y2)
{
int ret = ;
int st = x1*C+y1, len = y2-y1;
for(int x = x1; x <= x2; x++){
ql = st; qr = st+len;
ret = max(ret,queryMax1D());
st += C;
}
return ret;
} int queryMin2D(int x1,int y1,int x2,int y2)
{
int ret = INF;
int st = x1*C+y1, len = y2-y1;
for(int x = x1; x <= x2; x++){
ql = st; qr = st+len;
ret = min(ret,queryMin1D());
st += C;
}
return ret;
} int main()
{
//freopen("in.txt","r",stdin);
int m;
while(~scanf("%d%d%d",&R,&C,&m)){
ql = ; qr = R*C-; val = ;
set1D();
while(m--){
int op,x1,y1,x2,y2; scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
if(op == ){
int v; scanf("%d",&v);
add2D(x1-,y1-,x2-,y2-,v);
}else if(op == ){
int v; scanf("%d",&v);
set2D(x1-,y1-,x2-,y2-,v);
}else {
x1--;x2--;y1--;y2--;
printf("%d %d %d\n",querySum2D(x1,y1,x2,y2),queryMin2D(x1,y1,x2,y2),queryMax2D(x1,y1,x2,y2));
}
}
}
return ;
}
UVA 11992 Fast Matrix Operations (降维)的更多相关文章
- UVA 11992 - Fast Matrix Operations(段树)
UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...
- uva 11992 Fast Matrix Operations 线段树模板
注意 setsetset 和 addvaddvaddv 标记的下传. 我们可以控制懒惰标记的优先级. 由于 setsetset 操作的优先级高于 addaddadd 操作,当下传 setsetset ...
- 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
题目传送门 题意:训练指南P207 分析:因为矩阵不超过20行,所以可以建20条线段的线段树,支持两个区间更新以及区间查询. #include <bits/stdc++.h> using ...
- UVA 11992 Fast Matrix Operations (二维线段树)
解法:因为至多20行,所以至多建20棵线段树,每行建一个.具体实现如下,有些复杂,慢慢看吧. #include <iostream> #include <cstdio> #in ...
- uva 11992 - Fast Matrix Operations
简单的线段树的题: 有两种方法写这个题,目前用的熟是这种慢点的: 不过不知道怎么老是T: 感觉网上A过的人的时间度都好小,但他们都是用数组实现的 难道是指针比数组慢? 好吧,以后多用数组写写吧! 超时 ...
- 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(段树模板)
主体段树,要注意,因为有set和add操作,当慵懒的标志下推.递归优先set,后复发add,每次运行set行动add马克清0 WA了好几次是由于计算那一段的时候出问题了,可笑的是我对着模板找了一个多小 ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
随机推荐
- C++ 定位构造
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://yiluohuanghun.blog.51cto.com/3407300/1258 ...
- NOIP2015提高组 跳石头 ACM-ICPC2017香港 E(选择/移除+二分答案)
跳石头 题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 NN ...
- 利用C++创建DLL并C#调用
日期:2018年11月26日 环境:window 10,VS2015 community 一.利用C++创建DLL 1.新建项目: 2.打开CreateDLL.cpp文件,并输入测试代码 #inclu ...
- 无监督学习:Deep Generative Mode(深度生成模型)
一 前言 1.1 Creation 据说在费曼死后,人们在他生前的黑板上拍到如图画片,在左上角有道:What i cannot create ,I do not understand. Generat ...
- UOJ #32. 【UR #2】跳蚤公路【Floydbellman-ford】
首先看这个范围很夸张但是其实有限制的也就在1e18*n范围里(走完一圈的边权),然后限制一定是有负环 用Floyd传递闭包,然后设f[i][j][k]为从1走了i步到j并且有k个x的最短路,用B-F处 ...
- python接口测试框架遇到的坑(一)excel数字转文本
一.遇到的问题 python编写接口测试框架中,接口用例使用excel维护,其中预期值(code码的值)20000和实际值总是不一致,后来通过打印type发现一个是unicode,一个是float. ...
- python 之 匿名函数
5.14 匿名函数 lambda x , y : x+y 1 匿名的目的就是要没有名字,给匿名函数赋给一个名字是没有意义的 2 匿名函数的参数规则.作用域关系与有名函数是一样的 3 匿名函数的函数体通 ...
- C 语言实例 - 计算自然数的和
C 语言实例 - 计算自然数的和 自然数是指表示物体个数的数,即由0开始,,,,,,……一个接一个,组成一个无穷的集体,即指非负整数. 实例 - 使用 for #include <stdio.h ...
- according to tld or attribute directive in tag file attribute *** does not accept any expressions
http://stackoverflow.com/questions/13428788/according-to-tld-or-attribute-directive-in-tag-file-attr ...
- Java基础笔记(五)——数据类型转换
数据类型的精度由低到高为:byte < short < char < int < long < float < double 低精度的类型与高精度的类型在进行运算时 ...