首页 > 其他 > 详细

uva 12086 - Potentiometers

时间:2015-08-20 22:06:36      阅读:204      评论:0      收藏:0      [点我收藏+]

是一道树状数组的模板题

 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. It
has two terminals and some kind of control mechanism (often a dial, a wheel or a slide) with which the
resistance 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 the
other, 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 of
N
potmeters, numbered 1 to
N
from left to right. The
left terminal of some potmeter numbered
x
is connected to the right terminal of potmeter
x

uva 12086 - Potentiometers

原文:http://www.cnblogs.com/tianmin123/p/4746222.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!