题目链接:这道题是线段树,树状数组最基础的问题

两种分类方式:按照更新对象和查询对象

单点更新,区间查询;

区间更新,单点查询;

按照整体维护的对象:

维护前缀和;

维护区间最值.

线段树模板代码

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define re(i,n) for(int i=0;i<n;i++)
typedef long long ll;
 * 1e5 + ;
#define lson(x) x<<1,f,mid
#define rson(x) x<<1|1,mid+1,t
int n, m;
];
int x, y;
int ans;
void build(int r, int f, int t){
    if (f == t){
        scanf("%d", &tr[r]);
        return;
    }
    ;
    build(lson(r)),build(rson(r));
    tr[r] = max(tr[r << ], tr[r <<  | ]);
}
void query(int r, int f, int t){
    if (f >= x&&t <= y){
        ans = max(ans, tr[r]);
        return;
    }
    ;
    if (x <= mid)query(lson(r));
    if (y > mid)query(rson(r));
}
void update(int r, int f, int t){
    if (f == t){
        tr[r] = y;
        return;
    }
    ;
    if (x<= mid)update(lson(r));
    else update(rson(r));
    tr[r] = max(tr[r << ], tr[r <<  | ]);
}
int main(){
    //freopen("in.txt", "r", stdin);
    while (cin >> n >> m){
        build(, , n);
        while (m--){
            ];  scanf("%s%d%d", op,&x,&y);
            ] == 'Q'){
                if (x > y)swap(x, y);
                ans = -;
                query(, , n);
                printf("%d\n", ans);
            }
            else{
                update(, , n);
            }
        }
    }
    ;
}

树状数组区间最值模板代码

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
*1e5+;
typedef long long ll;
#define re(i,n) for(int i=0;i<n;i++)
int a[maxn], c[maxn];
/*
a里面存放本值,c里面维护最值
*/
int n, q;
int lowbit(int x){
    return x&-x;
}
void redo(int i){
    c[i] = i;
    ; j < lowbit(i); j <<= )
    if (a[c[i - j]]>a[c[i]])c[i] = c[i - j];
}
void init(){
    ; i <= n; i++){
        redo(i);
    }
}
void update(int x, int y){
    bool big = y > a[x];
    a[x] = y;
    int i = x;
    if (big){
        /*
        这个地方如果错写成a[c[i]]<y就会导致无法传递上去,因为一开始时就是a[c[i]]==y.
        */
        while (i<=n&&a[c[i]] <= y)c[i] = x, i += lowbit(i);
    }
    else{
        while (i<=n&&c[i] == x)redo(i), i += lowbit(i);
    }
}
int query(int l, int r){
    int ans = a[r];
    while (l<=r){
        while (r - lowbit(r) >= l){
            ans = max(a[c[r]], ans);
            r -= lowbit(r);
        }
        ans = max(a[r], ans);//根节点
        r--;//向下走一步
    }
    return ans;
}
int main(){
    freopen("in.txt", "r", stdin);
    ){
        re(i, n)scanf(]);
        init();
        ;
        while (q--){
            ]; int x, y;
            qi++;
            scanf("%s%d%d", &op, &x, &y);
            ] == 'U'){
                update(x, y);
            }
            else{
                int ans = query(x, y);
                printf("%d\n", ans);
            }
        }
    }
    ;
}

复杂度是O(lgn*lgn).这道题数据好像有点弱,因为错误的代码也能过这道题.

zwk线段树模板

zwk线段树巧妙之处在于开区间写法,对于长度为N的线段,初始化时,共N+2片叶子:1和N+2空着,2~N+1才是本体,是实实在在的数据.张昆玮说了:如果有1023个数据,那就要有2048片叶子.空间要开到(N+2)*4才行.虽然费了点空间,但编程简洁了.

根节点(1号结点)和每层的第一个节点和最后一个节点是哨兵节点,永远都不会访问到它们.它们的存在只是方便编程.老子曰:将欲取之,必先予之.一言以蔽之,就是两端边界处的节点都是哨兵节点,这是开区间方便编程导致的.

那么闭区间写法行不行呢?对于长度为N的线段,1~N全部用上,查询时闭区间可以转换成开区间.如果有1023个数,那就只需要开辟1024片叶子的树状数组.开区间=闭区间-两个端点.若无论如何都更新两个端点,那就会导致更新的东西有点靠下,这对于结果并没什么影响.需要注意l==r的情形.

zwk开区间写法

 #include<iostream>
 #include<stdio.h>
 #include<algorithm>
 using namespace std;
  * 1e5 + ;
 typedef long long ll;
 #define re(i,n) for(int i=0;i<n;i++)
 ];
 int n, q, sz;
 void update(int x, int v){
     x += sz;
     a[x] = v;
     ){
         x >>= ;
         a[x] = max(a[x << ], a[x <<  | ]);
     }
 }
 int query(int l, int r){
     l += sz - , r += sz + ;
     ;
     ){//当两人不是兄弟时
         ){
             ans = max(ans, a[l ^ ]);
         }
         ){
             ans = max(ans, a[r ^ ]);
         }
         l >>= , r >>= ;
     }
     return ans;
 }
 int main(){
     //freopen("in.txt", "r", stdin);
     ){
         sz = ; )sz <<= ;
         re(i, n){
             scanf(]);
         }
         ); i++)a[i + sz+] = ;
         ; i>; i--){
             a[i] = max(a[i << ], a[i <<  | ]);
         }
         while (q--){
             ]; int x, y;
             scanf("%s%d%d", op, &x, &y);
             ] == 'U'){
                 update(x, y);
             }
             else{
                 int ans = query(x, y);
                 printf("%d\n", ans);
             }
         }
     }
     ;
 }

