LA2191:Potentiometers(樹狀數組),

來源:互聯網
上載者:User

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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.