zoukankan      html  css  js  c++  java
  • HDU1754 I Hate It(线段树基础题单节点更新区间查询)

    这题用树状数组写要简单很多,因为我学习线段树,找了这个题入门。。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define M 200005
    #define ls node<<1,l,m
    #define rs node<<1|1,m+1,r
    int n,m,tree[M*4];
    void buildtree(int node,int l,int r)
    {
        if(l==r) scanf("%d",&tree[node]);
        else
        {
            int m=(l+r)>>1;
            buildtree(ls);
            buildtree(rs);
            tree[node]=max(tree[node<<1],tree[node<<1|1]);
        }
    }
    int query(int node,int l,int r,int L,int R)
    {
        if(L<=l&&r<=R) return tree[node];
        int p=0,m=(l+r)>>1;
        if(m>=L) p=query(ls,L,R);
        if(m<R) p=max(p,query(rs,L,R));
        return p;
    }
    void update(int node,int l,int r,int ind,int add)
    {
        if(l==r) tree[node]=add;
        else
        {
            int m=(l+r)>>1;
            if(m>=ind) update(ls,ind,add);
            else update(rs,ind,add);
            tree[node]=max(tree[node<<1],tree[node<<1|1]);
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        while(~scanf("%d%d",&n,&m))
        {
            buildtree(1,1,n);
            char s[2];
            int a,b;
            while(m--)
            {
                scanf("%s%d%d",s,&a,&b);
                if(s[0]=='Q') printf("%d
    ",query(1,1,n,a,b));
                else update(1,1,n,a,b);
            }
        }
        return 0;
    }
  • 相关阅读:
    设计模式:组合模式
    对技术的认识及思考
    设计模式:策略模式
    java集合:常用集合的数据结构
    设计模式:代理模式
    java反射
    Spring事务管理
    在Spring使用junit注解进行单元测试
    tomcat限制ip访问
    获取openid回调两次
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/4746283.html
Copyright © 2011-2022 走看看