Thursday 1 October 2015

UVA 12086 Potentiometers (UVALive 2191, Regionals 2006 >> Asia - Dhaka)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 200010

int tree[MX];

void update(int x, int val)
{
    while(x<=200000)
    {
        tree[x]+=val;
        x+=(x & -x);
    }
return;
}

int read(int x)
{
    int val=0;
    while(x)
    {
        val+=tree[x];
        x-=x & -x;
    }
return val;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1, n, x, y;
    string s;
    char ch;
    while(cin>>n && n)
    {
        CLR(tree);
        for(int i=1;i<=n;i++)
        {
            cin>>x;
            update(i, x);
        }
        if(kk>1) cout<<"\n";
        cout<<"Case "<<kk++<<":\n";
        while(cin>>s && s!="END")
        {
            cin>>x>>y;
            if(s=="S")  update(x, y-read(x)+read(x-1));
            else cout<<read(y)-read(x-1)<<"\n";
        }
    }
return 0;
}

No comments:

Post a Comment