UVALive 2191 - Potentiometers (樹狀數組)

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


題意:

給定n個整數,有一下操作:

S x y 把第x個數改爲y

M x y 計算第x個數到第y個數的和



記錄一下每個數的值,然後S x y的時候就可以計算出d值滿足 val[x] + d = y

然後樹狀數組維護一下就行了

注意兩個樣例之間輸出換行 否則會WA而不是PE




#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>

using namespace std;

//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif

#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
#define lowbit(x) (x&-x)

const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));

const int maxn = 200000 + 20;
LL C[maxn];
LL val[maxn];
int n;

LL sum(int x) {
    LL ret = 0;
    while(x > 0) {
        ret += C[x];
        x -= lowbit(x);
    }
    return ret;
}

void add(int x, LL v) {
    while(x <= n) {
        C[x] += v;
        x += lowbit(x);
    }
}

char str[10];

int main() {
    int kase = 1;

    while(scanf("%d", &n) != EOF && n) {
        if(kase > 1) putchar('\n');
        printf("Case %d:\n", kase++);
        memset(C, 0, sizeof(LL) * (n+10));
        for(int i=1; i<=n; i++) {
            scanf("%lld", &val[i]);
            add(i, val[i]);
        }
        while(scanf("%s", str) != EOF && str[0] != 'E') {
            LL x, y;
            scanf("%lld%lld", &x, &y);
            if(str[0] == 'S') {
                LL d = y - val[x];
                add(x, d);
                val[x] = y;
            } else {
                LL ans = sum(y) - sum(x-1);
                P64I(ans);
            }
        }
    }

    return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章