The question is simple: Enter n numbers first and then perform a series of operations!
S Num Sum is to change the value of the num number to sum;
M a B is the sum of the numbers from position a to position B!
This is basically a raw tree array. You can directly use a tree array and then print the corresponding code based on the question! Pay attention to the details and you will be able to access the Internet !!!
The Code is as follows:
#include<iostream>#include<cstdio>#include<cstring>//UVA 2191 Potentiometersusing namespace std;const int maxn=1000000;int c[maxn];int ss[maxn];int n;int lowbit(int x){ return x&(-x);}int sum(int x){ int res=0; while(x>0) { res+=c[x]; x-=lowbit(x); } return res;}void update(int x,int d){ while(x<=n) { c[x]+=d; x+=lowbit(x); }}int main(){ int ji=1; while(cin>>n) { memset(c,0,sizeof(c)); if(!n) break; for(int i=1; i<=n; i++) { cin>>ss[i]; update(i,ss[i]); } char s[10]; int a,b; if(ji!=1) cout<<endl; printf("Case %d:\n",ji++); while(cin>>s) { if(!strcmp(s,"END")) break; if(s[0]=='S') { cin>>a>>b; update(a,b-ss[a]); ss[a]=b; } if(s[0]=='M') { cin>>a>>b; cout<<sum(b)-sum(a-1)<<endl; } } } return 0;}
UV 2191 Potentiometers