比较综合的一道题目。

二维的线段树,支持区间的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. 【环境】Linux下连接无线网常用命令

    启用/重启/关闭 网络服务 /etc/init.d/networking start /etc/init.d/networking restart /etc/init.d/networking sto ...

  2. asp.net 中插入数据到access

    这样报语法错误: insert into Menu_wx(userid,menutype,MenuID,menuname,MenuTitle,Url,Key,OrderValue,State) val ...

  3. 【HDOJ】【3037】Saving Beans

    排列组合 啊……这题是要求c(n-1,0)+c(n,1)+c(n+1,2)+......+c(n+m-1,m) 这个玩意……其实就等于c(n+m,m) 好吧然后就是模P……Lucas大法好= = 我S ...

  4. 检测php网站是否已经被攻破的方法

    0x01 查看访问日志 看是否有文件上传操作(POST方法), IPREMOVED - - [01/Mar/2013:06:16:48 -0600] "POST/uploads/monthl ...

  5. 表中相同数据的sql语句

    1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断select * from peoplewhere peopleId in (select   peopleId from  ...

  6. qsort()与besearch()

    功 能: 使用快速排序例程进行排序 头文件:stdlib.h 用 法: void qsort(void *base,int nelem,int width,int (*fcmp)(const void ...

  7. hdu 1271 整数对

    看了别人的解题报告a了, 大致思路就是 A=a+b*10^k+c*10^(k+1) B=a+c*10^k (在A中取出一位数后) N=A+B=2*a+b*10^k+11*c*10^k 这样就好做了,再 ...

  8. linux设置和查看环境变量的方法

    1.    显示环境变量HOME $ echo $HOME /home/redbooks 2.    设置一个新的环境变量hello $ export HELLO="Hello!" ...

  9. 通过快捷键及cmd命令注销系统

    公司的外网内网是隔离的 外网的远程电脑屏幕一半卡那了,页面注销键正好在卡死的那一半屏幕上,用以下简单方法注销远程重新连接,问题解决了. 1.通过快捷键win+r打开“运行...” 2.输入CMD 回车 ...

  10. 李洪强漫谈iOS开发[C语言-038]-if else if语句

    李洪强漫谈iOS开发[C语言-038]-if else if语句