HDU 1166 敌兵布阵(线段树区间和)

题目大意:给N个点,会有2种操作,一个修改一个点的值,一个是求区间和

就是最基础的线段树,我是学了张昆玮神牛的统计的力量写的。所以代码风格跟那个一样。第一次写线段树一次A了好开心!

加了一个代码是递归的线段树,学习别人代码写的,因为ZKW的线段树对于区间修改我实在没看懂怎么做,貌似要维护2个线段树。所以还是去学了别人正统线段树的代码,速度也挺快的。

AC代码1:

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
using namespace std;
#define NMAX 50000000
#define MOD 1000000007
#define pb push_back
#define ll __int64
int T[1<<17];
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("o1.txt","w",stdout);
    int i,j,n,t,nct=1;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case %d:\n",nct++);
        scanf("%d",&n);
        int k;
        for(i = 0; i <= 16; i++) if(1<<i > n)
        {
            k = i;
            break;
        }
//        cout<<k<<endl;
        for(i = 0; i < n; i++)
        {
            scanf("%d",&T[i+(1<<k)]);
//            printf("T[%d]=%d ",i+(1<<k),T[i+(1<<k)]);
        }
        while(i < (1<<k))
        {
            T[i+(1<<k)] = 0;
            i++;
        }
//        cout<<endl;
        for(i = (1<<k)-1; i >= 1; i--)
        {
            T[i] = T[i+i]+T[i+i+1];
//            printf("T[%d] = %d\n",i,T[i]);
        }
        char c[10];
        while(scanf("%s",c) && strcmp(c,"End") !=0)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(strcmp(c,"Query") == 0)
            {
                int ans = 0;
                if(a == 1)
                {
                    a++;
                    ans = T[1<<k];
                }
                a += (1<<k)-2;
                b += (1<<k);
                for(;a^b^1; a >>= 1, b >>= 1)
                {
                    if(~a&1) ans += T[a^1];
                    if(b&1) ans += T[b^1];
                }
                printf("%d\n",ans);
            }
            if(strcmp(c,"Add") == 0)
            {
                T[(1<<k)+a-1] += b;
                i = (1<<k)+a-1;
                for(i >>= 1;i; i >>= 1)
                    T[i] = T[i+i] + T[i+i+1];
            }
            if(strcmp(c,"Sub") == 0)
            {
                T[(1<<k)+a-1] -= b;
                i = (1<<k)+a-1;
                for(i >>= 1;i; i >>= 1)
                    T[i] = T[i+i] + T[i+i+1];
            }
        }
    }
    return 0;
}


AC代码2:

#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<ctime>
using namespace std;

#define pb push_back
#define ll __int64
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
int sum[1<<18];

void pushup(int rt)
{
    sum[rt] = sum[rt<<1]+sum[rt<<1|1];
}

void build(int l, int r, int rt)
{
    if(l == r)
    {
        scanf("%d",&sum[rt]);
        return;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int p, int add, int l, int r, int rt)
{
    if(l == r)
    {
        sum[rt] += add;
        return;
    }
    int m = (l+r)>>1;
    if(p <= m) update(p,add,lson);
    else update(p,add,rson);
    pushup(rt);
}

int query(int L, int R, int l, int r, int rt)
{
    if(L <= l && R >= r)
        return sum[rt];
    int m = (l+r)>>1;
    int ret = 0;
    if(L <= m) ret += query(L,R,lson);
    if(R > m) ret += query(L,R,rson);
    return ret;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("o1.txt","w",stdout);
    int i,t,n,c=1;
    scanf("%d",&t);
    while(t--)
    {
        printf("Case %d:\n",c++);
        scanf("%d",&n);
        build(1,n,1);
        char temp[10];
        while(scanf("%s",temp)&&temp[0]!='E')
        {
            int a,b;
            scanf("%d%d",&a,&b);
            if(temp[0] == 'A') update(a,b,1,n,1);
            if(temp[0] == 'S') update(a,-b,1,n,1);
            if(temp[0] == 'Q') printf("%d\n",query(a,b,1,n,1));
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值