UVA 11992
来源:互联网 发布:凯恩之角 数据库 编辑:程序博客网 时间:2024/05/08 02:39
[Submit] [Go Back] [Status]
Description
Problem F
Fast Matrix Operations
There is a matrix containing at most 106 elements divided into r rows and c columns. Each element has a location (x,y) where 1<=x<=r,1<=y<=c. Initially, all the elements are zero. You need to handle four kinds of operations:
1 x1 y1 x2 y2 v
Increment each element (x,y) in submatrix (x1,y1,x2,y2) by v (v>0)
2 x1 y1 x2 y2 v
Set each element (x,y) in submatrix (x1,y1,x2,y2) to v
3 x1 y1 x2 y2
Output the summation, min value and max value of submatrix (x1,y1,x2,y2)
In the above descriptions, submatrix (x1,y1,x2,y2) means all the elements (x,y) satisfying x1<=x<=x2 and y1<=x<=y2. It is guaranteed that 1<=x1<=x2<=r, 1<=y1<=y2<=c. After any operation, the sum of all the elements in the matrix does not exceed 109.
Input
There are several test cases. The first line of each case contains three positive integers r, c, m, where m (1<=m<=20,000) is the number of operations. Each of the next m lines contains a query. There will be at most twenty rows in the matrix. The input is terminated by end-of-file (EOF). The size of input file does not exceed 500KB.
Output
For each type-3 query, print the summation, min and max.
Sample Input
4 4 81 1 2 4 4 53 2 1 4 41 1 1 3 4 23 1 2 4 43 1 1 3 42 2 1 4 4 23 1 2 4 41 1 1 4 3 3
Output for the Sample Input
45 0 578 5 769 2 739 2 7
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yeji Shen, Dun Liang
Note: Please make sure to test your program with the gift I/O files before submitting!
#include<cstdio>#include<vector>#include<algorithm>#include<cstring>using namespace std;int r,c,m;const int maxn = 1000010;const int inf = 1000000009;int amax[22],amin[22],asum[22];struct node{ int l,r,sum,min,max,add,cov;}T[maxn*5];void build(int i,int id,int l,int r){ int t=4*i*c; T[id+t].l=l; T[id+t].r=r; T[id+t].add=0; T[id+t].cov=-1;T[id+t].sum=T[id+t].min=T[id+t].max=0; if(l==r) return ; int m=(l+r)>>1; build(i,id<<1,l,m);build(i,id<<1|1,m+1,r);}void pushDown(int k,int id){ int t=4*k*c; if(T[t+id].cov!=-1){ if(T[t+id].l!=T[t+id].r){ T[t+(id<<1)].add=T[t+(id<<1|1)].add=0; T[t+(id<<1)].cov=T[t+(id<<1|1)].cov=T[t+id].cov; T[t+(id<<1)].max=T[t+(id<<1|1)].max=T[t+id].cov; T[t+(id<<1)].min=T[t+(id<<1|1)].min=T[t+id].cov; T[t+(id<<1)].sum=(T[t+(id<<1)].r-T[t+(id<<1)].l+1)*T[t+id].cov; T[t+(id<<1|1)].sum=(T[t+(id<<1|1)].r-T[t+(id<<1|1)].l+1)*T[t+id].cov; } T[t+id].cov=-1; } int tt=T[t+id].add; if(T[t+id].add>0){ if(T[t+id].l!=T[t+id].r){ T[t+(id<<1)].add+=tt; T[t+(id<<1|1)].add+=tt; T[t+(id<<1)].max+=tt; T[t+(id<<1|1)].max+=tt; T[t+(id<<1)].min+=tt; T[t+(id<<1|1)].min+=tt; T[t+(id<<1)].sum+=tt*(T[t+(id<<1)].r-T[t+(id<<1)].l+1); T[t+(id<<1|1)].sum+=tt*(T[t+(id<<1|1)].r-T[t+(id<<1|1)].l+1); } T[t+id].add=0; }}void pushUp(int k,int id){ int t=4*k*c; T[t+id].max=max(T[t+(id<<1)].max,T[t+(id<<1|1)].max); T[t+id].min=min(T[t+(id<<1)].min,T[t+(id<<1|1)].min); T[t+id].sum=T[t+(id<<1)].sum+T[t+(id<<1|1)].sum;}void update_add(int k,int id,int l,int r,int val){ int t=4*k*c+id; if(T[t].l==l&&T[t].r==r){ T[t].add+=val; T[t].max+=val; T[t].min+=val; T[t].sum+=val*(r-l+1); return ; } pushDown(k,id); int m=(T[t].l+T[t].r)>>1; if(m>=r) update_add(k,id<<1,l,r,val); else if(m<l) update_add(k,id<<1|1,l,r,val); else{ update_add(k,id<<1,l,m,val); update_add(k,id<<1|1,m+1,r,val); } pushUp(k,id);}void update_set(int k,int id,int l,int r,int val){ int t=4*k*c+id; if(T[t].l==l&&T[t].r==r){ T[t].cov=val; T[t].add=0; T[t].max=val; T[t].min=val; T[t].sum=val*(r-l+1); return ; } pushDown(k,id); int m=(T[t].l+T[t].r)>>1; if(m>=r) update_set(k,id<<1,l,r,val); else if(m<l) update_set(k,id<<1|1,l,r,val); else{ update_set(k,id<<1,l,m,val); update_set(k,id<<1|1,m+1,r,val); } pushUp(k,id);}void query(int k,int id,int l,int r){ int t=4*k*c+id; if(T[t].l==l&&T[t].r==r){ asum[k]+=T[t].sum; amax[k]=max(amax[k],T[t].max); amin[k]=min(amin[k],T[t].min); return ; } pushDown(k,id); int m=(T[t].l+T[t].r)>>1; if(m>=r) query(k,id<<1,l,r); else if(m<l) query(k,id<<1|1,l,r); else{ query(k,id<<1,l,m); query(k,id<<1|1,m+1,r); } pushUp(k,id);}int main(){ while(scanf("%d%d%d",&r,&c,&m)!=EOF){ for(int i=1;i<=r;i++) build(i,1,1,c+1); int id,x1,y1,x2,y2,v; while(m--){ scanf("%d",&id); if(id==1){ scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v); for(int i=x1;i<=x2;i++) update_add(i,1,y1,y2,v); } else if(id==2){ scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v); for(int i=x1;i<=x2;i++) update_set(i,1,y1,y2,v); } else { memset(asum,0,sizeof(asum)); scanf("%d%d%d%d",&x1,&y1,&x2,&y2); int sum=0,rmax=-inf,rmin=inf; for(int i=x1;i<=x2;i++) amax[i]=-inf,amin[i]=inf; for(int i=x1;i<=x2;i++){ query(i,1,y1,y2); sum+=asum[i]; rmax=max(rmax,amax[i]); rmin=min(rmin,amin[i]); } printf("%d %d %d\n",sum,rmin,rmax); } } } return 0;}
- UVA 11992
- UVA 11992
- UVa 11992
- Uva 11992
- UVA - 11992
- UVA 11992
- uva 11992(线段树)
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- eclipse 3.4.0 反编译
- java 工厂方法模式
- 那些日夜想念你的泪水 是咸的我的心还是会痛
- hdu 4435 天津赛e题 贪心+乱搞
- Android开发——MediaProvider源码分析 .
- UVA 11992
- linux 删除指定文件夹下边的.svn文件的命令
- 对于DLL不是不能,而是不想
- 无线医疗:一场医疗电子技术革命
- JAVA--封装一类对象,功能是随机产生一个2000年后的年份,并输出该年2月的日历页,需处理闰年的问题
- 多内核处理器架构改善嵌入式系统性能
- 黑白遐想的伤感情侣日志发布:故作坚强的姿态 我始终放不下来
- OpenRTMFP/Cumulus开发笔记(4) Cumulus线程详解 (续1)
- 杜比全景声深受好莱坞青睐