线段树区间更新——UVA 11992
来源:互联网 发布:md5加密java代码 密钥 编辑:程序博客网 时间:2024/05/21 03:59
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
题意:对一个r*c的矩阵,元素个数不会超过10^6,有3个操作,任何运算的和不会超过10^9。
1 x1, y1, x2, y2, v (x1 <= x <= x2, y1 <= y <= y2) 表示把此子矩阵里的元素全加上v。
2 x1, y1, x2, y2, v (x1 <= x <= x2, y1 <= y <= y2) 表示把此子矩阵里的元素全变成v。
3 x1, y1, x2, y2, (x1 <= x <= x2, y1 <= y <= y2) 表示求此子矩阵的最大元素,最小元素,所有元素和。
PS:题目描述有点不具体,虽然影响不大。。。(没有说明操作2的v值范围,还有后面的y1 <= x <= y2)
思路: 把多维展开成一维,就相当于每次询问就要查询 x2 - x1 + 1 次(一行一行地更新)。总区间为 r*c ,每次查询区间为left = y1 + c*(x-1); right = y2 + c*(x - 1); 这样就变成普通的区间覆盖和增加问题。只需要注意一点,就是再遇到覆盖时就要把增加标记置0。这样,在往下更新时就先判断覆盖,再判断增加。
#include <stdio.h>#include <stdlib.h>#include <string.h>#define MAX(x,y) ((x)>(y)?(x):(y))#define MIN(x,y) ((x)<(y)?(x):(y))#define MS(x,y) memset(x, y, sizeof(x))const int MAXN=1000100;const int INF=1000000005;int sum[MAXN<<2];int min[MAXN<<2];int max[MAXN<<2];int add[MAXN<<2];int cover[MAXN<<2];void down(int root, int len){int ll = root<<1;int rr = root<<1|1;int cov = cover[root];int ad = add[root];if(cov){//有覆盖就要向下更新cover[ll] = cover[rr] = cov;sum[ll] = cov * (len - (len>>1));sum[rr] = cov * (len>>1);max[ll] = max[rr] = cov;min[ll] = min[rr] = cov;add[ll] = add[rr] = 0;//把增加置空cover[root] = 0;}if(ad){//有增加就说明最后的操作不是覆盖add[ll] += ad;add[rr] += ad;sum[ll] += ad * (len - (len>>1));sum[rr] += ad * (len>>1);max[ll] += ad;max[rr] += ad;min[ll] += ad;min[rr] += ad;add[root] = 0;}}void up(int root){int ll = root<<1;int rr = root<<1|1;sum[root] = sum[ll] + sum[rr];max[root] = MAX(max[ll], max[rr]);min[root] = MIN(min[ll], min[rr]);}void IS(int root, int left, int right, int l, int r, int v, int flag){if(l == left && right == r){if(1 == flag){add[root] += v;sum[root] += v * (right - left + 1);max[root] += v;min[root] += v;}else{cover[root] = v;add[root] = 0;//把增加置空sum[root] = v * (right - left + 1);max[root] = v;min[root] = v;}return;}down(root, right - left + 1);int mid = (left + right)>>1;if(r <= mid) IS(root<<1, left, mid, l, r, v, flag);else if(l > mid) IS(root<<1|1, mid + 1, right, l, r, v, flag);else{IS(root<<1, left, mid, l, mid, v, flag);IS(root<<1|1, mid + 1, right, mid + 1, r, v, flag);}up(root);}typedef struct{int sum, min, max;}Node;Node query(int root, int left, int right, int l, int r){Node N,NL,NR;if(l == left && right == r){N.sum = sum[root];N.max = max[root];N.min = min[root];return N;}down(root, right - left + 1);int mid = (left + right)>>1;if(r <= mid) return N = query(root<<1, left, mid, l, r);else if(l > mid) return N = query(root<<1|1, mid+1, right, l, r);else{NL = query(root<<1, left, mid, l, mid);NR = query(root<<1|1, mid+1, right, mid+1, r);N.sum = NL.sum + NR.sum;N.max = MAX(NL.max, NR.max);N.min = MIN(NL.min, NR.min);return N;}up(root);}int main(){//freopen("in.txt","r",stdin);int r,c,m,n;while(~scanf("%d%d%d", &r,&c,&m)){MS(add, 0);MS(cover, 0);MS(sum, 0);MS(max, 0);MS(min, 0);n = r*c;int o,x1,y1,x2,y2,v;int i,beg,end,Sum,Min,Max;Node node;while(m--){scanf("%d", &o);if(1 == o){scanf("%d%d%d%d%d", &x1,&y1,&x2,&y2,&v);for(i=x1; i<=x2; i++){beg = y1 + c * (i - 1);end = y2 + c * (i - 1);IS(1, 1, n, beg, end, v, 1);}}else if(2 == o){scanf("%d%d%d%d%d", &x1,&y1,&x2,&y2,&v);for(i=x1; i<=x2; i++){beg = y1 + c * (i - 1);end = y2 + c * (i - 1);IS(1, 1, n, beg, end, v, 0);}}else{scanf("%d%d%d%d", &x1,&y1,&x2,&y2);Sum = 0;Max = 0;Min = INF;for(i=x1; i<=x2; i++){beg = y1 + c * (i - 1);end = y2 + c * (i - 1);node = query(1, 1, n, beg, end);Sum += node.sum;Max = MAX(Max, node.max);Min = MIN(Min, node.min);}printf("%d %d %d\n", Sum, Min, Max);}}}return 0;}
- 线段树区间更新——UVA 11992
- UVA 11992 Fast Matrix Operations (线段树区间更新)
- uva 11992 Fast Matrix Operations (线段树区间更新)
- Uva11992——线段树区间更新
- UVA 11992 (维护多棵线段树,区间更新,区间替换)
- HDOJ 4614 —— 线段树区间更新&区间求和
- UVA 11992 线段树区间更新 + 两个lazy(二维线段树)
- UVA 1232 - SKYLINE(线段树区间更新)
- 线段树区间更新
- 线段树区间更新
- 线段树 区间更新
- 线段树区间更新
- 线段树-区间更新
- 线段树区间更新
- 线段树区间更新
- 线段树区间更新
- 区间更新线段树
- 线段树区间更新
- matlab画图,在图中标出字母
- xcode功能学习
- FastJSON 原理剖析 以及 和 Jackson的对比 who is the most fast!
- 【翻译原创】(一)MonkeyTalk 工具简介
- java--数据结构特性
- 线段树区间更新——UVA 11992
- JAVA学习笔记10——static+this
- poj 2164 (卷包裹算法)
- 总结2014——迷茫以及迷茫过后的坚持
- 算法导论第16章 贪心算法-活动选择问题
- HTML上划线 中划线 下划线实现
- dbcp 连接 postgresql 报org.postgresql.jdbc4.Jdbc4Statement.setQueryTimeout(int)
- mac下搭建openfire
- Robotium 专题(四): Robotium 中部分常用的方法集锦