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;
}

  

目录
相关文章
|
9月前
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
26 0
|
10月前
|
算法
数星星(树状数组模板题)
数星星(树状数组模板题)
30 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.
1243 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 很多学校流行一种比较的习惯。
1085 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
1064 0
hdu1394 线段树求最小逆序数
hdu 1394 http://acm.hdu.edu.cn/showproblem.php?pid=1394 用线段树求逆序数,例如要求x的逆序数只需要访问(x+1,n)段有多少个数,就是x的逆序数。还有就是求最小逆序数的时候有个巧妙的想法,当把x放入数组的后面,此时的逆序数应该为x没放入最后面之前的逆序总数加上(n-x)再减去(x-1);sum = sum+(n-x[i])-(x[
1114 0