zoukankan      html  css  js  c++  java
  • Hdu1754单点更新

      和上一题差不多,单点更新,求和,求最值,求乘积之类的,都可以照着搞。

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <climits>
    #include <string>
    #include <iostream>
    #include <map>
    #include <cstdlib>
    #include <list>
    #include <set>
    #include <queue>
    #include <stack>
    
    using namespace std;
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    int sum[888888];
    void up(int rt)
    {
        sum[rt] = max(sum[rt << 1], sum[rt << 1 | 1]);
    }
    void build(int l, int r, int rt)
    {
        if (l == r){
            scanf("%d", &sum[rt]); return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        up(rt);
    }
    
    int ask(int L, int R, int l, int r, int rt)
    {
        if (L <= l&&r <= R)return sum[rt];
        int mid = (l + r) >> 1;
        int ans = 0;
        if (L <= mid) ans = max(ans, ask(L, R, lson));
        if (R>mid) ans = max(ans, ask(L, R, rson));
        return ans;
    }
    
    void update(int key, int add, int l, int r, int rt)
    {
        if (l == r){
            sum[rt] = add; return;
        }
        int mid = (l + r) >> 1;
        if (key <= mid) update(key, add, lson);
        else update(key, add, rson);
        up(rt);
    }
    int main()
    {
        int n, m;
        int a, b;
        char str[100];
        while (scanf("%d%d", &n, &m) != EOF){
            build(1, n, 1);
            for (int i = 0; i<m; i++){
                scanf("%s", str);
                scanf("%d%d", &a, &b);
                if (str[0] == 'U'){
                    update(a, b, 1, n, 1);
                }
                else printf("%d
    ", ask(a, b, 1, n, 1));
            }
        }
        return 0;
    }
  • 相关阅读:
    [JSOI2015]最小表示
    [洛谷2002]消息扩散
    [洛谷1726]上白泽慧音
    [CodeVS2822]爱在心中
    [POJ2186]Popular Cows
    [洛谷1991]无线通讯网
    [CQOI2009]跳舞
    [洛谷1342]请柬
    [USACO07JAN]Balanced Lineup
    [NOIp2003提高组]神经网络
  • 原文地址:https://www.cnblogs.com/yigexigua/p/3928226.html
Copyright © 2011-2022 走看看