uva 12086 - Potentiometers

线段树or树状数组均可以做此题,不过树状数组的代码量要小:
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define INF 0x3fffffff
#define inf -0x3f3f3f3f
#define N 200010
#define M 4000010
#define LL long long
#define mod 20071027

using namespace std;

int n;
int d[N], arr[N];

int lowbit(int x){
    return x & (-x);
}

void add(int x, int val){
    while(x <= n){
        d[x] += val;
        x += lowbit(x);
    }
}

int sum(int x){
    int s = 0;
    while(x > 0){
        s += d[x];
        x -= lowbit(x);
    }
    return s;
}

int main() {
   // freopen("in.txt", "r", stdin);
    int t = 0;
    while(scanf("%d", &n) != EOF){
        if(! n) break;
        memset(d, 0, sizeof(d));
        for(int i = 1; i <= n; ++ i){
            scanf("%d", &arr[i]);
            add(i, arr[i]);
        }
        if(t) puts("");
        printf("Case %d:\n", t + 1);
        char str[5];
        while(scanf("%s", str) != EOF){
            if(str[0] == 'E') break;
            int l, r;
            scanf("%d %d", &l, &r);
            if(str[0] == 'S'){
                add(l, r - arr[l]);
                arr[l] = r;
                continue;
            }
            printf("%d\n", sum(r) - sum(l - 1));
        }
        ++ t;
       // printf("  %d\n", d[2]);
    }
    return 0;
}

你可能感兴趣的:(uva)