LA2191—Potentiometers (树状数组)

题目链接:传送门


题目大意:

给定n个整数,实现下面的两种操作

S x y(把第x个数改成y)  M x y(计算第x个数到第y个数之和)。


代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>

using namespace std;

#define lowbit(x) (x&-x)

typedef long long llt;

const int N = 200010;
const int M = 50010;
const int INF = 0x3fffffff;

int C[N],A[N];

void init(int n)
{
    memset(C,0,sizeof(C));
    for(int i = 1; i <= n; ++i){
        for(int j = i-lowbit(i)+1; j <= i; ++j){
            C[i] += A[j];
        }
    }
}

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

//A[x] += d;
void add(int x,int d,int n)
{
    while(x <= n){
        C[x] += d;
        x += lowbit(x);
    }
}

char st[100];

int main()
{
//    freopen("D:\\06.in","r",stdin);
//    freopen("D:\\data.txt","w",stdout);
    int n,cnt = 0;
    while(~scanf("%d",&n) && n){
        for(int i = 1; i <= n; ++i) scanf("%d",&A[i]);
        init(n);
        char ch; int a,b;
        getchar();
        if(cnt) printf("\n");
        printf("Case %d:\n",++cnt);
        while(1){
            gets(st);
            if(strcmp(st,"END") == 0) break;
            sscanf(st,"%c %d %d",&ch,&a,&b);
            if(ch == 'S'){
                add(a,b-A[a],n);
                A[a] = b;
            }else{
                printf("%d\n",sum(b)-sum(a-1));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值