LA2191:Potentiometers(树状数组)

题意:

先给出一个数组,然后有两个操作

S x y 把第x个数改成y

M x y计算x~y个数的和


思路:

普通的树状数组,但不知道为什么会莫名其妙的一直WA


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 5000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)

LL c[N];
int a[N],n;

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

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

int main()
{
    int i,j,k,x,y,cas = 1;
    char str[10];
    scanf("%d",&n);
    while(n)
    {
        printf("Case %d:\n",cas++);
        for(i = 1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        MEM(c,0);
        for(i = 1;i<=n;i++)
        {
            add(i,a[i]);
        }
        while(1)
        {
            scanf("%s",str);
            if(!strcmp(str,"END")) break;
            if(str[0]=='M')
            {
                scanf("%d%d",&x,&y);
                LL ans = sum(y)-sum(x-1);
                printf("%lld\n",ans);
            }
            else if(str[0]=='S')
            {
                scanf("%d%d",&x,&y);
                add(x,y-a[x]);
                a[x] = y;
            }
        }
        scanf("%d",&n);
        if(n)
        printf("\n");
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值