Home Database Mysql Tutorial 树状数组整理(2.区间修改、二维)

树状数组整理(2.区间修改、二维)

Jun 07, 2016 pm 03:10 PM
number Revise interval array tidy

1.区间整体加一个数,单点求: 已经很常用的方法了,就当成有多少线段覆盖,对a[l,r]k的操作转化为对辅助数组b[l]k,b[r1]-k,树状数组维护b[i]前缀和就好…… 具体来说,是对a[i]差分后生成新数组b[i],使得b[i]=a[i]-a[i-1],这样成段修改时: 对il或ir1,a

1.区间整体加一个数,单点求值:

已经很常用的方法了,就当成有多少线段覆盖,对a[l,r]+k的操作转化为对辅助数组b[l]+k,b[r+1]-k,树状数组维护b[i]前缀和就好……
具体来说,是对a[i]差分后生成新数组b[i],使得b[i]=a[i]-a[i-1],这样成段修改时:
    对ir+1,a[i]值不变故b[i]不变;l     但b[l]'=(a[l]+k)-a[l-1]=b[l]+k;b[r+1]'=a[r+1]-(a[r]+k)=b[r+1]-k。
同时对b[i]求前缀和会发现:
    sum(p)=b[1]+b[2]+...+b[p]=(a[1]-a[0])+(a[2]-a[1])+...+(b[p]-b[p-1])=a[p]-a[0]=a[p]
这样单点求值的方式也出来了,上代码(套用了下原始的BIT):

struct BIT_ex {
  BIT t; void init(int s) {t.init(s);}
  void change(int l, int r, _int k) {t.change(l,k); t.change(r+1,-k);}
  _int get(int p) {return t.sum(p);}
};
Copy after login

 

2.区间整体加一个数,求区间和(前缀和):

好像不是很常见,普及推广一下……
区间整体的修改已经搞出来,肯定是要继续用结论了……
上面差分数组得出了sum(p)=a[p],这里求前缀和当然是求a[0]+a[1]+...+a[p]了~剩下的全是算数
a[0]+a[1]+a[2]+a[3]+...+a[p]=0+sum(1)+sum(2)+sum(3)+...+a[p]=(b[1])+(b[1]+b[2])+(b[1]+b[2]+b[3])
+...+(b[1]+...+b[p])
b[1]在sum(1..p)中都出现,共p次,b[2]在sum(2..p)出现(p-1)次,类推可得
原式=p*b[1]+(p-1)*b[2]+(p-2)*b[3]+...+1*b[p]
本来想把每一项当成一个整体用BIT搞,但发现对于不同的p值,每项也会跟着变,显然没办法……
这里看到前面系数和b[]的下标和都是(p+1),考虑逆用倒序相加大法:
原式=(p+1)*(b[1]+b[2]+b[3]+...+b[p])-(1*b[1]+2*b[2]+3*b[3]+...+p*b[p])
前面括号里是直接对b[i]求的前缀和,后面括号是对i*b[i]求前缀和——系数和下标一致的项出来了!
好,这样我们可以搞两个BIT,一个是维护b[i]的前缀和,一个维护i*b[i]的前缀和,维护方法同上
为了减少依赖所以这里仍然套了原始BIT,套BIT_ex会更好写,上代码:

struct BIT_im {
  BIT t1; BIT t2; void init(int s) {t1.init(s); t2.init(s);}
  void chage(_int l, _int r, _int k) {
    t1.change(l,k); t1.change(r+1,-k);
    t2.change(l,l*k); t2.change(r+1,-(r+1)*k);
  }
  _int sum(_int p) {return (p+1)*t1.sum(p)-t2.sum(p);}
};
Copy after login


 

3.二维(多维)树状数组:

