UVa11992
来源:互联网 发布:java下载原理 编辑:程序博客网 时间:2024/05/17 05:06
题目链接
简介:
给出一个矩阵,支持以下操作:
子矩阵加,子矩阵赋值,子矩阵查询元素和,最大值和最小值
分析:
矩阵不超过20行
所以我们可以每一行都建一棵线段树,这样就转换成了一维问题
set和add操作,我们默认set的优先级高
但是我认为,因为我们随时进行push
应该不会有操作叠加或者同时又add和set标记的情况
tip
UVa上一直RE。。。STO
//这里写RE的代码片#include<cstdio>#include<cstring>#include<iostream>using namespace std;const int INF=1<<20;const int N=50003;struct node{ int x,y,mn,mx,sum,st,ad;};node t[21][N<<2];int n,m,q;void update(node *t,int bh){ int lc=bh<<1; int rc=(bh<<1)+1; t[bh].mn=min(t[lc].mn,t[rc].mn); t[bh].mx=max(t[lc].mx,t[rc].mx); t[bh].sum=t[lc].sum+t[rc].sum; return;}void push(node *t,int bh){ int lc=bh<<1; int rc=bh<<1|1; if (t[bh].st!=-1||t[bh].ad!=0) { if (t[bh].st!=-1) { t[lc].sum=(t[lc].y-t[lc].x+1)*t[bh].st; t[lc].mn=t[bh].st; t[lc].mx=t[bh].st; t[rc].sum=(t[rc].y-t[rc].x+1)*t[bh].st; t[rc].mn=t[bh].st; t[rc].mx=t[bh].st; t[lc].st=t[rc].st=t[bh].st; } if (t[bh].ad!=0) { t[lc].sum+=(t[lc].y-t[lc].x+1)*t[bh].ad; t[lc].mn+=t[bh].ad; t[lc].mx+=t[bh].ad; t[rc].sum+=(t[rc].y-t[rc].x+1)*t[bh].ad; t[rc].mn+=t[bh].ad; t[rc].mx+=t[bh].ad; t[lc].ad+=t[bh].ad; t[rc].ad+=t[bh].ad; } t[bh].ad=0; t[bh].st=-1; }}void build(node *t,int bh,int l,int r){ t[bh].x=l; t[bh].y=r; t[bh].st=-1; t[bh].ad=0; t[bh].mn=0; t[bh].mx=0; t[bh].sum=0; if (l==r) return; int mid=(l+r)>>1; build(t,bh<<1,l,mid); build(t,(bh<<1)+1,mid+1,r); update(t,bh);}void add(node *t,int bh,int l,int r,int v){ push(t,bh); if (t[bh].x>=l&&t[bh].y<=r) { t[bh].sum+=(t[bh].y-t[bh].x+1)*v; t[bh].mn+=v; t[bh].mx+=v; t[bh].ad=v; return; } int mid=(t[bh].x+t[bh].y)>>1; if (l<=mid) add(t,bh<<1,l,r,v); if (r>mid) add(t,(bh<<1)+1,l,r,v); update(t,bh);}void set(node *t,int bh,int l,int r,int v){ push(t,bh); if (t[bh].x>=l&&t[bh].y<=r) { t[bh].sum=(t[bh].y-t[bh].x+1)*v; t[bh].mn=v; t[bh].mx=v; t[bh].st=v; return; } int mid=(t[bh].x+t[bh].y)>>1; if (l<=mid) set(t,bh<<1,l,r,v); if (r>mid) set(t,(bh<<1)+1,l,r,v); update(t,bh);}int askmin(node *t,int bh,int l,int r){ push(t,bh); if (t[bh].x>=l&&t[bh].y<=r) return t[bh].mn; int mid=(t[bh].x+t[bh].y)>>1; int ans=INF; if (l<=mid) ans=min(ans,askmin(t,bh<<1,l,r)); if (r>mid) ans=min(ans,askmin(t,(bh<<1)+1,l,r)); return ans;}int askmax(node *t,int bh,int l,int r){ push(t,bh); if (t[bh].x>=l&&t[bh].y<=r) return t[bh].mx; int mid=(t[bh].x+t[bh].y)>>1; int ans=-INF; if (l<=mid) ans=max(ans,askmax(t,bh<<1,l,r)); if (r>mid) ans=max(ans,askmax(t,(bh<<1)+1,l,r)); return ans;}int asksum(node *t,int bh,int l,int r){ push(t,bh); if (t[bh].x>=l&&t[bh].y<=r) return t[bh].sum; int mid=(t[bh].x+t[bh].y)>>1; int ans=0; if (l<=mid) ans+=asksum(t,bh<<1,l,r); if (r>mid) ans+=asksum(t,(bh<<1)+1,l,r); return ans;}int main(){ while (scanf("%d%d%d",&n,&m,&q)!=EOF) { for (int i=1;i<=n;i++) build(t[i],1,1,m); int opt,xa,ya,xb,yb,v; for (int i=1;i<=q;i++) { scanf("%d%d%d%d%d",&opt,&xa,&ya,&xb,&yb); if (opt==1) { scanf("%d",&v); for (int j=xa;j<=xb;j++) add(t[j],1,ya,yb,v); } else if (opt==2) { scanf("%d",&v); for (int j=xa;j<=xb;j++) set(t[j],1,ya,yb,v); } else { int minn=INF; int maxx=-INF; int sum=0; for (int j=xa;j<=xb;j++) { minn=min(minn,askmin(t[j],1,ya,yb)); maxx=max(maxx,askmax(t[j],1,ya,yb)); sum+=asksum(t[j],1,ya,yb); } printf("%d %d %d\n",sum,minn,maxx); } } } return 0;}
阅读全文
0 0
- uva11992
- uva11992
- UVa11992
- Fast Matrix Operation UVA11992
- 线段树UVa11992
- UVA11992 - Fast Matrix Operations
- uva11992 Fast Matrix Operations
- UVA11992 线段树
- 二维线段树 uva11992
- uva11992区间修改线段树
- Uva11992——线段树区间更新
- UVA11992----线段树的成段更新
- UVA11992不错的线段树段更新
- uva11992(线段树-区间修改)
- UVA11992:Fast Matrix Operations(线段树)
- uva11992 Fast Matrix Operations (线段树)
- UVA11992二维线段树区间修改
- BNU20410 UVA11992 线段树区间更新
- List 分组实现
- javaScript中eval()函数详解
- 前端学习路线--计划制定
- 2017第十八届广州金融理财博览会会刊(参展商名录)
- 在单链表里如何将所有的unique data进行average
- UVa11992
- 【Kafka源码】Kafka代码模块
- mongodb命令
- Android 获取电池电量
- Powershell 5.1 脚本化配置 Windows 2016 & SQL Server 2016 无域 AlwaysOn AG
- 关于Unable to read additional data from server sessionid 0x0问题的解决。
- 解决Quartz的Job类中使用注解Service为null的办法
- String、StringBuffer与StringBuilder之间区别
- 设计模式简介