zoukankan      html  css  js  c++  java
  • ExFenwickTree

    ExFenwickTree

    这是可支持区间加区间求和的树状数组

    首先设(delta[i])([i,n])的共同增量.

    所以将([l,r])共同加上(x)就相当于(delta[l]+=x,delta[r+1]-=x)

    然后求(pre[i]),([1,i])的和

    显然(pre[i])等于(sum_{j=1}^{i}delta[j] imes (i-j+1))

    拆开来得到((i+1) imes sum_{j=1}^i delta[j]-sum_{j=1}^i delta[j] imes j)

    所以维护两个前缀就好啦.

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    const int MAXN=100000;
    const int MAXM=100000;
    int n,C1[MAXN+10],C2[MAXN+10];
    int lowbit(const int x){return x&(-x);}
    void update(int x,int w)
    {
    	int now=x;
    	while(now<=n)
    	{
    		C1[now]+=w;
    		C2[now]+=w*x;
    		now+=lowbit(now);
    	}
    }
    void update(int l,int r,int w){update(l,w);update(r+1,-w);}
    int query(int x)
    {
    	int now=x,res=0;
    	while(now)
    	{
    		res+=C1[now]*(x+1)-C2[now];
    		now-=lowbit(now);
    	}
    	return res;
    }
    
  • 相关阅读:
    盲山有感
    一个用Regex的完成sql语句中字段替换的demo
    月夜奔跑
    乱弹
    《勇敢抉择》摘录一
    梦想高歌
    从今天起
    php性能优化
    unity3d shader中RenderType的所有类型
    Unity打包ipa图文教程
  • 原文地址:https://www.cnblogs.com/DOlaBMOon/p/7587772.html
Copyright © 2011-2022 走看看