zoukankan      html  css  js  c++  java
  • HDOJ1754(I Hate It)

    维护一个整数序列,支持2中操作:

    1、修改某个指定数的值;

    2、查询指定区间的最大值。

    View Code
    #include <stdio.h>
    #define MAX(a,b) ((a)>(b)?(a):(b))
    #define N 200001
    int n,m,D;
    int max[4*N];
    void init()
    {
        int i;
        for(D=1;D<n+2;D<<=1);
        for(i=1;i<2*D;i++)  max[i]=0;
        for(i=1;i<=n;i++)   scanf("%d",&max[i+D]);
        for(i=D-1;i^1;i--)  max[i]=MAX(max[i<<1],max[i<<1|1]);
    }
    void update(int x,int y)
    {
        max[x+D]=y;
        for(x+=D;x^1;x>>=1) max[x>>1]=MAX(max[x],max[x^1]);
    }
    void getmax(int x,int y)
    {
        int i=x+D-1,j=y+D+1,ret=0;
        for(;i^j^1;i>>=1,j>>=1)
        {
            if(~i&1)    ret=MAX(ret,max[i^1]);
            if(j&1) ret=MAX(ret,max[j^1]);
        }
        printf("%d\n",ret);
    }
    int main()
    {
        int i,a,b;
        char c;
        while(~scanf("%d%d",&n,&m))
        {
            init();
            for(i=0;i<m;i++)
            {
                c=0;
                while(c!='U' && c!='Q') scanf("%c",&c);
                scanf("%d%d",&a,&b);
                if(c=='Q')  getmax(a,b);
                else    update(a,b);
            }
        }
        return 0;
    }
  • 相关阅读:
    HashMap 原理?jdk1.7 与 1.8区别
    内存泄漏与溢出
    NIO
    Mysql 存储引擎
    编写一个 rpc
    dubbo 与 zookeeper
    MongoDB
    natapp 内网穿透服务
    【AHOI 2009】同类分布
    [HNOI 2016] 序列
  • 原文地址:https://www.cnblogs.com/algorithms/p/2586583.html
Copyright © 2011-2022 走看看