zoukankan      html  css  js  c++  java
  • I hate it 线段树

      题意: 两种操作  一个是把a的值改为b   一个是读取a到b中的最大值   最大值单点修改区域查询线段树  

    线段树3个函数都要return 不要忘记

    #include<bits/stdc++.h>
    using namespace std;
    #define N 220000
    #define lson L,m,pos<<1
    #define rson m+1,R,pos<<1|1
    #define mid  m=(L+R)>>1
    int sum[N<<2];
    
    void up(int pos)
    {
        sum[pos]=max(sum[pos<<1],sum[pos<<1|1]);
    }
    
    void build(int L,int R,int pos)
    {
        if(L==R)
        {
            scanf("%d",&sum[pos]);
            return ;
        }
        int mid;
        build(lson);
        build(rson);
        up(pos);
    }
    
    void update(int x,int v,int L,int R,int pos)
    {
        if(L==R)
        {
            sum[pos]=v;return ;
        }
        int mid;
        if(x<=m)update(x,v,lson);
        else   update(x,v,rson);
        up(pos);
    }
    
    int query(int l,int r,int L,int R,int pos)
    {
        if(l<=L&&r>=R)
        {
            return sum[pos];
        }
        int mid;
        int ans=0;
        if(l<=m)ans=max(ans,query(l,r,lson));
        if(r>m) ans=max(ans, query(l,r,rson));
        return ans;
    }
    
    int main()
    {
        int n,m;
      while(2==scanf("%d%d",&n,&m) )
       {
        build(1,n,1);
        while(m--)
        {
            int a,b;char s[2];
            scanf("%s%d%d",s,&a,&b);
            if(s[0]=='U')
                update(a,b,1,n,1);
            else
                printf("%d
    ",query(a,b,1,n,1));
        }
       }
        return 0;
    }
  • 相关阅读:
    POJ 3258 (NOIP2015 D2T1跳石头)
    POJ 3122 二分
    POJ 3104 二分
    POJ 1995 快速幂
    409. Longest Palindrome
    389. Find the Difference
    381. Insert Delete GetRandom O(1)
    380. Insert Delete GetRandom O(1)
    355. Design Twitter
    347. Top K Frequent Elements (sort map)
  • 原文地址:https://www.cnblogs.com/bxd123/p/10461298.html
Copyright © 2011-2022 走看看