一维是前缀和,sum(p)=sum{a[i],i 二维的话,sum(x,y)=sum{a[i][j],i 多维类似,只讨论二维
静态维护很简单:

for (int i=1; i
<p><span>这样如果求(x1,y1)-(x2,y2)构成的矩形和,ans=s[x2][y2]-s[x1-1][y2]-s[x2,y1-1]+s[x1-1][y1-1]<br>
那么动态维护呢?首先对(a[i])[]这个数组可以用BIT来维护一个前缀和,再维护维护(a[i])[]前缀和BIT的前缀和……好绕<br>
总之呢,可以先用一组BIT可以维护多条平行线上的和,再用一个和它们正交的BIT把它们挂进去维护,此时原来那组BIT的意义其实已经变了……<br>
这个思路比较像下面这段代码(鸣谢mlzmlz95):</span></p>
<pre class="brush:php;toolbar:false">for(i=1;i
<p><span>那么上代码:</span></p>
<pre class="brush:php;toolbar:false">struct BIT_2D {
  BIT c[N]; int n;
  void init(int s1,int s2) {n=s1; while (s1) c[s1--].init(s2);}
  void change(int x,int y,int k) {for (; x
<p><span>这个实现中,我们通过外层BIT来确定里层BIT的更新,初始化要把它们循环一遍设定好尺寸<br>
至于3D,那就再挂个2D吧,不过求一个长方体和的公式有点复杂>_
那么想搞多维难道就要把前面所有维的写出来吗……太麻烦了,我们直接手工inline一下,再稍作修改:</span></p>
<pre class="brush:php;toolbar:false">struct BIT_2D_in {
  int c[N][M],n,m;
  void init(int s1,int s2) {n=s1; m=s2; memset(c,0,sizeof(c));}
  void change(int xx,int yy,int k) {
    for (int x=xx; x
<p><span>这个实现中,我们可以看出来这两重循环直接控制好了下标,那么多维直接加几重循环就完事了,xx,yy当参数名可以在后面循环写x,y,我懒……</span></p>
<p><br>
<span>没了……其实全文可能没啥新鲜的<br>
下期预告:邪道(写萎)的BIT<br>
</span></p>


Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk How to change the personal name in the group on DingTalk_How to modify the personal name in the group on DingTalk Mar 29, 2024 pm 08:41 PM

1. First open DingTalk. 2. Open the group chat and click the three dots in the upper right corner. 3. Find my nickname in this group. 4. Click to enter to modify and save.

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

The method of using a foreach loop to remove duplicate elements from a PHP array is as follows: traverse the array, and if the element already exists and the current position is not the first occurrence, delete it. For example, if there are duplicate records in the database query results, you can use this method to remove them and obtain results without duplicate records.

How to modify the address location of published products on Xianyu How to modify the address location of published products on Xianyu Mar 28, 2024 pm 03:36 PM

When publishing products on the Xianyu platform, users can customize the geographical location information of the product according to the actual situation, so that potential buyers can more accurately grasp the specific location of the product. Once the product is successfully put on the shelves, there is no need to worry if the seller's location changes. The Xianyu platform provides a flexible and convenient modification function. So when we want to modify the address of a published product, how do we modify it? This tutorial guide will provide you with a detailed step-by-step guide. I hope it can help. Everyone! How to modify the release product address in Xianyu? 1. Open Xianyu, click on what I published, select the product, and click Edit. 2. Click the positioning icon and select the address you want to set.

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

The performance comparison of PHP array key value flipping methods shows that the array_flip() function performs better than the for loop in large arrays (more than 1 million elements) and takes less time. The for loop method of manually flipping key values ​​takes a relatively long time.

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

Methods for deep copying arrays in PHP include: JSON encoding and decoding using json_decode and json_encode. Use array_map and clone to make deep copies of keys and values. Use serialize and unserialize for serialization and deserialization.

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

Multidimensional array sorting can be divided into single column sorting and nested sorting. Single column sorting can use the array_multisort() function to sort by columns; nested sorting requires a recursive function to traverse the array and sort it. Practical cases include sorting by product name and compound sorting by sales volume and price.

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

The best practice for performing an array deep copy in PHP is to use json_decode(json_encode($arr)) to convert the array to a JSON string and then convert it back to an array. Use unserialize(serialize($arr)) to serialize the array to a string and then deserialize it to a new array. Use the RecursiveIteratorIterator to recursively traverse multidimensional arrays.

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

PHP's array_group_by function can group elements in an array based on keys or closure functions, returning an associative array where the key is the group name and the value is an array of elements belonging to the group.

See all articles