zoukankan      html  css  js  c++  java
  • P1531 I Hate It(线段树)

    题见洛谷
    一节课。。。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #define MAXN 200000 
    using namespace std;
    int a0[MAXN+5],st[MAXN*4+5]; //四倍
    void build(int o,int l,int r)
    {
        if(l==r)
         st[o]=a0[l];
        else
        {
            int m=l+((r-l)>>1);
            build((o<<1),l,m);
            build((o<<1)|1,m+1,r);
            st[o]=max(st[o<<1],st[(o<<1)|1]); 
        }
    }
    void exch(int o,int l,int r,int ind,int ans)
    {
        if(l==r)
        {
          st[o]=ans;
          return; 
        }
        else
        {
            int m=l+((r-l)>>1);
            if(ind<=m)
            {
                exch((o<<1),l,m,ind,ans);//|1相当于+1 
            }
            else
            {
                exch((o<<1)|1,m+1,r,ind,ans);
            }   
        }
        st[o]=max(st[o<<1],st[(o<<1)|1]);//第二次写时,忘记这步  :-(
    }
    int sear(int o,int l,int r,int ql,int qr)
    {
        if(qr<l||ql>r)return 0;
        if(qr>=r&&ql<=l)return st[o];
        int m=l+((r-l)>>1);
        return max(sear((o<<1),l,m,ql,qr),sear((o<<1)|1,m+1,r,ql,qr));
    }
    int main()
    {
        //freopen("a.in","r",stdin);
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
         scanf("%d",&a0[i]); 
    
        build(1,1,n);
    
        for(int i=1;i<=m;i++)
        {
            string c;
            int a,b;
            cin>>c;
            scanf("%d%d",&a,&b);
            if(c=="Q")
             printf("%d
    ",sear(1,1,n,a,b));
            else
             if(a0[a]<b)
            a0[a]=b,exch(1,1,n,a,b);//a0[]需要修改
        }
        return 0;
    }
  • 相关阅读:
    UVA 562 Dividing coins
    who is in front of me 解题报告
    UVA 111 历史考试
    UVA 1045 最长公共子序列
    HDU 1003 解题报告
    ACM2014-04训练计划
    基于邻接表的新顶点的增加
    4004.六度空间理论
    4003.基于Dijsktra算法的最短路径求解
    4002.基于快排思想的查找
  • 原文地址:https://www.cnblogs.com/dfsac/p/6819791.html
Copyright © 2011-2022 走看看