Hdu1754-线段树-单点更新

简介: #include<iostream>#include<cstdio>#include<algorithm>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=200005;int MAX[maxn&l
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int maxn=200005;
int MAX[maxn<<2];
void pushup(int rt)
{
    MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&MAX[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p ,int x, int l,int r,int rt)
{
    if(l==r)
    {
        MAX[rt]=x;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m)
        update(p,x,lson);
    else
        update(p,x,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
        return MAX[rt];
    int m=(r+l)>>1;
    int ans=0;
    if(L<=m)
        ans=max(query(L,R,lson),ans);
    if(R>m)
        ans=max(query(L,R,rson),ans);
    return ans;
}
int main()
{
    int n,m;
    while( scanf("%d %d",&n,&m)!=EOF)
    {
        build(1,n,1);
        char s[2];
        while(m--)
        {
            int a,b;
            scanf("%s%d%d",s,&a,&b);
            if(s[0]=='Q')
                printf("%d\n",query(a,b,1,n,1));
            else if(s[0]=='U')
                update(a,b,1,n,1);
        }
    }
    return 0;
}

  

目录
相关文章
|
6月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
20 0
|
6月前
|
机器学习/深度学习 存储 C++
[蓝桥杯] 树状数组与线段树问题(C/C++)
[蓝桥杯] 树状数组与线段树问题(C/C++)
53 0
|
7月前
|
算法
数星星(树状数组模板题)
数星星(树状数组模板题)
27 0
|
人工智能 索引
树状数组总结
能够解决数据压缩里的累积频率的计算问题,现多用于高效计算数列的前缀和、区间和。
105 0
树状数组总结
|
人机交互
POJ-2524,Ubiquitous Religions(并查集模板题)
POJ-2524,Ubiquitous Religions(并查集模板题)
|
人工智能 BI 存储
|
人工智能 网络架构
|
人工智能
POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Description BackgroundRaymond Babbitt drives his brother Charlie mad.
1236 0
|
Java
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 很多学校流行一种比较的习惯。
1079 0
线段树-poj-2823
Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k
1057 0