• 线段树 一个单独的数加上或减去某个数字 最后一段求和


    #include<vector>
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<math.h>
    using namespace std;
    #define INF 0x3f3f3f3f
    #define N 60022
    #define lson rood<<1
    #define rson rood<<1|1
    int w[N];
    struct node
    {
        int l,r,v,mid;
    }a[N<<2];
    void q(int rood,int l,int r)
    {
        a[rood].l=l;a[rood].r=r;a[rood].mid=(r+l)/2;
        if(l==r)
        {
            a[rood].v=w[l];
            return ;
        }
        q(lson,l,a[rood].mid);
        q(rson,a[rood].mid+1,r);
        a[rood].v=a[lson].v+a[rson].v;
    }   
    void qq(int rood,int e,int f)
    {
        a[rood].v+=f;
        if(a[rood].r==a[rood].l) return ;
        if(a[rood].mid>=e) qq(lson,e,f);
        else qq(rson,e,f);
    }
    int Q(int rood,int l,int r)
    {
        if(a[rood].l==l&&a[rood].r==r)
            return a[rood].v;
        if(a[rood].mid>=r) return Q(lson,l,r);
        else if(a[rood].mid<l) return Q(rson,l,r);
        else
        {
            int x=Q(lson,l,a[rood].mid);
            int y=Q(rson,a[rood].mid+1,r);
            return x+y;
        }
    }
    
    int main()
    {
        int T,i,e,f,n,t=1;
        char str[30];
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d",&n);
            for(i=1;i<=n;i++)
                scanf("%d",&w[i]);
            q(1,1,n);
            printf("Case %d:
    ",t++);
            while(scanf("%s",str),strcmp(str,"End"))
            {
                scanf("%d%d",&e,&f);
                if(str[0]=='Q')
                {
                    printf("%d
    ",Q(1,e,f));
                    continue;
                }
                int ans=1;
                if(str[0]=='S')
                    ans*=-1;
                f*=ans;
                qq(1,e,f);
            }
        }
        return 0;
    }

    http://acm.hdu.edu.cn/showproblem.php?pid=1166

  • 相关阅读:
    Augular JS里的各种ng-
    JS笔记1
    JQuery实战学习--在dreamweaver 8中配置Jquery自动提示
    android 设置桌面背景图片适应屏幕大小
    canvas实现进度条!
    Javascript之Prototype
    Sql Server 之 for xml (path,raw,auto,root)
    MVC 知识点学习3(linq to sql)
    MVC 知识点学习2
    MVC 知识点学习1
  • 原文地址:https://www.cnblogs.com/a719525932/p/5667177.html
Copyright © 2020-2023  润新知