UVALive 2191 Potentiometers(线段树RMQ)

来源:互联网 发布:db2和mysql语法的区别 编辑:程序博客网 时间:2024/05/19 23:56

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 − 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 N are not connected.
Initially all the potmeters are set to some value between 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 3 cases. Each case starts with N, the number of potmeters in the array,
on a line by itself. N can be as large as 200000. Each of next N lines contains one numbers between 0
and 1000, the initial resistances of the potmeters 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. There are 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 signals the end of the input and it should not be processed.
Output
For each case in the input produce a line ‘Case n:’, where n is the case number, starting from 1.
For each measurement in the input, output a line containing one number: the measured resistance
in Ohms. The actions should be applied to the array of potmeters in the order given in the input.
Print a blank line between cases.
Warning: Input Data is pretty big (∼ 8 MB) so use faster IO.
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
Sample Output
Case 1:
100
300
300
200
Case 2:
55

线段树RMQ
#include<stdio.h>#include<algorithm>#define N 200005using namespace std;struct Node{    int left,right;    int sum;}tree[4*N];int num[N],sum;void create(int root, int left, int right){    tree[root].left = left;    tree[root].right = right;    if(left==right)    {        tree[root].sum = num[left];        return ;    }    int mid = (left + right)>>1;    create(root<<1, left, mid);    create(root<<1|1, mid+1, right);    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;}void update(int root, int pos, int val){    if(tree[root].left==pos&&tree[root].right==pos)    {        tree[root].sum = val;        return ;    }    int mid = (tree[root].left + tree[root].right)>>1;    if(pos <= mid)        update(root<<1, pos, val);    else update(root<<1|1, pos, val);    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;}void query(int root, int left, int right){    if(tree[root].left==left&&tree[root].right==right)    {        sum += tree[root].sum;        return ;    }    int mid = (tree[root].left + tree[root].right)>>1;    if(right <= mid)        query(root<<1, left, right);    else if(left > mid)        query(root<<1|1, left, right);    else    {        query(root<<1, left, mid);        query(root<<1|1, mid+1, right);    }}int main(){    int T,n,cnt = 0;    char q[10];    while(~scanf("%d", &n) && n)    {        for(int i = 1; i <= n; ++i)            scanf("%d", &num[i]);        create(1, 1, n);        if(cnt && n) printf("\n");        printf("Case %d:\n", ++cnt);        while(~scanf("%s", q))        {            if(q[0]=='E')break;            int i,j;            scanf("%d%d", &i,&j);            sum = 0;            if(q[0]=='S')            {                update(1, i, j);            }            else if(q[0]=='M')            {                query(1, i, j);                printf("%d\n", sum);            }        }    }    return 0;}


0 0
原创粉丝点击