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 ...
随机推荐
- C++ GUI Qt4编程(07)-3.1menu
1. C++ GUI Qt4编程第三章,添加menu菜单. 2. mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include < ...
- python基础学习-思维导图总结
- vue中遇到的坑 --- 变化检测问题(数组相关)
最近在项目中遇到了一个问题,不知道为什么,所以最后通过动手做demo实践.查文档的方式解决了,这里做一个总结. 例1 <!DOCTYPE html> <html lang=" ...
- 记录: Win10+Ubuntu18.04双系统安装
在重装windows系统的时候顺便将ubuntu也重装了. window 10 安装 制作USB启动盘 到"微软中国下载中心"(http://www.microsoft.com/z ...
- 通过宏定义将__declspec(dllexport)与__declspec(dllimport)的转化,实现库代码和使用代码使用同一份头文件
我们知道,在VC编程中,如果要编译成动态链接库,需要将函数.变量.类等导出,这时使用__declspec(dllexport).使用动态链接库时,需要在声明的时候有使用__declspec(dllim ...
- Python之装饰器、迭代器和生成器
在学习python的时候,三大“名器”对没有其他语言编程经验的人来说,应该算是一个小难点,本次博客就博主自己对装饰器.迭代器和生成器理解进行解释. 为什么要使用装饰器 什么是装饰器?“装饰”从字面意思 ...
- 深入理解JavaScript系列(15):函数(Functions)
介绍 本章节我们要着重介绍的是一个非常常见的ECMAScript对象——函数(function),我们将详细讲解一下各种类型的函数是如何影响上下文的变量对象以及每个函数的作用域链都包含什么,以及回答诸 ...
- FontSize sp 和 dp 的区别
dp不会随着“设置->显示->字体大小”的改变而改变,sp会. sp会随着configeration的配置来scale, dp不会. 所以,什么时候用sp, 什么时候用dp需要斟酌.
- 转载:解决IE下a标签会触发window.onbeforeunload的问题
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- javascript 权威指南
1.对象 1.1.序列话对象 JSON.stringify() 和 JSON.parse() 用来序列化和还原 javascript 对象. var o = {x:1, y:{z:[false,nul ...