Potentiometers

题意:

线段树的单点修改,区间查询

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 200010
const ll  INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod =  1000000007;
int num[N<<2],n,q;
void pushup(int rt){
    num[rt]=num[rt<<1]+num[rt<<1|1];
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%d",&num[rt]);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int p,int b,int l,int r,int rt){
    if(l==r){
        num[rt]=b;
        return;
    }
    int m=(l+r)>>1;
    if(p<=m)update(p,b,lson);
    else update(p,b,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
        if(L<=l&&R>=r){
            return num[rt];
        }
        int m=(l+r)>>1;
       int sum=0;
       if(L<=m)sum+=query(L,R,lson);
       if(R>m)sum+=query(L,R,rson);
       pushup(rt);
      return sum;
}
int main()
{
    char s[4];
    int a,b,cas=0;
    while(~scanf("%d",&n)&&n){
            cas++;
        if(cas!=1)printf("\n");
        build(1,n,1);
        printf("Case %d:\n",cas);
        while(~scanf("%s",s)){
                if(s[0]=='E')
                break;
            else if(s[0]=='M'){
                    scanf("%d%d",&a,&b);
                printf("%d\n",query(a,b,1,n,1));
            }
            else if(s[0]=='S'){
                scanf("%d%d",&a,&b);
                update(a,b,1,n,1);
            }
        }
    }
return 0;
}

 

posted on 2015-10-25 22:25  积跬步、至千里  阅读(180)  评论(0编辑  收藏  举报

导航