zoukankan      html  css  js  c++  java
  • 线段树求最值

    http://acm.hdu.edu.cn/showproblem.php?pid=1754

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define maxn 200005
    int  tree[4*maxn];
    void build(int left,int right,int root)
    {
        if(left==right)
        {
            scanf("%d",&tree[root]);
        }
        else
        {
            int mid=(left+right)/2;
            build(left,mid,root*2);
            build(mid+1,right,root*2+1);
            tree[root]=max(tree[root*2],tree[root*2+1]);
        }
    }
    void Pointupdate(int pos,int newn,int left,int right,int root)
    {
        if(left==right)
        {
            tree[root]=newn;
        }
        else
        {
            int mid=(left+right)/2;
            if(pos<=mid)
                Pointupdate(pos,newn,left,mid,root*2);
            else
                Pointupdate(pos,newn,mid+1,right,root*2+1);
            tree[root]=max(tree[root*2],tree[root*2+1]);
    
        }
    }
    int query(int x,int y,int left,int right,int root)
    {
        if(x<=left&&right<=y)
        {
            return tree[root];
        }
        else
        {
            int cnt=0;
            int mid=(left+right)/2;
            if(x<=mid)
              cnt=max(cnt,query(x,y,left,mid,root*2));
            if(y>mid)
              cnt=max(cnt,query(x,y,mid+1,right,root*2+1));
              return cnt;
        }
    
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            build(1,n,1);
            while(m--)
            {
                char ch[3];
                int x,y;
                scanf("%s%d%d",ch,&x,&y);
                if(ch[0]=='Q')
                    printf("%d
    ",query(x,y,1,n,1));
                else
                    Pointupdate(x,y,1,n,1);
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    spring中applicationContext.xml配置文件
    获取当前项目的名称
    文件上传
    bean工厂
    过滤器
    hibernate入门
    struts2入门案例
    第一个分派struts2实例
    mysql实用函数
    通过jquery 获取用户当前所在的城市名称和IP地址
  • 原文地址:https://www.cnblogs.com/lyf123456/p/3383003.html
Copyright © 2011-2022 走看看