hdu-1754 I Hate It---线段树模板题
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1754
题目大意:
求区间最大值+单点修改
解题思路:
直接套用模板即可
#include<bits/stdc++.h>
#define MID(l, r) (l + (r - l) / 2)
#define lson(o) (o * 2)
#define rson(o) (o * 2 + 1)
using namespace std;
typedef long long ll;
const int INF = 1e9 +;
const int maxn = 1e6 + ;
int a[maxn];
struct node
{
int l, r, mmax, mmin, sum;
}tree[maxn];
void build(int o, int l, int r)
{
tree[o].l = l, tree[o].r = r;
if(l == r)
{
tree[o].mmax = tree[o].mmin = tree[o].sum = a[l];
return;
}
int m = MID(l, r);
int lc = lson(o), rc = rson(o);
build(lc, l, m);
build(rc, m + , r);
tree[o].mmax = max(tree[lc].mmax, tree[rc].mmax);
tree[o].mmin = min(tree[lc].mmin, tree[rc].mmin);
tree[o].sum = tree[lc].sum + tree[rc].sum;
}
int ql, qr;//查询区间[ql, qr]中的max,min,sum
int ans_max, ans_min, ans_sum;
void query_init()//查询前,将全局变量初始化
{
ans_max = -INF;
ans_min = INF;
ans_sum = ;
}
void query(int o)
{
if(ql <= tree[o].l && qr >= tree[o].r)//[L, R]包含在[ql, qr]区间内,直接用该节点的信息,达到线段树查询快的操作
{
ans_max = max(ans_max, tree[o].mmax);
ans_min = min(ans_min, tree[o].mmin);
ans_sum += tree[o].sum;
return;
}
int m = MID(tree[o].l, tree[o].r);
if(ql <= m)query(lson(o));
if(qr > m)query(rson(o));
}
//单点更新,a[p] = v;
int p, v;
void update(int o)
{
if(tree[o].l == tree[o].r)
{
tree[o].mmax = v;
tree[o].mmin = v;
tree[o].sum = v;
return;
}
int m = MID(tree[o].l, tree[o].r);
int lc = lson(o), rc = rson(o);
if(p <= m)update(lc);
else update(rc);
tree[o].mmax = max(tree[lc].mmax, tree[rc].mmax);
tree[o].mmin = min(tree[lc].mmin, tree[rc].mmin);
tree[o].sum = tree[lc].sum + tree[rc].sum;
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
{
for(int i = ; i <= n; i++)scanf("%d", &a[i]);
build(, , n);
char s[];
int x, y;
while(m--)
{
scanf("%s%d%d", s, &x, &y);
if(s[] == 'Q')
{
ql = x, qr = y;
query_init();
query();
printf("%d\n", ans_max);
}
else if(s[] == 'U')
{
p = x, v = y;
update();
}
}
}
return ;
}
hdu-1754 I Hate It---线段树模板题的更多相关文章
- HDU 1754 I Hate It(线段树模板题)
题目链接: 传送门 I Hate It Time Limit: 3000MS Memory Limit: 32768 K Description 很多学校流行一种比较的习惯.老师们很喜欢询问, ...
- hdu 1754 I Hate It 线段树基础题
Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求, ...
- hdu 1754 I Hate It(线段树水题)
>>点击进入原题测试<< 思路:线段树水题,可以手敲 #include<string> #include<iostream> #include<a ...
- hdu 1754 I Hate It 线段树 点改动
// hdu 1754 I Hate It 线段树 点改动 // // 不多说,裸的点改动 // // 继续练 #include <algorithm> #include <bits ...
- HDU 1698 Just a Hook (线段树模板题-区间求和)
Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- [AHOI 2009] 维护序列(线段树模板题)
1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...
- hdu1823(二维线段树模板题)
hdu1823 题意 单点更新,求二维区间最值. 分析 二维线段树模板题. 二维线段树实际上就是树套树,即每个结点都要再建一颗线段树,维护对应的信息. 一般一维线段树是切割某一可变区间直到满足所要查询 ...
- [POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]
可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include &l ...
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- UESTC - 1057 秋实大哥与花 线段树模板题
http://acm.uestc.edu.cn/#/problem/show/1057 题意:给你n个数,q次操作,每次在l,r上加上x并输出此区间的sum 题解:线段树模板, #define _CR ...
随机推荐
- 安装软件或运行软件时提示缺少api-ms-win-crt-runtime库解决方法
最近碰到一个问题,在我软件安装或运行时会提示缺少api-ms-win-crt-runtime-|1-1-0.dll 当然第一个想到的是运行库没有装,但是很清楚的是我的电脑是装过vc_redist_20 ...
- Android应用中添加Log4j的示例
[2016-06-30]最新的log4j已经集成在DR_support_lib库中 具体请看: https://coding.net/u/wrcold520/p/DR_support_lib/git/ ...
- jsoup: Java HTML Parser
jsoup Java HTML Parser jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于j ...
- 配intelliJ IDEA 过程
1.安装svn 选项全选择,命令行执行要选择上2.安装java jdk 配jdk环境变量3.安装intelliJ IDEA 地址:http://idea.imsxm.com4.注册intelliJ I ...
- 第三章:ionic环境搭建之windows篇
下面是在windows操作系统上面安装ionic的步骤,已经在Windows 10/ 7/ XP下面通过验证. 安装JDK 1.1 下载(http://www.oracle.com/technetwo ...
- nyoj 269——VF——————【dp】
VF 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 Vasya is the beginning mathematician. He decided to make ...
- 【Shell】shell 判断文件夹或文件是否存在
1.文件夹不存在则创建,文件夹是directory if [ ! -d "/data/" ];then mkdir /data else echo "文件夹已经存在&qu ...
- [转]MVC+JQuery validate实现用户输入验证
本文转自:http://www.cnblogs.com/ahui/archive/2010/10/08/1845677.html MVC服务器端: 1.在controller中验证用户输入,如果验证失 ...
- 打包.NET Core的程序到一个单独的可执行文件
博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:打包.NET Core的程序到一个单独的可执行文件.
- Vue2.0以后,有哪些变化
最近移动端项目版本升级,Vue由之前的1.0升级到2.3,那么,Vue2.0之后,有哪些细节的变化呢,现在总结如下: 1.在每个组件模板,不再支持片段代码 组件中模板: 之前: <templat ...