本文主要是介绍UVALive - 2191 Potentiometers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:S操作将x改为y,M操作求[x,y]的和
思路:稍加改动一下树状数组就行了,当然线段树也可以
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 400005;int n,t[MAXN];int lowbit(int x){return x&(-x);
}void Update(int x,int d){while (x <= n)t[x] += d,x += lowbit(x);
}long long sum(int x){int ret = 0;while (x > 0)ret += t[x],x -= lowbit(x);return ret;
}int main(){int cas = 1;while (scanf("%d",&n) != EOF && n){memset(t,0,sizeof(t));for (int i = 1; i <= n; i++){int x;scanf("%d",&x);Update(i,x);}if (cas > 1)printf("\n");printf("Case %d:\n",cas++);while (1){char op[10];scanf("%s",op);if (op[0] == 'E')break;if (op[0] == 'S'){int x,y;scanf("%d%d",&x,&y);long long ans = sum(x) - sum(x-1);Update(x,y-ans);}else {int x,y;scanf("%d%d",&x,&y);printf("%lld\n",sum(y)-sum(x-1));}}}return 0;
}
这篇关于UVALive - 2191 Potentiometers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!