zoukankan      html  css  js  c++  java
  • hdoj1166【线段树】

    单点更新+区间求和
    不多说,直接上渣code………

    #include<cstdio>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    const int N=50007;
    struct st{
        int left,right;
        int w;
    };
    st q[N*4];
    int n;
    
    
    
    void build(int num,int L,int R)
    {
        q[num].left=L;
        q[num].right=R;
        if(L==R)
        {
            scanf("%d",&q[num].w);
            return;
        }
        build(2*num,L,(L+R)/2);
        build(2*num+1,(L+R)/2+1,R);
        q[num].w=q[2*num].w+q[2*num+1].w;
    }
    
    int Query(int num,int s,int t)
    {
        if(q[num].left>=s&&q[num].right<=t)
            return q[num].w;
        int mid=(q[num].left+q[num].right)/2;
        int ans=0;
        if(mid>=t)
            ans+=Query(2*num,s,t);
        else if(mid<s)
            ans+=Query(2*num+1,s,t);
        else{
            ans+=Query(2*num,s,mid);
            ans+=Query(2*num+1,mid+1,t);
        }
        return ans;
    }
    void add(int num,int i,int j)
    {
        if(q[num].left==q[num].right&&q[num].left==i)
        {
            q[num].w+=j;
            return;
        }
        int mid=(q[num].left+q[num].right)/2;
        if(mid>=i)
            add(2*num,i,j);
        else if(mid<i)
            add(2*num+1,i,j);
        q[num].w=q[2*num].w+q[2*num+1].w;
    }
    
    int main()
    {
        int t;
        int cas=1;
        char ss[5];
        int a,b;
        scanf("%d",&t);
        while(t--)
        {
            printf("Case %d:
    ",cas++);
            scanf("%d",&n);
            build(1,1,n);
            while(scanf("%s",ss))
            {
                if(strcmp(ss,"End")==0)
                    break;
                scanf("%d%d",&a,&b);
                if(strcmp(ss,"Query")==0)
                    printf("%d
    ",Query(1,a,b));
                else if(strcmp(ss,"Sub")==0)
                    add(1,a,-b);
                else
                    add(1,a,b);
            }
        }
    }
  • 相关阅读:
    Python 缓冲区
    Python接收执行参数
    Python编码
    Maven 多环境 打包
    JS 时间 获取 当天,昨日,本周,上周,本月,上月
    Window Mysql 5.7.18安装
    Eclipse 更改Maven项目名
    Redis 命令
    Redis 安装 和 启动
    Mongodb 安装 和 启动
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934389.html
Copyright © 2011-2022 走看看