线段树模板QAQ

两道水题,都抄的模板QAQ

HDU 1166 敌兵布阵

#include<iostream>
#include<cstdio>
using namespace std;

#define maxn 50005
int tree[maxn<<2];
void build(int left,int right,int root)
{
    if(left==right)
    {
        scanf("%d",&tree[root]);
        return ;
    }
    int mid=(left+right)>>1;
    build(left,mid,root<<1);
    build(mid+1,right,root<<1|1);
    tree[root]=tree[root<<1]+tree[root<<1|1];
}
int query(int x,int y,int left,int right,int root)
{
    if(x<=left&&right<=y)
    {
        return tree[root];
    }
    int sum=0,mid=(left+right)>>1;
    if(x<=mid)sum=sum+query(x,y,left,mid,root<<1);
    if(y>mid)sum=sum+query(x,y,mid+1,right,root<<1|1);
    return sum;

}
void update(int p,int add,int left,int right,int root)
{
    if(left==right)
    {
        tree[root]+=add;
        return ;
    }

    int mid=(left+right)>>1;
    if(p<=mid)update(p,add,left,mid,root<<1);
    else update(p,add,mid+1,right,root<<1|1);
    tree[root]=tree[root<<1]+tree[root<<1|1];

}
int main()
{
   // printf("%d %d\n",2<<4,100>>2);
    //freopen("in.txt","r",stdin);
    int n,t,x,y;
    char o[10];
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        printf("Case %d:\n",i);
        scanf("%d",&n);
        build(1,n,1);
        while(scanf("%s",o))
        {
            if(o[0]=='E'){
                break;
            }
            scanf("%d %d",&x,&y);
            if(o[0]=='A')
                update(x,y,1,n,1);
            else if(o[0]=='S'){
                update(x,-y,1,n,1);
            }
            else
                printf("%d\n",query(x,y,1,n,1));
        }
    }
    return 0;
}

HDU 1754 I Hate It

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std;
 
 
int tree[800005];
void build(int left,int right,int root)
{
    if(left==right)
    {
        scanf("%d",&tree[root]);
    }
    else
    {
        int mid=(left+right)>>1;
        build(left,mid,root<<1);
        build(mid+1,right,root<<1|1);
        tree[root]=max(tree[root<<1],tree[root<<1|1]);
    }
}
void update(int p,int add,int left,int right,int root)
{
    if(left==right)
    {
        tree[root]=add;
    }
    else
    {
        int mid=(left+right)>>1;
        if(p<=mid)update(p,add,left,mid,root<<1);
        else update(p,add,mid+1,right,root<<1|1);
        tree[root]=max(tree[root<<1],tree[root<<1|1]);
    }
}
int query(int x,int y,int left,int right,int root)
{
    if(x<=left&&right<=y)
    {
        return tree[root];
    }
    else
    {
        int sum=0,mid=(left+right)>>1;
        if(x<=mid)sum=max(sum,query(x,y,left,mid,root<<1));
        if(y>mid)sum=max(sum,query(x,y,mid+1,right,root<<1|1));
        return sum;
    }
}
int main()
{
    int n,m,x,y;
    char o[3];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            scanf("%s %d %d",o,&x,&y);
            if(o[0]=='U')
                update(x,y,1,n,1);
            else
                printf("%d\n",query(x,y,1,n,1));
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值