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:\n", ++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\n", 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); } }
posted on 2015-11-02 09:43 chuninsane 阅读(214) 评论(0) 编辑 收藏 举报