uva 11992
题意:
给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作。
1 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素add上val;
2 x1 y1 x2 y2 val 表示将(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素set为val;
3 x1 y1 x2 y2 val 表示输出(x1,y1,x2,y2)(x1<=x2,y1<=y2)子矩阵中的所有元素的sum,最大最小值
链接:
https://vjudge.net/contest/203644#problem/B
题解:
显然这两个操作是可以用线段树来维护的,但关键在于二维
注意到n<=20 ,所以可以考虑对每一行对列进行线段树维护
另外修改为v和增加v两个操作的同时存在
要注意:修改为v要清空增加v 并且先处理修改为v后处理增加v
代码:
- #include <bits/stdc++.h>
- #define mid (h+t)/2
- using namespace std;
- struct re{int a,b,c;} tmp;
- struct ree{int h,t,x1,x2,x3,lazy1,lazy2;}p[][];
- void build(int hh,int x,int h,int t)
- {
- p[hh][x].h=h; p[hh][x].t=t; p[hh][x].x3=;
- if (h==t)
- {
- return;
- }
- build(hh,x*,h,mid); build(hh,x*+,mid+,t);
- };
- void down(int hh,int x)
- {
- if (p[hh][x].lazy2!=)
- {
- p[hh][x].x1=(p[hh][x].t-p[hh][x].h+)*p[hh][x].lazy2;
- p[hh][x].x2=p[hh][x].lazy2;
- p[hh][x].x3=p[hh][x].lazy2;
- p[hh][x*].lazy2=p[hh][x].lazy2;
- p[hh][x*+].lazy2=p[hh][x].lazy2;
- p[hh][x*].lazy1=p[hh][x*+].lazy1=;
- p[hh][x].lazy2=;
- }
- p[hh][x].x1+=(p[hh][x].t-p[hh][x].h+)*p[hh][x].lazy1;
- p[hh][x].x2+=p[hh][x].lazy1;
- p[hh][x].x3+=p[hh][x].lazy1;
- p[hh][x*].lazy1+=p[hh][x].lazy1;
- p[hh][x*+].lazy1+=p[hh][x].lazy1;
- p[hh][x].lazy1=;
- };
- void change1(int hh,int x,int sum,int h,int t)
- {
- down(hh,x);
- if (h>p[hh][x].t || p[hh][x].h>t) return;
- if (h<=p[hh][x].h && p[hh][x].t<=t)
- {
- p[hh][x].lazy1+=sum,down(hh,x);
- return;
- }
- change1(hh,x*,sum,h,t);
- change1(hh,x*+,sum,h,t);
- p[hh][x].x1=p[hh][x*].x1+p[hh][x*+].x1;
- p[hh][x].x2=max(p[hh][x*].x2,p[hh][x*+].x2);
- p[hh][x].x3=min(p[hh][x*].x3,p[hh][x*+].x3);
- };
- void change2(int hh,int x,int sum,int h,int t)
- {
- down(hh,x);
- if (h>p[hh][x].t || p[hh][x].h>t) return;
- if (h<=p[hh][x].h && p[hh][x].t<=t)
- {
- p[hh][x].lazy1=,p[hh][x].lazy2=sum,down(hh,x);
- return;
- }
- change2(hh,x*,sum,h,t);
- change2(hh,x*+,sum,h,t);
- p[hh][x].x1=p[hh][x*].x1+p[hh][x*+].x1;
- p[hh][x].x2=max(p[hh][x*].x2,p[hh][x*+].x2);
- p[hh][x].x3=min(p[hh][x*].x3,p[hh][x*+].x3);
- };
- re query(int hh,int x,int h,int t)
- {
- re tmp,tmp1,tmp2;
- tmp.a=; tmp.b=; tmp.c=;
- down(hh,x);
- if (h>p[hh][x].t || p[hh][x].h>t) return(tmp);
- if (h<=p[hh][x].h && p[hh][x].t<=t)
- {
- tmp.a=p[hh][x].x1,tmp.b=p[hh][x].x2,tmp.c=p[hh][x].x3;
- return(tmp);
- }
- tmp1=query(hh,x*,h,t); tmp2=query(hh,x*+,h,t);
- tmp.a=tmp1.a+tmp2.a; tmp.b=max(tmp1.b,tmp2.b); tmp.c=min(tmp1.c,tmp2.c);
- return(tmp);
- };
- int main()
- {
- int n,m,k,a1,a2,a3,a4,a5,a6;
- while (cin>>n>>m>>k)
- {
- memset(p,,sizeof(p));
- for (int i=; i<=n;i++)
- build(i,,,m);
- for (int i=;i<=k;i++)
- {
- cin>>a1>>a2>>a3>>a4>>a5;
- if (a1==)
- {
- cin>>a6;
- for (int j=a2;j<=a4;j++)
- change1(j,,a6,a3,a5);
- }
- if (a1==)
- {
- cin>>a6;
- for (int j=a2;j<=a4;j++)
- change2(j,,a6,a3,a5);
- }
- if (a1==)
- {
- int sum1=,maxn=,minn=;
- for (int j=a2;j<=a4;j++)
- {
- tmp=query(j,,a3,a5);
- sum1+=tmp.a;
- maxn=max(maxn,tmp.b);
- minn=min(minn,tmp.c);
- }
- cout<<sum1<<" "<<minn<<" "<<maxn<<endl;
- }
- }
- }
- return();
- }
uva 11992的更多相关文章
- UVA 11992 - Fast Matrix Operations(段树)
UVA 11992 - Fast Matrix Operations 题目链接 题意:给定一个矩阵,3种操作,在一个矩阵中加入值a,设置值a.查询和 思路:因为最多20列,所以全然能够当作20个线段树 ...
- Fast Matrix Operations(UVA)11992
UVA 11992 - Fast Matrix Operations 给定一个r*c(r<=20,r*c<=1e6)的矩阵,其元素都是0,现在对其子矩阵进行操作. 1 x1 y1 x2 y ...
- UVA 11992 Fast Matrix Operations(线段树:区间修改)
题目链接 2015-10-30 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=s ...
- uva 11992 为矩阵更新查询段树
http://uva.onlinejudge.org/index.php? option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 线段树(多维+双成段更新) 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
比较综合的一道题目. 二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max 写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递 ...
- uva 11992 - Fast Matrix Operations
简单的线段树的题: 有两种方法写这个题,目前用的熟是这种慢点的: 不过不知道怎么老是T: 感觉网上A过的人的时间度都好小,但他们都是用数组实现的 难道是指针比数组慢? 好吧,以后多用数组写写吧! 超时 ...
- UVA 11992 线段树
input r c m r<=20,1<=m<=20000 m行操作 1 x1 y1 x2 y2 v add v 2 x1 y1 x2 y2 v s ...
- UVA - 11992:Fast Matrix Operations
线段树,注意tag优先级 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cs ...
随机推荐
- 休眠与开机自动运行等VC代码
//根据文件句柄,获取文件名 #include <windows.h> #include <stdio.h> #include <tchar.h> #include ...
- AIX系统下sed的用法与实例——查询/打印/替换字符串并生成文件/删除
sed是AIX中非常重要的文本流编辑器,它对输入的文本进行查询/打印/替换/删除等操作,并将结果写到标准输出.sed 命令包含很多功能,用于选择要修改的行,并只对选择的行作更改. 首先,使用sed命令 ...
- Linux内核初探
内存管理之内存寻址 内存管理是迄今为止Unix内核中最复杂的活动 虚拟内存: 所有新近的Unix系统都提供一种有用的抽象, 叫作虚拟内存(virtual memory): 虚拟内存可以理解为一种逻辑层 ...
- 025_lua脚本语言
一.--cat /opt/nginx/conf/conf.dlua_package_path '/opt/nginx/conf/lua/?.lua;;'; --lua模块路径,其中”;;”表示默认搜索 ...
- 所有ORM操作 (第二版)
####################################################################### # PUBLIC METHODS THAT ALTER ...
- Ex 2_22 两个有序列表合并后的第k小元素..._第四次作业
package org.xiu68.ch02; public class Ex2_22 { public static void main(String[] args) { // TODO Auto- ...
- js实现两种排序算法——冒泡、快速排序
* 一:冒牌排序1思想:冒泡排序思想:每一次对比相邻两个数据的大小,小的排在前面,如果前面的数据比后面的大就交换这两个数的位置要实现上述规则需要用到两层for循环,外层从第一个数到倒数第二个数,内层从 ...
- Elasticsearch入门,这一篇就够了
实时搜索引擎Elasticsearch Elasticsearch(简称ES)是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进 ...
- 关于如何实现Android透明状态栏的总结
开门见山. 原来做的效果,如下图(顶部有一条明显的橙色状态栏): a1.gif 改过之后(顶部状态栏是透明的): p2.gif 我发现网上写的一些文章,不够简洁明了,我整理了一下,复制粘贴一下 ...
- pytorch 参数初始化
https://blog.csdn.net/daydayjump/article/details/80899029