uvalive 2191(BIT)

来源:互联网 发布:蒙科立蒙古文网络平台 编辑:程序博客网 时间:2024/06/08 17:02

题意:给出n个数字,操作有修改和输出区间和。
题解:树状数组模板水题。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 200005;int C[N], n;char str[10];int lowbit(int x) {    return x & (-x);}int Sum(int x) {    int ret = 0;    while (x > 0) {        ret += C[x];        x -= lowbit(x);    }    return ret;}void Add(int x, int d) {    while (x <= n) {        C[x] += d;        x += lowbit(x);    }}int main() {    int cas = 0;    while (scanf("%d", &n) == 1 && n) {        memset(C, 0, sizeof(C));        int x, a, b;        for (int i = 1; i <= n; i++) {            scanf("%d", &x);            Add(i, x);        }        if (cas)                printf("\n");        printf("Case %d:\n", ++cas);        while (scanf("%s", str) && str[0] != 'E') {            scanf("%d%d", &a, &b);            if (str[0] == 'M')                printf("%d\n", Sum(b) - Sum(a - 1));            else {                x = Sum(a) - Sum(a - 1);                int temp = b - x;                Add(a, temp);            }        }    }    return 0;}
0 0
原创粉丝点击