• HDU1166 敌兵布阵(线段树基础题单节点更新区间查询)


    这题用树状数组写要简单很多,因为我学习线段树,找了这个题入门。。

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <queue>
    #include <cmath>
    #include <algorithm>
    using namespace std;
    #define M 50005
    #define ls node<<1,l,m
    #define rs node<<1|1,m+1,r
    int n;
    int Tree[M*4];
    void buildtree(int node,int l,int r)
    {
        if(l==r) scanf("%d",&Tree[node]);
        else
        {
            int m=(l+r)>>1;
            buildtree(ls);
            buildtree(rs);
            Tree[node]=Tree[node<<1]+Tree[node<<1|1];
        }
    }
    void update(int node,int l,int r,int ind,int add)
    {
        if(l==r)
        {
            Tree[node]+=add;
            return ;
        }
        int m=(l+r)>>1;
        if(ind<=m) update(ls,ind,add);
        else update(rs,ind,add);
        Tree[node]+=add;
    }
    int query(int node,int l,int r,int L,int R)
    {
        if(L<=l&&r<=R) return Tree[node];
        int m=(l+r)>>1;
        int ans=0;
        if(m>=L) ans+=query(ls,L,R);
        if(m<R) ans+=query(rs,L,R);
        return ans;
    }
    int main()
    {
        freopen("in.txt","r",stdin);
        int t,cas=1,x,y;
        scanf("%d",&t);
        while(t--)
        {
            char s[9];
            printf("Case %d:
    ",cas++);
            scanf("%d",&n);
            buildtree(1,1,n);
            scanf("%s",s);
            while(1)
            {
                if(s[0]=='E') break;
                scanf("%d%d",&x,&y);
                if(s[0]=='A') update(1,1,n,x,y);
                else if(s[0]=='S') update(1,1,n,x,-y);
                else printf("%d
    ",query(1,1,n,x,y));
                scanf("%s",s);
            }
        }
        return 0;
    }
  • 相关阅读:
    hive表增量抽取到oracle数据库的通用程序(二)
    java进程的守护进程脚本
    hadoop2.7节点的动态增加与删除
    hive表增量抽取到oracle数据库的通用程序(一)
    arduino 驱动电调
    arduino IO口
    通过电机编码器AB相输出确定电机转向
    Wifi小车资料
    winform 按键控制
    vs2010 EF4.0 访问mysql
  • 原文地址:https://www.cnblogs.com/d-e-v-i-l/p/4744619.html
Copyright © 2020-2023  润新知