uva 12086 - Potentiometers

1、http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3238

2、题目大意:

给定N个电阻值,然后有两种操作,M a b操作是查询a b之间的电阻值之和,S a b 操作时改变a电阻值为b,对于每一个M操作,输出一个电阻值之和,此题与hdu 1166 完全类似,代码一遍水过。。。

3、题目:

N - datastruct-easy
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Appoint description:

Description

Download as PDF
 
 

B

 
 
 

Potentiometers

 

Input: Standard Input

 

Output:  Standard Output

 

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

 

4、ac代码:

#include<stdio.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[200005*4];
void build(int l,int r,int rt)
{
    if(l==r)
    {
        scanf("%d",&sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r)
        return sum[rt];
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m)
        ret+=query(L,R,lson);
    if(R>=m+1)
        ret+=query(L,R,rson);
    return ret;
}
void update(int a,int b,int l,int r,int rt)
{
    if(l==r)
    {
        sum[rt]=b;
        return ;
    }
    int m=(l+r)>>1;
    if(a<=m)
        update(a,b,lson);
    else
        update(a,b,rson);
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
int main()
{
    int t,n;
    char str[10];
    int a,b,cas=0;
    while(scanf("%d",&n))
    {
        if(n==0)
        break;
        cas++;
        if(cas!=1)
        {
            printf("\n");
        }
        printf("Case %d:\n",cas);
        build(1,n,1);
        while(scanf("%s",str)!=EOF)
        {
            if(str[0]=='E')
                break;
            else if(str[0]=='S')
            {
                scanf("%d%d",&a,&b);
                update(a,b,1,n,1);
            }
            else if(str[0]=='M')
            {
                scanf("%d%d",&a,&b);
                printf("%d\n",query(a,b,1,n,1));
            }
            /*for(int i=1;i<=25;i++)
            printf("sum[%d]=%d ",i,sum[i]);
            printf("\n");
            */
        }
    }
    return 0;
}
/*
1
10
1 2 3 4 5 6 7 8 9 10
Query 1 3
Add 3 6
Query 2 7
Sub 10 2
Add 6 3
Query 3 10
End
*/
/*
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
*/


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值