uva 12086 - Potentiometers

本文介绍了一种处理电位器阵列中电阻测量与调整的问题,通过使用一种特殊的数据结构来高效地进行电阻的设置与测量操作。具体包括电位器的工作原理、电阻的串联计算方式以及如何在不改变其他电位器的情况下调整单个电位器的电阻。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:\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 }

 

转载于:https://www.cnblogs.com/tianmin123/p/4746222.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值