比较综合的一道题目。

二维的线段树,支持区间的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. Graduate Summer School: Deep Learning, Feature Learning

    http://www.ipam.ucla.edu/programs/summer-schools/graduate-summer-school-deep-learning-feature-learni ...

  2. 【WCF--初入江湖】04 WCF通信模式

    04 WCF通信模式 WCF的通信模式有三种 [1]请求响应模式: 只能是客户端调用服务器; 客户端请求并等待服务器的响应后才继续执行后续操作(异步调用除外) [2]单工模式: 只能是客户端调用服务器 ...

  3. wordpress数据库优化wp_posts表 OPTIMIZE

    wordpress数据库优化wp_posts表 对 MySQL 数据记录进行插入.更新或删除时,会占有不同大小的空间,记录就会变成碎片,且留下空闲的空间.就像具有碎片的磁盘,会降低性能,需要整理,因此 ...

  4. 如何在winform DataGridView控件的DataGridViewButtonColumn按钮列中禁用按钮

    原文:http://msdn.microsoft.com/en-us/library/ms171619(v=vs.85).ASPX public class DataGridViewDisableBu ...

  5. 【redis】06Redis的高级应用之事务处理、持久化操作、pub_sub、虚拟内存

    上节课详细讲解了redis数据库的常用命令,以及redis数据库高级应用当中的, 安全性,跟咱们的主从复制, 这节课呢,咱们继续来讲咱们的高级应用, 首先来看一下咱们的事务处理, 事务处理 我前面说过 ...

  6. Relevance Between Variable Declaration and Definition in C++

    A declaration makes a name known to a programm. A definition creates the assocatied entity. A variab ...

  7. IT 基础设施

    http://www.cnblogs.com/wintersun/p/4355267.html

  8. POJ3080Blue Jeans

    http://poj.org/problem?id=3080 题意 : 给你几个DNA序列,让你找他们的共同的最长的子串,若是子串长度小于3,就输出no significant commonaliti ...

  9. Linux查看随机启动服务

    Liunx操作系统跟Windos XP一样,有一批系统服务随机而启动:略懂电脑的Windows XP用户会禁止那些不必要的服务,以提高开机速度:如今安装了Ubuntu操作系统,咱们也有必要了解Ubun ...

  10. DVB系统几种传输方式

    卫星 (DVB-S 及 DVB-S2)有线 (DVB-C)地面无线 (DVB-T)手持地面无线 (DVB-H)