UVa 11992 (线段树 区间修改) Fast Matrix Operations
比较综合的一道题目。
二维的线段树,支持区间的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的更多相关文章
- 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序加上一个线 ...
- 题解报告: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 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- 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 ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- UVA 11992 ——线段树(区间修改)
解题思路: 将矩阵每一行建立一棵线段树,进而变成一维问题求解.注意数组要开 4*N 代码如下: #include <iostream> #include <cstdio> #i ...
- hdu 3577 Fast Arrangement(线段树区间修改,求区间最小值)
Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...
- hiho_1078_线段树区间修改
题目 给定一组数,要求进行若干次操作,这些操作可以分为两种类型: (1) CMD 1 beg end value 将数组中下标在[beg, end] 区间内数字都变为value (2) CMD 2 b ...
随机推荐
- 2432: [Noi2011]兔农 - BZOJ
Description 农夫栋栋近年收入不景气,正在他发愁如何能多赚点钱时,他听到隔壁的小朋友在讨论兔子繁殖的问题. 问题是这样的:第一个月初有一对刚出生的小兔子,经过两个月长大后,这对兔子从第三个月 ...
- 1452: [JSOI2009]Count - BZOJ
Description Input Output Sample Input Sample Output 1 2HINT 一开始还想什么离线做,其实不用,空间足够,我们直接开100个二维树状数组,然后就 ...
- MyEclipse2015破解版_MyEclipse 2015 stable 2.0 稳定版 破解日志
前言:在MyEclipse 2015 Stable 1.0下载安装破解日志(http://www.cnblogs.com/wql025/p/5161979.html)一文中,笔者主要讲述了该版本的破解 ...
- "Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b7
1.错误背景 系统安装了.net framework4.0.4.5,项目先使用VS2013(4.5)开发,后来又重新用VS2010开发(4.0),运行时出现这个错误 2.错误原因 In .Net 4. ...
- java Socket用法详解(转)
在客户/服务器通信模式中, 客户端需要主动创建与服务器连接的 Socket(套接字), 服务器端收到了客户端的连接请求, 也会创建与客户连接的 Socket. Socket可看做是通信连接两端的收发器 ...
- Unity3d开发wp8问题汇总
原地址:http://blog.csdn.net/sunshine_1984/article/details/12849117 .wp8应用点击返回键没有响应将Unity3d导出wp8工程后,编译工程 ...
- WinForm 资源文件的使用
1. 创建资源文件: 2.双击资源文件,打开如下图:添加一个字符串: 名称为cnnstr 值为-- 3.添加文本文件和图像 4. 调用代码 MessageBox.Show(Resource1.cnns ...
- What is the difference between supervised learning and unsupervised learning?
Machine Learning is a class of algorithms which is data-driven, i.e. unlike "normal" algor ...
- VS2010 MFC DataGrid绑定实例
VS2010环境下MFC使用DataGrid绑定数据源 参考:http://blog.csdn.net/fddqfddq/article/details/7874706 详细介绍如何在MFC中使用Da ...
- 转:samba 启动和重新启动 以及在虚拟系统和实际系统怎么实现软件交换
转自:http://blog.csdn.net/zwhfyy/article/details/1605151 启动 smb start 重新启动 root 用户登陆 CHQ_WEB:/etc/init ...