HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)

点我挑战题目

题意分析

从题目中可以看出是大数据的输入,和大量询问。基本操作有:

1.Q(i,j)代表求区间max(a[k]) k∈[i,j];

2.U(i,j)代表a[i] = j;

对于询问U,用单点替换的操作维护线段树。对于询问Q,那么除了叶子节点,其他的节点应该保存的是左子树和右子树的最大值,因此pushup函数应该是对最大值的一个维护,query的时候找出的应该是最大值,故改为ans = max(……) 即可。

代码总览

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 200005
using namespace std;
int a[nmax],add[nmax<<2],sum[nmax<<2];
void pushup(int rt)
{
sum[rt] = max(sum[rt<<1] ,sum[rt<<1|1]);
}
void pushdown(int rt, int ln, int rn)
{
if(add[rt]){
add[rt<<1]+=add[rt];
add[rt<<1|1]+=add[rt];
sum[rt<<1]+=add[rt]*ln;
sum[rt<<1|1]+=add[rt]*rn;
add[rt] = 0;
} }
void build(int l ,int r, int rt)
{
if(l == r){
sum[rt] = a[l];
return;
}
int m = (l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
pushup(rt);
}
void updatep(int L, int c, int l, int r, int rt)
{
if(l == r){
sum[rt] = c;
return;
}
int m = (l+r)>>1;
pushdown(rt,m-l+1,r-m);
if(L<=m) updatep(L,c,l,m,rt<<1);
else updatep(L,c,m+1,r,rt<<1|1);
pushup(rt);
}
void updatei(int L, int R, int c, int l, int r, int rt)
{
if( l>=L && r<= R){
sum[rt]+=c*(r-l+1);
add[rt]+=c;
return;
}
int m = (l+r)>>1;
pushdown(rt,m-l+1,r-m);
if(L<=m) updatei(L,R,c,l,m,rt<<1);
if(R>m) updatei(L,R,c,m+1,r,rt<<1);
pushup(rt);
}
int query(int L, int R, int l ,int r, int rt)
{
if(L<=l && r<=R){
return sum[rt];
}
int m = (l+r)>>1;
pushdown(rt,m-l+1,r-m);
int ANS =0;
if(L<=m) ANS = max( query(L,R,l,m,rt<<1),ANS);
if(R>m) ANS = max(query(L,R,m+1,r,rt<<1|1),ANS);
return ANS;
}
int main()
{
//freopen("in.txt","r",stdin);
int n,m;
while(scanf("%d%d",&n,&m)!= EOF){
for(int i =1;i<=n; ++i) scanf("%d",&a[i]);
build(1,n,1);
char com;
for(int i = 1;i<=m;++i){
scanf(" %c",&com);
if(com == 'Q'){
int L,R;
scanf("%d%d",&L,&R);
printf("%d\n",query(L,R,1,n,1));
}else{
int L,c;
scanf("%d%d",&L,&c);
updatep(L,c,1,n,1);
}
}
}
return 0;
}

HDOJ(HDU).1754 I Hate It (ST 单点替换 区间最大值)的更多相关文章

  1. HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和)

    HDOJ(HDU).1166 敌兵布阵 (ST 单点更新 区间求和) 点我挑战题目 题意分析 根据数据范围和询问次数的规模,应该不难看出是个数据结构题目,题目比较裸.题中包括以下命令: 1.Add(i ...

  2. hdu1754(线段树单点替换&区间最值模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:中文题诶- 思路:线段树单点替换&区间最大值查询模板 代码: #include & ...

  3. HDU 1754 I Hate It(线段树单点替换+区间最值)

    I Hate It [题目链接]I Hate It [题目类型]线段树单点替换+区间最值 &题意: 本题目包含多组测试,请处理到文件结束. 在每个测试的第一行,有两个正整数 N 和 M ( 0 ...

  4. hdu 1754 线段树(单点替换 区间最值)

    Sample Input5 61 2 3 4 5Q 1 5 //1-5结点的最大值U 3 6 //将点3的数值换成6Q 3 4Q 4 5U 2 9Q 1 5 Sample Output5659 # i ...

  5. HDU 1754 I Hate It 线段树 单点更新 区间最大值

    #include<iostream> #include<string> #include<algorithm> #include<cstdlib> #i ...

  6. hdu 1754 线段树 水题 单点更新 区间查询

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu 1754 I Hate It(单点更新,区段查最值)

    题意: N个成绩.M个操作. Q a b:查询第a个到第b个成绩中最高成绩 U a b:将第a个成绩改成b 思路: 看代码,, 代码: const int maxn = 200010; int max ...

  8. hdoj 1166 敌兵布阵【线段树求区间最大值+单点更新】

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  9. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

随机推荐

  1. 代码重复率检查工具jsinspect

    检查重复代码,去掉冗余代码. 安装: npm install -g jsinspect 用法:jsinspect [options] <paths ...> 检测复制粘贴和结构类似的Jav ...

  2. beauifulsoup模块的介绍

    01   爬虫基础知识介绍 相关库:1.requests,re  2.BeautifulSoup   3.hackhttp 使用requests发起get,post请求,获取状态码,内容: 使用re匹 ...

  3. idea前端页面不刷新----springboot

    修改这里就好了

  4. 第六阶段·数据库MySQL及NoSQL实践 第2章·Redis

    01-Redis简介 02-Redis基本安装启动 03-Redis的配置文件基本使用 04-Redis安全管理 05-Redis安全持久化-RDB持久化 06-Redis安全持久化-AOF持久化 0 ...

  5. C++11 type_traits 之is_same源码分析

    请看源码: template<typename _Tp, _Tp __v> struct integral_constant { static const _Tp value = __v; ...

  6. linux学习总结---mysql总结②

    函数: 字符串 日期时间 数学 子查询:嵌套查询 1. 做子查询: select sclass from studb where sname='..' 2.select * from studb wh ...

  7. 论文笔记:Attentional Correlation Filter Network for Adaptive Visual Tracking

    Attentional Correlation Filter Network for Adaptive Visual Tracking CVPR2017 摘要:本文提出一种新的带有注意机制的跟踪框架, ...

  8. Dictionary tabPage使用

    public override bool AccptChange() { //if (oldvalue == null || oldvalue.Count <= 0) //{ // return ...

  9. 浅谈蓝牙低功耗(BLE)的几种常见的应用场景及架构(转载)

    转载来至beautifulzzzz,网址http://www.cnblogs.com/zjutlitao/,推荐学习 蓝牙在短距离无线通信领域占据举足轻重的地位—— 从手机.平板.PC到车载设备, 到 ...

  10. Fox and Number Game

    Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can ...