比较综合的一道题目。

二维的线段树,支持区间的add和set操作,然后询问子矩阵的sum,min,max

写完这道题也是醉醉哒,代码仓库里还有一份代码就是在query的过程中也pushdown向下传递标记。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxnode = << ;
int _sum, _min, _max, op, x1, x2, y1, y2, x, v; struct IntervalTree
{
int maxv[maxnode], minv[maxnode], sumv[maxnode], setv[maxnode], addv[maxnode]; void maintain(int o, int L, int R)
{
int lc = o*, rc = o*+;
if(R > L)
{
sumv[o] = sumv[lc] + sumv[rc];
minv[o] = min(minv[lc], minv[rc]);
maxv[o] = max(maxv[lc], maxv[rc]);
}
if(setv[o] >= ) { maxv[o] = minv[o] = setv[o]; sumv[o] = (R-L+)*setv[o]; }
if(addv[o]) { minv[o] += addv[o]; maxv[o] += addv[o]; sumv[o] += (R-L+)*addv[o]; }
} void pushdown(int o)
{
int lc = o*, rc = o*+;
if(setv[o] >= )
{
setv[lc] = setv[rc] = setv[o];
addv[lc] = addv[rc] = ;
setv[o] = -;
}
if(addv[o])
{
addv[lc] += addv[o];
addv[rc] += addv[o];
addv[o] = ;
}
} void update(int o, int L, int R)
{
int lc = o*, rc = o*+;
if(y1 <= L && R <= y2)
{
if(op == ) addv[o] += v;
else { setv[o] = v; addv[o] = ; }
}
else
{
pushdown(o); //往下传递标记
int M = (L + R) / ;
if(y1 <= M) update(lc, L, M); else maintain(lc, L, M);
if(y2 > M) update(rc, M+, R); else maintain(rc, M+, R);
}
maintain(o, L, R);
} void query(int o, int L, int R, int add)
{
if(setv[o] >= )
{
int v = setv[o] + addv[o] + add;
_sum += v * (min(y2, R) - max(y1, L) + );
_min = min(_min, v);
_max = max(_max, v);
}
else if(y1 <= L && R <= y2)
{
_sum += sumv[o] + add*(R-L+);
_min = min(_min, minv[o] + add);
_max = max(_max, maxv[o] + add);
}
else
{
int M = (L + R) / ;
if(y1 <= M) query(o*, L, M, add + addv[o]);
if(y2 > M) query(o*+, M+, R, add + addv[o]);
}
}
}; const int maxr = + ;
const int INF = ; IntervalTree tree[maxr]; int main()
{
//freopen("in.txt", "r", stdin); int r, c, m;
while(scanf("%d%d%d", &r, &c, &m) == )
{
memset(tree, , sizeof(tree));
for(int x = ; x <= r; x++)
{
memset(tree[x].setv, -, sizeof(tree[x].setv));
tree[x].setv[] = ;
} while(m--)
{
scanf("%d%d%d%d%d", &op, &x1, &y1, &x2, &y2);
if(op < )
{
scanf("%d", &v);
for(x = x1; x <= x2; x++) tree[x].update(, , c);
}
else
{
_sum = ; _max = -INF; _min = INF;
for(x = x1; x <= x2; x++) tree[x].query(, , c, );
printf("%d %d %d\n", _sum, _min, _max);
}
}
} return ;
}

代码君

UVa 11992 (线段树 区间修改) Fast Matrix Operations的更多相关文章

  1. Codeforces Round #442 (Div. 2) E Danil and a Part-time Job (dfs序加上一个线段树区间修改查询)

    题意: 给出一个具有N个点的树,现在给出两种操作: 1.get x,表示询问以x作为根的子树中,1的个数. 2.pow x,表示将以x作为根的子树全部翻转(0变1,1变0). 思路:dfs序加上一个线 ...

  2. 题解报告:hdu 1698 Just a Hook(线段树区间修改+lazy懒标记的运用)

    Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for m ...

  3. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  4. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  5. HDU 4027 Can you answer these queries? (线段树区间修改查询)

    描述 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to u ...

  6. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  7. UVA 11992 ——线段树(区间修改)

    解题思路: 将矩阵每一行建立一棵线段树,进而变成一维问题求解.注意数组要开 4*N 代码如下: #include <iostream> #include <cstdio> #i ...

  8. hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  9. hiho_1078_线段树区间修改

    题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...

随机推荐

  1. WPF解析PPT为图片

    偶遇需要解析 PPT为单张图片 其中,对于包含动画的PPT页,分别对动画最后效果进行截取,即每个连续动画截取 (动画N个)N+1(原图)张 http://git.oschina.net/jiailiu ...

  2. C#WinForm中在dataGridView中添加中文表头

    第一步: 注意事项:(1)如果使用数据库,那么第三步的名称可以是任意的,但是不能和数据库中的列名一样,否则会报错:    (2)第四步的页眉文本就是你想用的中文列名,自己定: (3)第六步尤其重要,不 ...

  3. 【Leetcode】 - Divide Two Integers 位运算实现整数除法

    实现两个整数的除法,不许用乘法.除法和求模.题目被贴上了BinarySearch,但我没理解为什么会和BinarySearch有关系.我想的方法也和BS一点关系都没有. 很早以前我就猜想,整数的乘法是 ...

  4. Azure VM 远程无法登陆问题(No Remote Desktop License)

    解决方法: 打开 Wins+R=> mstsc /v: yourVMIPadress /admin mstsc /v:xx.xx.xx.xx:54738 /admin

  5. cookieContainer应用

    PublicSharedFunctionGetCookiesSetByPage(ByVal strUrl AsString,ByVal cookieToProvide AsString)AsIEnum ...

  6. C++中的mutable关键字

    mutalbe的中文意思是“可变的,易变的”,跟constant(既C++中的const)是反义词. 在C++中,mutable也是为了突破const的限制而设置的.被mutable修饰的变量,将永远 ...

  7. KASS分布式文件系统(Kass File System)

    KASS分布式文件系统(Kass File System),简称KFS,是开始公司自主研发的分布式文件存储服务平台.KFS系统架构及功能服务类似Hadoop/GFS/DFS,它通过HTTP-WEB为上 ...

  8. POJ 1701

    /* Every tenant went up N floors would make the dissatisfied degree rise N * a + 0.5 * N * (N - 1) d ...

  9. D&F学数据结构系列——红黑树

    红黑树 定义:一棵二叉查找树如果满足下面的红黑性质,则为一棵红黑树: 1)每个结点不是红的就是黑的 2)根结点是黑的 3)每个叶结点是黑的 4)如果一个结点是红的,它的两个儿子都是黑的(即不可能有两个 ...

  10. CSS 的命名和书写

    CSS书写顺序 1.位置属性(position, top, right, z-index, display, float等) 2.大小(width, height, padding, margin) ...