zoukankan      html  css  js  c++  java
  • 线段树hdu1754


    #include<iostream>
    #include<stdio.h>
    using namespace std;
    const int maxa=200005;
    int val[maxa];
    struct tree{int max, left, right;
    }tree[maxa*3];
    int create(int root,int left,int right) //root的*2和*2+1分别存root的前后部分
    {
    tree[root].left=left;
    tree[root].right=right;
    if(left==right)
    return tree[root].max=val[left];
    int a,b,mid=(left+right)/2;
    a=create(root*2,left,mid);
    b=create(root*2+1,mid+1,right);
    return tree[root].max=max(a,b);
    }
    int change(int root,int pos,int vall) //当root代表的是改变的点时直接改变tree[root].max,其他与建树部分相同
    {
    if(pos<tree[root].left||tree[root].right<pos)
    return tree[root].max;
    if(pos==tree[root].right&&pos==tree[root].left)
    return tree[root].max=vall;
    int a,b;
    a=change(root*2,pos,vall);
    b=change(root*2+1,pos,vall);
    return tree[root].max=max(a,b);
    }
    int query(int root,int left,int right)
    {
    if(tree[root].left>right||tree[root].right<left) //root范围与查询范围没有任何交集时return 0;
    return 0;
    if(tree[root].left>=left&&tree[root].right<=right) //root范围完全在查询范围时return tree[root].max;
    return tree[root].max;
    int a,b; //其他与建树部分相同
    a=query(root*2,left,right);
    b=query(root*2+1,left,right);
    return max(a,b);
    }
    int main()
    {
    //freopen("input.cpp","r",stdin);
    int n,m;
    char a;
    int x,y;
    while(~scanf("%d%d",&n,&m))
    {
    for(int i=1;i<=n;i++)
    {
    scanf("%d",&val[i]);
    }
    create(1,1,n);
    while(m--)
    {
    scanf(" %c",&a);
    scanf("%d%d",&x,&y);
    if(a=='Q')
    {
    printf("%d ",query(1,x,y));
    }
    else
    change(1,x,y);
    }
    }
    }

  • 相关阅读:
    Daemon Tools手工完全卸载方案
    不要轻易删除/windows/install下文件
    Dumpbin命令的使用
    v4l2 视频捕获
    2瓶4两酒,1个1.5两的酒杯
    n个平面分空间最多可分成多少份
    &#65279导致页面顶部空白一行解决方法
    Base64编码原理分析
    浏览器中“JavaScript解析器”工作原理
    IList转化为DataSet,解决了System.nullable()的问题
  • 原文地址:https://www.cnblogs.com/icodefive/p/3599177.html
Copyright © 2011-2022 走看看