【hdu】I Hate It(线段树,结点修改求区间最大值)

线段树的模板题,还是二分递归。

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long LL;
#define MAXD 10
#define maxn 200010
int n;
int tree[maxn << 2];
void BuildTree(int l,int r,int pos){   //递归建树
    if(l == r){
        scanf("%d",&tree[pos]);
        return ;
    }
    int m = (l + r) >> 1;
    BuildTree(l,m,(pos << 1));
    BuildTree(m + 1,r,(pos << 1)|1);
    tree[pos] = max(tree[pos << 1] , tree[(pos << 1) | 1]);
    return ;
}
void UpDate(int aim,int value,int l,int r,int pos){  //目标结点,当前区间长度,当前结点编号
    if(l == r){
        tree[pos] = value;
        return ;
    }
    int m = (l + r) >> 1;
    if(aim <= m) UpDate(aim,value,l,m,pos << 1);
    else
        UpDate(aim,value,m + 1, r,(pos << 1)|1);
    tree[pos] = max(tree[pos << 1] , tree[(pos << 1)|1]);
    return ;
}
int Query(int L,int R,int l,int r,int pos){
    if(L <= l && r <= R)
        return tree[pos];
    int m = (l + r) >> 1;
    int ans = -1;
    if(L <= m)
        ans = max(ans,Query(L,R,l,m,pos << 1));
    if(m + 1 <= R)
        ans = max(ans,Query(L,R,m + 1, r, (pos << 1)|1));
    return ans;
}
int main(){
    int m;
    while(scanf("%d%d",&n,&m) != EOF){
        BuildTree(1,n,1);
        char str[MAXD];
        for(int i = 0 ; i < m ; i++){
            scanf("%s",str);
            if(str[0] == 'Q'){
                int a,b;
                scanf("%d%d",&a,&b);
                int ans = Query(a,b,1,n,1);
                printf("%d\n",ans);
            }
            else if(str[0] == 'U'){
                int a,b;
                scanf("%d%d",&a,&b);
                UpDate(a,b,1,n,1);
            }
        }
    }
    return 0;
}
时间: 2024-08-24 23:17:51

【hdu】I Hate It(线段树,结点修改求区间最大值)的相关文章

hdu-1754 I Hate It【线段树】(求区间最大值)

题目链接:https://vjudge.net/contest/182746#problem/A I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                    Total Submission(s): 96252    Accepted Submission(s): 36

hdoj 2795 Billboard 【线段树 单点更新 + 维护区间最大值】

Billboard Time Limit: 20000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15719    Accepted Submission(s): 6629 Problem Description At the entrance to the university, there is a huge rectangular billboard of

树状数组求区间最大值

------  一直用 线段树 求区间最大值,想换种思路,用树状数组试试,肯定是可以的. 首先要对 树状数组的每个 i 所管理的区间有一定的理解.详见上篇博客: 树状数组(BIT)

HDU 1166 敌兵布阵 【线段树-点修改--计算区间和】

敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 59891    Accepted Submission(s): 25331 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营 地,Derek和Tidy的任务

HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11551    Accepted Submission(s): 4906 Problem Description There are several ancient Greek texts that contain descriptions of the fabled

hdu1394线段树点修改,区间求和

Problem Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of

线段树 单点更新查询 区间最大值 hdu 2795 Billboard

题意: 有一块h*w(1<=h,w<=10^9)的公告牌,需要在上面放n(n<=200000)个1*w[i]的公告,每个公告优先选可以放置的地方中最上的那行,同一行选最左的地方,依次输出每个公告放置在哪行,如果不能放置,输出-1. 可以把公告牌看作长为h的线段,构建一个线段树,每个节点存储区间的最大值,初始最大值为公告牌的宽度w.如果某区间的最大值大于当前公告的宽度,就可以放在该区间,由于公告优先放在上面,所以选区间的时候也应该先判断左边的区间可不可以,当在某个点放上公告后,该点的最大值

HDU 2795 Billboard (线段树单点更新 &amp;&amp; 求区间最值位置)

题意 : 有一块 h * w 的公告板,现在往上面贴 n 张长恒为 1 宽为 wi 的公告,每次贴的地方都是尽量靠左靠上,问你每一张公告将被贴在1~h的哪一行?按照输入顺序给出. 分析 : 这道题说明了每一次贴都尽量选择靠上靠左的位置,那既然这样,我们以1~h建立线段树,给每一个叶子节点赋值初值 w 表示当前行最大能够容纳宽度为 w 的公告纸,那么对于某一输入 wi 只要在线段树的尽量靠左的区间找出能够容纳这张公告的位置(即叶子节点)然后减去 wi 即可,需要对query()函数进行一点改造,将

线段树模板1 - 求区间和

今天再次入门线段树 然而还是入门失败 push_up 和 push_down 不是太懂啊 线段树在有结合律性质的区间操作都可以用 然而我并不会 先开个坑吧 #include<bits/stdc++.h> #define MAXN 1000001 #define ll long long #define ls(o) (o<<1) #define rs(o) (o<<1|1) using namespace std; ll n,m,a[MAXN],ans[MAXN<&