zoukankan      html  css  js  c++  java
  • 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:
    ",++k);
            while(true)
            {
                scanf("%s",s);
                if(s[0]=='Q')
                {
                    scanf("%d%d",&l,&r);
                    printf("%d
    ",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:
    ",++k);
            while(true)
            {
                scanf("%s",s);
                if(s[0]=='E')
                    break;
                else
                {
                    scanf("%d%d",&l,&r);
                    if(s[0]=='Q')
                        printf("%d
    ",query(1, l, r));
                    else if(s[0]=='A')
                        update(1, l, r);
                    else if(s[0]=='S')
                        update(1, l, -r);
                }
            }
        }
        return 0;
    }
    想的太多,做的太少。
  • 相关阅读:
    《数据结构》C++代码 线性表
    《数据结构》C++代码 Splay
    《数据结构》C++代码 前言
    蓝桥杯- 算法提高 最大乘积
    HDU-1241 Oil Deposits
    一个简单的网站计数器
    编写一个jsp页面,输出九九乘法表。
    Sum It Up
    历届试题 剪格子
    历届试题 分糖果
  • 原文地址:https://www.cnblogs.com/pealicx/p/6115585.html
Copyright © 2011-2022 走看看