A potentiometer, or potmeter for short, is anelectronic device with a variable electric resistance. It has two terminals andsome kind of control mechanism (often a dial, a wheel or a slide) with whichthe resistance between the terminals can be adjusted from zero (no resistance)to some maximum value. Resistance is measured in Ohms, and when two or moreresistors are connected in series (one after the other, in a row), the totalresistance of the array is the sum of the resistances of the individualresistors.
In this problem we will consider an array of Npotmeters, numbered 1 to N from left to right. The left terminalof some potmeter numbered x is connected to the right terminal ofpotmeter x-1, and its right terminal to the left terminal of potmeter x+1.The left terminal of potmeter 1 and the right terminal of potmeter Nare not connected.
Initially all the potmeters are set to some valuebetween 0 and 1000 Ohms. Then we can do two things:
- Set one of the potmeters to another value.
- Measure the resistance between two terminals anywhere in the array.
Input
The input consists less than 3cases. Each case starts with N, the number of potmeters in the array, ona line by itself. N can be as large as 200000. Each of next N linescontains one numbers between 0 and 1000, the initial resistances of thepotmeters in the order 1 to N. Then follow a number of actions,each on a line by itself. The number of actions can be as many as 200000. Thereare three types of action:
- "S x r" - set potmeter x to r Ohms. x is a valid potmeter number and r is between 0 and 1000.
- "M x y" - measure the resistance between the left terminal of potmeter x and the right terminal of potmeter y. Both numbers will be valid and x is smaller than or equal to y.
- "END" - end of this case. Appears only once at the end of a list of actions.
A case with N=0 signalsthe end of the input and it should not be processed.
Output
For each case in the input producea line "Case n:", where n is the case number, starting from 1.
For each measurement in the input, output a line containing one number:
themeasured resistance in Ohms. The actions should be applied to the
array ofpotmeters in the order given in the input.
Print a blank line between cases.
Warning: Input Data is prettybig (~ 8 MB) so use faster IO.
Sample Input Output for Sample Input
3 100 100 100 M 1 1 M 1 3 S 2 200 M 1 2 S 3 0 M 2 3 END 10 1 2 3 4 5 6 7 8 9 10 M 1 10 END 0 |
Case 1: 100 300 300 200
Case 2: 55 |
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: ",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 ",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 }