uva 12086 - Potentiometers(树状数组)
2014-08-25 14:05
274 查看
题目链接:uva 12086 - Potentiometers
题目大意:给定n个整数,两个操作,
S x y:把第x个数变成y
M x y:计算第x个数到第y个数的总和
解题思路:用num数组记录每个位置上的数,每次S操作则在树状数组上的x位置加上num[x] - y的值。
题目大意:给定n个整数,两个操作,
S x y:把第x个数变成y
M x y:计算第x个数到第y个数的总和
解题思路:用num数组记录每个位置上的数,每次S操作则在树状数组上的x位置加上num[x] - y的值。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define lowbit(x) ((x)&(-x)) const int maxn = 200005; int N, fenw[maxn], num[maxn]; void add (int x, int v) { while (x <= N) { fenw[x] += v; x += lowbit(x); } } int sum (int x) { int ret = 0; while (x) { ret += fenw[x]; x -= lowbit(x); } return ret; } int main () { int cas = 0; while (scanf("%d", &N) == 1 && N) { memset(fenw, 0, sizeof(fenw)); for (int i = 1; i <= N; i++) { scanf("%d", &num[i]); add(i, num[i]); } if (cas) printf("\n"); printf("Case %d:\n", ++cas); int x, y; char order[15]; while (scanf("%s", order) == 1 && strcmp(order, "END")) { scanf("%d%d", &x, &y); if (order[0] == 'M') printf("%d\n", sum(y) - sum(x-1)); else { add(x, y - num[x]); num[x] = y; } } } return 0; }
相关文章推荐
- UVA 12086 - Potentiometers(树状数组)
- UVA 12086 Potentiometers(树状数组)
- uva 12086 - Potentiometers(树状数组)
- UVa12086 - Potentiometers(树状数组即Fenwick树)
- UVA 12086 Potentiometers(树状数组|| 线段树单点更新)
- uva 12086 - Potentiometers (树状数组)
- UVA 12086 - Potentiometers(树状数组)
- UVA12086 树状数组 + 裸 +单点重置
- UVA 12086 (树状数组)
- UVALive 2191 - Potentiometers (树状数组)
- UVALive 2191 Potentiometers (树状数组)
- UVALive 4329 树状数组第二题
- 【uva】1428 - Ping pong(树状数组)
- 树状数组(LA4329,UVaLive4329,Ping pong)
- UVALive 4329 Ping pong(解法二:树状数组)
- UVa 12086 - Potentiometers
- UVALive 5902 Movie collection(树状数组)
- uva 1428 - Ping pong (树状数组的应用)
- UVALive 4329 Ping pong(树状数组)
- uva 12086 - Potentiometers