VJ16216/RMQ/线段树

题目链接

/*
单点更新,用RMQ维护最大值,add对c[i]修改,或加,或减。
求[l,r]的和,用sum(r)-sum(l-1).即可。
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long  LL;
const int maxn =500005;
int a[maxn];
int c[maxn];
char s[10];
int n,t;
void init()
{
    memset(c,0,sizeof(c));
}
int lowbit(int x)
{
    return x&(-x);
}
void add (int i,int v)
{
    while(i<=n)
    {
        c[i]+=v;
        i+=lowbit(i);
    }
}
int sum(int x)
{
    int ret=0;
    while(x>0)
    {
        ret+=c[x];
        x-=lowbit(x);
    }
    return ret;
}
int main ()
{
    int T;scanf("%d",&T);
    int k=0;
    while(T--)
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&t);
            add(i,t);
        }
        int l,r;
        printf("Case %d:\n",++k);
        while(true)
        {
            scanf("%s",s);
            if(s[0]=='Q')
            {
                scanf("%d%d",&l,&r);
                printf("%d\n",sum(r)-sum(l-1));
            }
            else if(s[0]=='S')
            {
                scanf("%d%d",&l,&r);
                add(l,-r);
            }
            else if(s[0]=='A')
            {
                scanf("%d%d",&l,&r);
                add(l,r);
            }
            else
                break;
        }
    }
    return 0;
}
/*
线段树的话就不说了,线段树处理单点更新的问题很好处理。
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long  LL;
const int maxn =500005;
struct node
{
    int l;
    int r;
    int sum;
};
node f[maxn*3];
int a[maxn];
int n,T,k=0;
char s[10];
void build(int root,int l,int r)
{
    f[root].l=l;
    f[root].r=r;
    if(l==r)
        f[root].sum=a[l];
    else
    {
        int mid=(l+r)>>1;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        f[root].sum=(f[root<<1].sum+f[root<<1|1].sum);
    }
}
int query(int root,int l,int r)
{
    if(f[root].l==l&&f[root].r==r)
        return f[root].sum;
    int mid=(f[root].l+f[root].r)>>1;
    if(r<=mid)
        return query(root<<1, l, r);
    else if(l>=mid+1)
        return query(root<<1|1, l, r);
    else
        return query(root<<1, l, mid)+query(root<<1|1, mid+1, r);
}
void update(int root,int r,int v)
{
    if(f[root].r==f[root].l&&f[root].r==r)
    {
        f[root].sum+=v;
        return ;
    }
    f[root].sum+=v;
    int mid=(f[root].l+f[root].r)>>1;
    if(r<=mid)
        update(root<<1, r, v);
    else
        update(root<<1|1, r, v);
}
int main ()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        int l,r;
        printf("Case %d:\n",++k);
        while(true)
        {
            scanf("%s",s);
            if(s[0]=='E')
                break;
            else
            {
                scanf("%d%d",&l,&r);
                if(s[0]=='Q')
                    printf("%d\n",query(1, l, r));
                else if(s[0]=='A')
                    update(1, l, r);
                else if(s[0]=='S')
                    update(1, l, -r);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值