zwk闭区间写法:下面的代码有bug.能过题.

 #include<iostream>
 #include<stdio.h>
 #include<algorithm>
 using namespace std;
  * 1e5 + ;
 typedef long long ll;
 #define re(i,n) for(int i=0;i<n;i++)
 ];
 int n, q, sz;
 void update(int x, int v){
     x += sz-;
     a[x] = v;
     ){
         x >>= ;
         a[x] = max(a[x << ], a[x <<  | ]);
     }
 }
 int query(int l, int r){
     l += sz - , r += sz - ;
     int ans =max(a[l],a[r]); //查询时先把两个端点给处理掉,就相当于开区间了.
     /*
     这个地方的不同决定了两种写法,我觉得没有必要开区间.闭区间预处理一下就很好.
     这个地方如果l==r,就会产生死循环.但是我这么写,这道题却过了.*/
     ){//当两人不是兄弟时
         ){
             ans = max(ans, a[l ^ ]);
         }
         ){
             ans = max(ans, a[r ^ ]);
         }
         l >>= , r >>= ;
     }
     return ans;
 }
 void init(){
     sz = ; ;
     re(i, n){
         scanf("%d", &a[i + sz]);//在1~N之间存放数据
     }
     ); i++)a[i + sz ] = ;
     ; i>; i--){
         a[i] = max(a[i << ], a[i <<  | ]);
     }
 }
 int main(){
     freopen("in.txt", "r", stdin);
     ){
         init();
         while (q--){
             ]; int x, y;
             scanf("%s%d%d", op, &x, &y);
             ] == 'U'){
                 update(x, y);
             }
             else{
                 int ans = query(x, y);
                 printf("%d\n", ans);
             }
         }
     }
     ;
 }

hdu1754 I hate it线段树模板 区间最值查询的更多相关文章

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

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

  2. xdoj-1324 (区间离散化-线段树求区间最值)

    思想 : 1 优化:题意是覆盖点,将区间看成 (l,r)转化为( l-1,r) 覆盖区间 2 核心:dp[i]  覆盖从1到i区间的最小花费 dp[a[i].r]=min (dp[k])+a[i]s; ...

  3. HDU-1754 I Hate It(线段树,区间最大值)

    很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.  这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问.当然,老师 ...

  4. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  5. 滑动窗口(poj,线段树维护区间最值)

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

  6. CodeForces 91B Queue (线段树,区间最值)

    http://codeforces.com/problemset/problem/91/B B. Queue time limit per test: 2 seconds memory limit p ...

  7. POJ 2482 Stars in Your Window (线段树+扫描线+区间最值,思路太妙了)

    该题和 黑书 P102 采矿 类似 参考链接:http://blog.csdn.net/shiqi_614/article/details/7819232http://blog.csdn.net/ts ...

  8. Balanced Lineup:线段树:区间最值 / RMQ

    不要被线段树这个名字和其长长的代码吓到. D - Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ...

  9. HDU6447 YJJ's Salesman-2018CCPC网络赛-线段树求区间最值+离散化+dp

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面.  1e5个点,问 ...

随机推荐

  1. web性能优化系列之网站瓶颈识别

    最近,公司网站的的访问量日益增大,PV马上过百万了.前期网站一到访问高峰,马上会变得很卡甚至打不开,后来做了很多优化后现在访问比较顺畅了.经过此事明白了一个道理,在遇到这类问题要做到两点,第一要相信自 ...

  2. sshpass批量执行操作

    while read line do ./sshpass -p 密码 ssh root@$line “ls” done<./backup while read line ;do /usr/bin ...

  3. css3中变形与动画(一)

    css3制作动画的几个属性:变形(transform),过渡(transition)和动画(animation). 首先介绍transform变形. transform英文意思:改变,变形. css3 ...

  4. 一起来啃书——PHP看书

    形式所迫,不得不开展android的学习,PHP这边也开始了啃书的日子.两部500+的书,45天够不,有点忙有点忙... 早上的胃胀,简直是一记闷棍,长点儿记性吧........ 1.PHP+MYSQ ...

  5. .Net程序员Python之道---Python基础

    最近对动态语言比较好奇.所以选择了学习Python这门语言.话不多说开始学习笔记. 一. Python 基础: 1. print对Python进行数据输出, #号后面是队友的输出结果, 通过encod ...

  6. MMORPG大型游戏设计与开发(客户端架构 part2 of vgui)

    这一节我将讲解vgui的基础系统部分,也是该库提供给外部使用的一些重要接口.作为UI部分比较重要的部分,该节有着至关重要的部分,如果没有看到上一节内容,请留意下面的连接.我们现在可以猜想一下在客户端U ...

  7. 网络之Ip地址

    0.0.0.0---255.255.255.255 Ip地址分类(D.E)不对外开放 网络类别 最大网络数 IP地址范围(,唯一的,花钱的) 最大主机数 私有IP地址范围 (做内网ip,不可直接访问公 ...

  8. Android驱动入门-LED--HAL硬件抽象层程序设计①

    硬件平台: FriendlyARM Tiny4412 Cortex-A9 操作系统: UBUNTU 14.04 LTS 时间:2016-09-21  16:15:26 设计HAL硬件抽象层程序,则需要 ...

  9. java之多线程之一/序列化和反序列化

    线程安全: 如何自己手动创建一个线程 答:继承Thread类或实现Runnable接口 public class Mythread extends Thread{ @Override public v ...

  10. cell与cell之间的间距问题,以及section跟随屏幕滑动而滑动问题

    苹果在cell与cell之间默认没有间距,这样有时候不能满足我们界面要求,所以我们就需要将cell设置为分组模式(也就是每组一行或者多行,分为n组),然后我们就可以在代理中根据自己的需求设计cell之 ...