• UVALive 2191 Potentiometers (树状数组)


    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=192

    给你一个长度为n的数组A,有两种操作,操作一:(S x y) 将A[x]的大小置为y,操作二:(M x y) 求A[x], A[x+1] ... A[y]的和

    用一个额外的数组tmp存储数组A,然后将 S x y转换为树状数组的 add x y-tmp[x]

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define maxn 200010
    using namespace std;
    
    typedef long long LL;
    
    LL c[maxn];
    int n, arr[maxn];
    
    int lowbit(int x);
    
    void add(int x, int u);
    
    LL sum(int x);
    
    int main(void)
    {
        int ca = 0;
        while (scanf("%d", &n), n)
        {
            memset( c, 0, sizeof(c));
            int x, y;
            char ope[10];
            for (int i = 1; i <= n; ++i)
            {
                scanf("%d", arr + i);
                add( i, arr[i]);
            }
            if (ca)
                putchar(10);
            printf("Case %d:
    ", ++ca);
            while (scanf("%s", ope), ope[0] != 'E')
            {
                scanf("%d %d", &x, &y);
                if (ope[0] == 'S')
                {
                    add( x, y - arr[x]);
                    arr[x] = y;
                }
                else
                {
                    printf("%lld
    ", sum(y) - sum(x - 1));
                }
            }
        }
        return 0;
    }
    
    int lowbit(int x)
    {
        return x&(-x);
    }
    
    LL sum(int x)
    {
        LL result = 0;
        while (x > 0)
        {
            result += c[x];
            x -= lowbit(x);
        }
        return result;
    }
    
    void add(int x, int u)
    {
        while (x <= n)
        {
            c[x] += u;
            x += lowbit(x);
        }
    }
  • 相关阅读:
    flask插件系列之flask_cors跨域请求
    【电脑蓝屏记】
    .net 定时启动任务
    c# winform+wcf代理上网的处理
    WCF
    Sql Over的用法
    【转】c#的逆向工程-IL指令集
    【随记】代码混编的重要性
    【转】android学习日记01--综述
    c#获取网页代码、数据、资源
  • 原文地址:https://www.cnblogs.com/chuninsane/p/4929279.html
Copyright © 2020-2023  润新知