比较综合的一道题目。

二维的线段树,支持区间的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解析PDF为图片

    偶遇需要解析PDF文件为单张图,此做, http://git.oschina.net/jiailiuyan/OfficeDecoder using System; using System.Colle ...

  2. Careercup - Facebook面试题 - 5671785349513216

    2014-05-02 01:05 题目链接 原题: bool anaStrStr (string needle, string haystack) { } Write a function that ...

  3. android开发之socket快传文件以及消息返回

    应用场景: 两台android机器:一台自建wifi热点,另一台搜索到,连接该wifi热点.之后再通过socket传递消息,文件等,当服务器端接收到消息之后会返回对应的应答消息: 注意点:接收到消息之 ...

  4. android开发 更新升级安装到一半自动闪退

    如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /**     * 安装APK文件     */    private void installApk( ...

  5. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  6. 剑指offer--面试题5

    到现在为止,看过的书+代码有一定量了,并且也参加了个比赛,给自己的总体感觉:编程需要的是灵活的头脑,书里的东西只是讲个规则.思想,其实际实现可以千差万别!   潜在的规则+灵活的思维 = 程序! 在做 ...

  7. 《剑指Offer》- 面试题3

    <剑指Offer——名企面试官精讲典型编程题>  面试题3:  二维数组元素从左到右.从上到下递增,输入一个二维数组和一个整数,  查找该整数. 自己的思路:有序条件下进行查找,当然最简单 ...

  8. hlsl 的tex函数

    texCUBE http://msdn.microsoft.com/en-us/library/windows/desktop/bb509687(v=vs.85).aspx

  9. 从String类看写C++ class需要注意的地方

    #include <iostream> #include <string.h> using namespace std; class String { char* m_data ...

  10. web配置详解

    1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Servl ...