UV 2191 Potentiometers

Source: Internet
Author: User

The question is simple: Enter n numbers first and then perform a series of operations!

S Num Sum is to change the value of the num number to sum;

M a B is the sum of the numbers from position a to position B!


This is basically a raw tree array. You can directly use a tree array and then print the corresponding code based on the question! Pay attention to the details and you will be able to access the Internet !!!


The Code is as follows:

#include<iostream>#include<cstdio>#include<cstring>//UVA 2191 Potentiometersusing namespace std;const int maxn=1000000;int c[maxn];int ss[maxn];int n;int lowbit(int x){    return x&(-x);}int sum(int x){    int res=0;    while(x>0)    {        res+=c[x];        x-=lowbit(x);    }    return res;}void update(int x,int d){    while(x<=n)    {        c[x]+=d;        x+=lowbit(x);    }}int main(){    int ji=1;    while(cin>>n)    {        memset(c,0,sizeof(c));        if(!n)            break;        for(int i=1; i<=n; i++)        {            cin>>ss[i];            update(i,ss[i]);        }        char s[10];        int a,b;        if(ji!=1)            cout<<endl;        printf("Case %d:\n",ji++);        while(cin>>s)        {            if(!strcmp(s,"END"))                break;            if(s[0]=='S')            {                cin>>a>>b;                update(a,b-ss[a]);                ss[a]=b;            }            if(s[0]=='M')            {                cin>>a>>b;                cout<<sum(b)-sum(a-1)<<endl;            }        }    }    return 0;}


UV 2191 Potentiometers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.