hdu(1754)——I hate it(更新节点,区间最值)

当C为‘Q‘的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。

当C为‘U‘的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。

题目大意就是这样,然后这道题呢,就是一道线段树的区间查询与端点更新的问题。

与区间和有所不同的是:这道题我们是维护线段树的最大值,所以在建树的时候,pushup时,我们要对父节点维护的是两个子节点中的最大值。

然后最后再注意一下更新的时候别忘记了要pushup来更新父节点。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 222222
int a[maxn];
int ans=0;
struct node{
    int l,r;
    int val;
}tree[maxn*4];
void pushup(int v){
    int temp=v*2;
    tree[v].val=max(tree[temp].val,tree[temp+1].val);
}
void build(int l,int r,int v){
    tree[v].l=l;
    tree[v].r=r;
    tree[v].val=0;
    if(l==r){
        tree[v].val=a[l];
        return;
    }
    int mid=(l+r)/2;
    int temp=v*2;
    build(l,mid,temp);
    build(mid+1,r,temp+1);
    pushup(v);
}
void query(int l,int r,int v){
    if(tree[v].l==l&&tree[v].r==r){
        ans=max(ans,tree[v].val);
        return;
    }
    int mid=(tree[v].l+tree[v].r)>>1;
    int temp=v<<1;
    if(r<=mid) query(l,r,temp);
    else if(mid<l) query(l,r,temp+1);
    else{
        query(l,mid,temp);
        query(mid+1,r,temp+1);
    }
}
void update(int v,int pos,int score){
    if(tree[v].l==pos&&tree[v].r==pos){
        tree[v].val=score;
        return;
    }
    int temp=v<<1;
    int mid=(tree[v].l+tree[v].r)>>1;
    if(pos<=mid) update(temp,pos,score);
    else if(pos>mid)    update(temp+1,pos,score);
    pushup(v);
}
int main(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        build(1,n,1);
        char ss[6];
        while(m--){
            int aa,bb;
            scanf("%s",ss);
            if(ss[0]=='Q'){
                ans=0;
                scanf("%d%d",&aa,&bb);
                query(aa,bb,0);
                printf("%d\n",ans);
            }
            else if(ss[0]=='U'){
                scanf("%d%d",&aa,&bb);
                update(0,aa,bb);
            }
        }
    }
}
时间: 2024-08-27 05:50:16

hdu(1754)——I hate it(更新节点,区间最值)的相关文章

hdu 1754 线段树 单点更新 动态区间最大值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 52417    Accepted Submission(s): 20598 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的

HDU 1754 I Hate It(线段树之单点更新,区间最值)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 70863    Accepted Submission(s): 27424 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感.不管你喜不喜欢,现在需要你做的是,就是按照老师的

HDU 1754-I Hate It(线段树:单点更新,区间最值)

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 39163    Accepted Submission(s): 15507 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师

poj 2763 树链剖分(单点更新,区间求值)

http://poj.org/problem?id=2763 Description After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional ro

Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E. Excellent Engineers-单点更新、区间最值-线段树 G. Growling Gears I. Interesting Integers-类似斐波那契数列-递推思维题

先写这几道题,比赛的时候有事就只签了个到. E. Excellent Engineers 传送门: 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到名单中. 因为是3个变量,所以按其中一个变量进行sort排序,然后,剩下的两个变量,一个当位置pos,一个当值val,通过线段树的单点更新和区间最值操作,就可以把名单确定. 代码: 1 //E-线段树 2 #include<iostream> 3 #include<cstdio> 4 #incl

hdu(1166)——敌兵布阵(更新节点,区间求和)

这是一道最简单的线段树的更新节点与区间查询. 当然practic makes perfect~ 现在我已经能够比较熟练地打出来了. 但是还有一些细节要注意就是对于父节点的更新. #include<stdio.h> #include<string.h> #include<algorithm> #include<iostream> using namespace std; #define maxn 55555 char a[55]; struct node{ i

HDU - 1754 I Hate It (线段树区间求最值)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 题意:线段树的单点更新和区间求最值 模板题,,,???,, 1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 5 typedef long long LL; 6 const int N=200010; 7 8 LL ans; 9 LL max(LL a,LL b){ 10 if(a>b) r

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

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

【HDU】1754 I hate it ——线段树 单点更新 区间最值

I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 37448    Accepted Submission(s): 14816 Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少.这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要

HDU 4302 线段树单点更新,维护区间最大最小值

http://acm.hdu.edu.cn/showproblem.php?pid=4302 Problem Description Holedox is a small animal which can be considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere in the p