zoukankan      html  css  js  c++  java
  • 树状树组区间修改,单点修改模板

    单点修改

    #include <iostream>
    #include <cstring>
    #include <stack>
    #include <cstdio>
    #include <cmath>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <set>
    #include <map>
    using namespace std;
    #define ll long long
    const int  N=500005;
    int a,c[N],n,m=0;
    
    int sum(int x)
    {
        int ans=0;
        for(;x;x-=x&-x)
        {
            ans=ans+c[x];
        }
        return ans;
    }
    void add(int x,int y)
    {
        for(;x<=N-1;x+=x&-x)
            c[x]+=y; 
    }
    int main()
    {
        ll ans=0;
        int x,y,z;
        cin>>n>>m;
          for(int i=1;i<=n;i++)
          {
              scanf("%d",&a);
              add(i,a);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            if(x==1)
                add(y,z);
            else
                printf("%d
    ",sum(z)-sum(y-1));
        }
        return 0;
    }

    区间修改

    #include<bits/stdc++.h>
    #define ll long long
    #define lowbit(x) x&-x
    using namespace std;
    const int N=5e5+10;
      ll tree[N];
    int n,m;
    void add(int x,ll num)
    {
        while(x<=n)
        {
            tree[x]+=num;
            x+=lowbit(x);
        }
    }
    ll query(int x)
    {
        ll ans=0;
        while(x)
        {
            ans+=tree[x];
            x-=lowbit(x);
        }
        return ans;
    }
    
    
    int main()
    {
        scanf("%d%d",&n,&m);
         long long last=0,now;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&now);
            add(i,now-last);
            last=now;
         }
         int flag;
         while(m--)
         {
             int x,y;
             ll k;
             scanf("%d",&flag);
             if(flag==1)
             {
                 scanf("%d%d%lld",&x,&y,&k);
                 add(x,k);
                 add(y+1,-k);
             }
             else
             {
                 scanf("%d",&x);
                 printf("%lld
    ",query(x));
             }
             
          } 
        return 0;
    }
  • 相关阅读:
    oracel 复制A列的内容到列
    视图转为表
    面向对象
    银弹效应
    解决linux删除文件后空间没有释放问题
    HttpAnalyzerStdV7安装教程
    HttpUploader6.2-process版本
    Chrome浏览器控件安装方法
    Firefox浏览器控件安装方法
    通达OA整合教程
  • 原文地址:https://www.cnblogs.com/hh13579/p/11387398.html
Copyright © 2011-2022 走看看