標籤:
是一道樹狀數組的模板題
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 int b[300000],a[300000],n; 6 int lowbit(int x) 7 { 8 return x&(-x); 9 }10 int sum(int x)11 {12 int sum=0;13 while(x>0)14 {15 sum=sum+b[x];16 x=x-lowbit(x);17 }18 return sum;19 }20 void add(int i,int x)21 {22 while(i<=n)23 {24 b[i]=b[i]+x;25 i=i+lowbit(i);26 }27 }28 int main()29 {30 int t=1;31 int cx=0;32 while(scanf("%d",&n)!=EOF&&n)33 {34 if(cx)35 puts("");36 memset(b,0,sizeof(b));37 int d,e,x;38 char c[10];39 for(int i=1; i<=n; i++)40 {41 scanf("%d",&a[i]);42 add(i,a[i]);43 }44 printf("Case %d:\n",t++);45 while(1)46 {47 scanf("%s",c);48 if(strcmp(c,"END")==0)49 break;50 if(c[0]==‘M‘)51 {52 scanf("%d%d",&d,&e);53 printf("%d\n",sum(e)-sum(d-1));54 }55 if(c[0]==‘S‘)56 {57 scanf("%d%d",&d,&e);58 x=e-a[d];59 a[d]=e;60 add(d,x);61 }62 }63 cx=n;64 }65 return 0;66 }
A potentiometer, or potmeter for short, is an electronic device with a variable electric resistance. Ithas two terminals and some kind of control mechanism (often a dial, a wheel or a slide) with which theresistance between the terminals can be adjusted from zero (no resistance) to some maximum value.Resistance is measured in Ohms, and when two or more resistors are connected in series (one after theother, in a row), the total resistance of the array is the sum of the resistances of the individual resistors.In this problem we will consider an array ofNpotmeters, numbered 1 toNfrom left to right. Theleft terminal of some potmeter numberedxis connected to the right terminal of potmeterx
uva 12086 - Potentiometers