zoukankan      html  css  js  c++  java
  • hdu 1166 线段树

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    
    using namespace std;
    const int maxn=1000007;
    #define lson rt<<1
    #define rson rt<<1|1
    int a[maxn];
    
    struct da
    {
        int l, r;
        int s;
    } tree[maxn<<2];
    
    void build(int l, int r, int rt)
    {
        tree[rt].l = l;
        tree[rt].r = r;
    
        if(l == r)
        {
            tree[rt].s = a[l];
            return ;
        }
        int mid = (l+r)/2;
        build(l, mid, lson);
        build(mid+1, r, rson);
        tree[rt].s = tree[lson].s + tree[rson].s;
    }
    
    void update(int pos, int val, int rt)
    {
        tree[rt].s+=val;
        if(tree[rt].l==tree[rt].r)
            return ;
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(pos<=mid) update(pos, val, lson);
        else update(pos, val, rson);
    }
    
    int query(int l, int r, int rt)
    {
        if(tree[rt].l==l&&tree[rt].r==r)
            return tree[rt].s;
        int mid=(tree[rt].l+tree[rt].r)/2;
        if(r<=mid)
            query(l, r, lson);
        else if(l>mid)
            query(l, r, rson);
        else
        {
            int lsum, rsum;
            lsum=query(l, mid, lson);
            rsum=query(mid+1, r, rson);
            return lsum+rsum;
        }
    }
    int main()
    {
        int T, n, cas=1;
        char op[20];
        scanf("%d", &T);
    
        while(T--)
        {
            scanf("%d", &n);
            for(int i=1; i<=n; i++)
                scanf("%d", &a[i]);
    
            build(1, n, 1);
    
            int a, b;
    
            printf("Case %d:
    ", cas++);
    
            while(scanf("%s", op), strcmp(op, "End"))
            {
                if(op[0]=='Q')
                {
                    scanf("%d%d", &a, &b);
                    printf("%d
    ", query(a, b, 1));
                    continue;
                }
                int sign=1;
                scanf("%d%d", &a, &b);
                if(op[0]=='S')
                    sign*=-1;
    
                update(a, b*sign, 1);
            }
        }
        return 0;
    }
  • 相关阅读:
    商贸通帐套隐藏方法
    固定资产打开提示:上年度数据未结转!
    ZOJ 2432 Greatest Common Increasing Subsequence
    POJ 1080 Human Gene Functions
    POJ 1088 滑雪
    POJ 1141 Brackets Sequence
    POJ 1050 To the Max
    HDOJ 1029 Ignatius and the Princess IV
    POJ 2247 Humble Numbers
    HDOJ 1181 变形课
  • 原文地址:https://www.cnblogs.com/9968jie/p/5659635.html
Copyright © 2011-2022 走看看