äºè¿å¶ç´¢å¼ï¼Fenwickï¼æ æ¯ä¸ç§æ°æ®ç»æï¼ä¸ºå®ç°å¨æ累积é¢ç表æä¾äºé«æçæ¹æ³ã
è¿ç§Fenwickæ æ°æ®ç»æ使ç¨äºè®¸å¤ä½æä½ææ¯ãå¨è¿ä¸ªå¯è§åä¸ï¼æ们å°ä½¿ç¨Fenwickæ è¿ä¸ªæ¯è¯æ¥æ代è¿ç§æ°æ®ç»æï¼é常缩å为'FT'ï¼ï¼å 为äºè¿å¶ç´¢å¼æ ç缩å'BIT'é常ä¸å¸¸è§çä½æä½ç¸å ³èã
å设æ们æä¸ä¸ªæ´æ°çå¤éés = {2,4,5,6,5,6,8,6,7,9,7}ï¼ä¸ä¸å®æåºï¼ãsä¸æn = 11个å ç´ ãå设æ们å°è¦ä½¿ç¨çæ大æ´æ°æ¯m = 10ï¼æ们ä»ä¸ä½¿ç¨æ´æ°0ãä¾å¦ï¼è¿äºæ´æ°ä»£è¡¨å¦çï¼æ´æ°ï¼çåæ°èå´å¨[1..10]ã注ænä¸mæ å ³ã
æ们å¯ä»¥éè¿ä¸ä¸ªç®åçO(n)æ¶é´å¾ªç¯ï¼åå¿è®¡æ°æåºç第ä¸æ¬¡è®¡æ°è¿ç¨ï¼ä»så建ä¸ä¸ªé¢ç表fãç¶åæ们å¯ä»¥ä½¿ç¨ç±»ä¼¼äºDP 1Dåç¼åçææ¯å¨O(m)æ¶é´å ä»é¢ç表få建累积é¢ç表cfï¼ä¾å¦ï¼å¨ä¸è¡¨ä¸ï¼cf[5] = cf[4]+f[5] = 2+2 = 4ï¼ç¶åcf[6] = cf[5]+f[6] = 4+3 = 7ã
ç´¢å¼/åæ°/ç¬¦å· | é¢ç f | 累积é¢ç cf |
---|---|---|
0 | - | - (忽ç¥ç´¢å¼0) |
1 | 0 | 0 |
2 | 1 | 1 |
3 | 0 | 1 |
4 | 1 | 2 |
5 | 2 | 4 == cf[4]+f[5] |
6 | 3 | 7 == cf[5]+f[6] |
7 | 2 | 9 |
8 | 1 | 10 |
9 | 1 | 11 |
10 == m | 0 | 11 == n |
æäºè¿æ ·ç累积é¢ç表cfï¼æ们å¯ä»¥æ§è¡èå´æ±åæ¥è¯¢ï¼rsq(i, j)ï¼ä»¥è¿åç´¢å¼iåjï¼å å«ï¼ä¹é´çé¢çä¹åï¼ä»¥é«æçO(1)æ¶é´ï¼å次使ç¨DP 1Dåç¼åï¼å³ï¼å å«-æé¤ååï¼ãä¾å¦ï¼rsq(5, 9) = rsq(1, 9) - rsq(1, 4) = 11-2 = 9ãç±äºè¿äºé®ï¼5ã6ã7ã8å9代表åæ°ï¼rsq(5, 9)æå³çå¨5å°9ä¹é´å¾åçå¦çæ»æ°ï¼9ï¼ã
ç´¢å¼/åæ°/ç¬¦å· | é¢ç f | 累积é¢ç cf |
---|---|---|
0 | - | - (忽ç¥ç´¢å¼0) |
1 | 0 | 0 |
2 | 1 | 1 |
3 | 0 | 1 |
4 | 1 | 2 == rsq(1, 4) |
5 | 2 | 4 |
6 | 3 | 7 |
7 | 2 | 9 |
8 | 1 | 10 |
9 | 1 | 11 == rsq(1, 9) |
10 == m | 0 | 11 == n |
å¨ææ°æ®ç»æéè¦å¨æ¥è¯¢ä¹é´æ¯æï¼é¢ç¹çï¼æ´æ°ãä¾å¦ï¼æ们å¯è½ä¼æ´æ°ï¼å¢å ï¼åæ°7çé¢çä»2 â 5ï¼ä¾å¦ï¼åæ3个å¦çå¾å7ï¼å¹¶æ´æ°ï¼åå°ï¼åæ°9çé¢çä»1 â 0ï¼ä¾å¦ï¼ä¹åå¾å9ç1个å¦ç被åç°æè¢ä½ä¸ï¼ç°å¨è¢«ç½ä¸º0ï¼å³ï¼ä»åæ°ä¸ç§»é¤ï¼ï¼ä»èæ´æ°è¡¨æ ¼ä¸ºï¼
ç´¢å¼/åæ°/ç¬¦å· | é¢ç f | 累积é¢ç cf |
---|---|---|
0 | - | - (忽ç¥ç´¢å¼0) |
1 | 0 | 0 |
2 | 1 | 1 |
3 | 0 | 1 |
4 | 1 | 2 |
5 | 2 | 4 |
6 | 3 | 7 |
7 | 2 â 5 | 9 â 12 |
8 | 1 | 10 â 13 |
9 | 1 â 0 | 11 â 13 |
10 == m | 0 | 11 â 13 == n |
ç¨çº¯æ°ç»åºç¡æ°æ®ç»ææ¥å®ç°è¿ä¸ªå¨æ累积é¢ç表å°éè¦O(m)çæ´æ°æä½ãæ们è½åå¾æ´å¥½åï¼
ä»ç»ï¼Fenwick æ æ°æ®ç»æã
å¨è¿ä¸ªå¯è§åä¸ï¼Fenwick æ æä¸ç§ä½¿ç¨æ¨¡å¼ã
第ä¸ç§æ¨¡å¼æ¯é»è®¤ç Fenwick æ ï¼å®å¯ä»¥å¨ O(log n) ä¸å¤çç¹æ´æ° (PU)åèå´æ¥è¯¢ (RQ)ï¼å ¶ä¸ n æ¯æ°æ®ç»æä¸æ大çï¼æ´æ°ï¼ç´¢å¼/é®ã请记ä½ï¼æ°æ®ç»æä¸å®é çé®æ°ç±å¦ä¸ä¸ªåé m 表示ãæ们å°è¿ç§é»è®¤ç±»åç®ç§°ä¸º PU RQï¼å³ç¹æ´æ°èå´æ¥è¯¢ã
è¿ç§å·§å¦çæ´æ°é®æåææ³æååºç°å¨Peter M. Fenwick ç 1994 年论æä¸ã
æ¨å¯ä»¥ç¹å»'å建'èåæ¥å建ä¸ä¸ªé¢çæ°ç»fï¼å ¶ä¸f[i]表示é®iå¨æ们åå§é®æ°ç»sä¸åºç°çé¢çã
éè¦æ示ï¼è¿ä¸ªé¢çæ°ç»fä¸æ¯åå§é®æ°ç»sãä¾å¦ï¼å¦ææ¨è¾å ¥{0,1,0,1,2,3,2,1,1,0}ï¼è¿æå³çæ¨æ£å¨å建0个ä¸ï¼1个äºï¼0个ä¸ï¼1个åï¼2个äºï¼...ï¼0个åï¼åºäº1çç´¢å¼ï¼ãå¨è¿ä¸ªä¾åä¸ï¼æ大çç´¢å¼/æ´æ°é®æ¯m = 10ï¼å°±åå¨ä¹åçå¹»ç¯çä¸ä¸æ ·ã
å¦ææ¨æåå§æ°ç»sï¼å å«n个å ç´ ï¼ä¾å¦ï¼ä»ä¹åçå¹»ç¯çä¸ç{2,4,5,6,5,6,8,6,7,9,7}ï¼sä¸éè¦æåºï¼ï¼æ¨å¯ä»¥è¿è¡ä¸æ¬¡O(n)çéåï¼å°s转æ¢ä¸ºn个索å¼/æ´æ°é®çé¢ç表fãï¼æ们å°å¨ä¸ä¹ çå°æ¥æä¾è¿ç§æ¿ä»£è¾å ¥æ¹æ³ï¼ã
æ¨å¯ä»¥ç¹å»'éæºå'æé®æ¥çæn个é®[1..n]çéæºé¢çã
ç¹å»'å¼å§'æ¥è¿ä»£è°ç¨n次update(i, f[i])æä½ãï¼æ们å°å¨ä¸ä¹ çå°æ¥æä¾ä¸ä¸ªæ´å¿«çæ建FTåè½ï¼ã
尽管ä»æ¦å¿µä¸è®²ï¼è¿ä¸ªæ°æ®ç»ææ¯ä¸ä¸ªæ ï¼ä½å®å°è¢«å®ç°ä¸ºä¸ä¸ªå为ftçæ´æ°æ°ç»ï¼èå´ä»ç´¢å¼1å°ç´¢å¼nï¼æ们çºç²äºæ们çftæ°ç»çç´¢å¼0ï¼ãä¸é¢æ¾ç¤ºçFenwickæ çï¼é»è²è½®å»åç½è²å é¨ï¼é¡¶ç¹ä¸çå¼æ¯åå¨å¨åºäº1çFenwickæ ftæ°ç»ä¸çå¼ã
ç®åï¼è¿ä¸ªFenwickæ çè¾¹è¿æ²¡ææ¾ç¤ºåºæ¥ãæ æ两个çæ¬ï¼æ¥è¯¢æ åæ´æ°æ ã
åºé¨ï¼èè²å é¨ï¼é¡¶ç¹å çå¼æ¯é¢çæ°ç» f çå¼ã
å¨æ°ç»ftä¸ç´¢å¼iåå¨çå¼ï¼Fenwickæ ä¸ç顶ç¹iï¼ï¼å³ft[i]ï¼æ¯é®èå´[i-LSOne(i)+1 .. i]ç累积é¢çãå¨è§è§ä¸ï¼è¿ä¸ªèå´ç±Fenwickæ ï¼æ¥è¯¢/æ¥è¯¢çæ¬ï¼çè¾¹ç¼æ¾ç¤ºã
å ³äºLSOne(i) = (i) & -(i)æä½çå¤ä¹ ï¼è¯·åè§æ们çä½æ©ç å¯è§å页é¢ã
ft[4] = 2 åå¨äºé®å¨ [4-LSOne(4)+1 .. 4] ä¸ç累积é¢çã
(沿çä»ç´¢å¼4åä¸åå°ç´¢å¼0çè¾¹ç¼ï¼åå ä¸1个索å¼)ã
è¿æ¯ [4-4+1 .. 4] = [1 .. 4] å f[1]+f[2]+f[3]+f[4] = 0+1+0+1 = 2ã
ft[6] = 5 åå¨äºé®å¨ [6-LSOne(6)+1 .. 6] ä¸ç累积é¢çã
(沿çä»ç´¢å¼6åä¸åå°ç´¢å¼4çè¾¹ï¼åå 1个索å¼)ã
è¿æ¯ [6-2+1 .. 6] = [5 .. 6] å f[5]+f[6] = 2+3 = 5ã
å½æ° rsq(j) è¿åä»ç¬¬ä¸ä¸ªç´¢å¼ 1ï¼å¿½ç¥ç´¢å¼ 0ï¼å°ç´¢å¼ j ç累积é¢çã
è¿ä¸ªå¼æ¯åå¨å¨æ°ç» ft ä¸ä¸ j ç¸å ³çåé¢çä¹åï¼å ³ç³»å ¬å¼ä¸º j' = j-LSOne(j)ãè¿ç§å ³ç³»å½¢æäºä¸ä¸ªFenwickæ ï¼å ·ä½æ¥è¯´ï¼æ¯Fenwickæ ç'询é®æ 'ã
æ们å°è¿ä¸ªå ¬å¼åå¤åºç¨ï¼ç´å° j 为0ã
æ 论 n çå¼å¦ä½ï¼è¿ä¸ªå½æ°çè¿è¡æ¶é´é½æ¯ O(log m)ã讨论ï¼ä¸ºä»ä¹ï¼
æ们ä¹åå·²ç»çå° ft[6] = rsq(5, 6) å ft[4] = rsq(1, 4)ã
å æ¤ï¼rsq(6) = ft[6] + ft[6-LSOne(6)] = ft[6] + ft[6-2] =
ft[6] + ft[4] = 5 + 2 = 7ã
é注ï¼4-LSOne(4) = 4-4 = 0ã
rsq(i, j) è¿åä»ç´¢å¼ i å° jï¼å å«ï¼ç累积é¢çã
å¦æ i = 1ï¼æ们å¯ä»¥åä¹åé£æ ·ä½¿ç¨ rsq(j)ã
å¦æ i > 1ï¼æ们åªéè¦è¿åï¼rsq(j) â rsq(iâ1)ï¼å
容-ææ¥åçï¼ã
讨论ï¼ä½ ç解åå åï¼
è¿ä¸ªå½æ°ä¹å¨ O(log m) ä¸è¿è¡ï¼æ 论 n å¦ä½ã讨论ï¼ä¸ºä»ä¹ï¼
rsq(4, 6) = rsq(6) â [ä¸ä¸å¼ å¹»ç¯ç...]ã
æ们ä¹åå·²ç»çå°rsq(6) = ft[6]+ft[4] = 5+2 = 7ã
rsq(4, 6) = rsq(6) â rsq(3)ã
æ们å¯ä»¥ç±»ä¼¼å°è®¡ç®åºrsq(3) = ft[3]+ft[2] = 0+1 = 1ã
è¦æ´æ°é®ï¼ç´¢å¼ï¼içé¢ç为vï¼vå¯ä»¥æ¯æ£æ°æè´æ°ï¼|v|ä¸ä¸å®ä¸ºä¸ï¼ï¼æ们使ç¨update(i, v)ã
ä¸iç¸å ³çç´¢å¼éè¿i' = i+LSOne(i)ä¸vç¸å ³ï¼å½i < ft.size()æ¶ï¼æ³¨æft.size()æ¯m+1ï¼æ们忽ç¥äºç´¢å¼0ï¼ãè¿äºå ³ç³»å½¢æäºä¸ç§å«å'æ´æ°æ 'çFenwickæ ç»æçåä½ã
讨论ï¼ä½ ç解è¿ä¸ªæä½ä»¥åæ们为ä»ä¹é¿å ç´¢å¼0åï¼
è¿ä¸ªå½æ°ä¹ä»¥O(log m)è¿è¡ï¼æ 论nå¦ä½ã讨论ï¼ä¸ºä»ä¹ï¼
第äºæ¨¡å¼
Fenwick æ ç第äºç§æ¨¡å¼æ¯å¯ä»¥å¤çèå´æ´æ° (RU)ï¼ä½åªè½ä»¥ O(log n) å¤çç¹æ¥è¯¢ (PQ)ã
æ们å°æ¤ç±»å缩å为RU PQã
å建æ°æ®å¹¶å°è¯å¨å ¶ä¸è¿è¡èå´æ´æ°æç¹æ¥è¯¢ç®æ³ã为æ¤ç±»åå建æ°æ®æå³çæå ¥å 个åºé´ãä¾å¦ï¼å¦ææ¨è¾å ¥[2,4],[3,5]ï¼è¿æå³çæ们æ£å¨éè¿+1æ´æ°èå´2å°4ï¼ç¶åéè¿+1æ´æ°èå´3å°5ï¼å æ¤æ们æ以ä¸é¢ç表ï¼0,1,2,2,1ï¼è¿æå³çæ1个0ï¼2个1ï¼3个2ï¼4个2ï¼5个1ã
顶é¨ç顶ç¹æ¾ç¤ºäºåå¨å¨Fenwickæ ä¸çå¼ï¼ftæ°ç»ï¼ã
åºé¨ç顶ç¹æ¾ç¤ºäºæ°æ®çå¼ï¼é¢ç表fï¼ã
注æå°è¿ç§RU PQç±»åä¸ä½¿ç¨çFenwickæ çå·§å¦ä¿®æ¹ï¼æ们å°èå´çå¼å§å¢å +1ï¼ä½å°èå´ç»æåçä¸ä¸ªç´¢å¼åå°-1以达å°è¿ä¸ªç»æã
æ们æ£å¨æ¢ç´¢æ¨¡å¼ä¸ã
Fenwick æ ç第ä¸ç§æ¨¡å¼æ¯å¯ä»¥åæ¶å¤çèå´æ´æ° (RU)åèå´æ¥è¯¢ (RQ)ç模å¼ï¼å ¶æ¶é´å¤æ度为 O(log n)ï¼ä½¿å¾è¿ç§ç±»åä¸å ·æææ°æ´æ°ç线段æ ç¸å½ï¼åè ä¹å¯ä»¥å¨ O(log n) çæ¶é´å¤æ度å è¿è¡ RU RQã
å建æ°æ®å¹¶å°è¯å¨å ¶ä¸è¿è¡èå´æ´æ°æèå´æ¥è¯¢ç®æ³ã
å建æ°æ®æ¯æå ¥å 个é´éï¼ç±»ä¼¼äº RU PQ çæ¬ãä½æ¯è¿æ¬¡ï¼ä½ ä¹å¯ä»¥é«æå°è¿è¡èå´æ¥è¯¢ã
å¨èå´æ´æ°èå´æ¥è¯¢ Fenwick æ ä¸ï¼æ们éè¦æ两个 Fenwick æ ã顶é¨ç顶ç¹æ¾ç¤ºç¬¬ä¸ä¸ª Fenwick æ çå¼ï¼BIT1[] æ°ç»ï¼ï¼ä¸é´ç顶ç¹æ¾ç¤ºç¬¬äºä¸ª Fenwick æ çå¼ï¼BIT2[] æ°ç»ï¼ï¼èåºé¨ç顶ç¹æ¾ç¤ºæ°æ®çå¼ï¼é¢ç表ï¼ã第ä¸ä¸ª Fenwick æ çè¡ä¸ºä¸ RU PQ çæ¬ç¸åã第äºä¸ª Fenwick æ ç¨äºè¿è¡å·§å¦çå移ï¼ä»¥å次å 许èå´æ¥è¯¢ã
æ们æä¸äºæ¶åæ¤æ°æ®ç»æçé¢å¤å 容ã
éæ¾çæ¯ï¼æªè³2020å¹´ï¼è¿ç§æ°æ®ç»æå¨C++ STLãJava APIãPythonæOCamlæ ååºä¸å°æªæä¾ãå æ¤ï¼æä»¬å¿ é¡»ç¼åæ们èªå·±çå®ç°ã
请æ¥ç以ä¸ä»¥é¢å对象ç¼ç¨ï¼OOPï¼æ¹å¼å®ç°çFenwick Treeæ°æ®ç»æçC++/Python/Java/OCamlå®ç°ï¼
fenwicktree_ds.cpp | py | java | ml
å次强è°ï¼æ¨å¯ä»¥èªç±å®å¶è¿ä¸ªèªå®ä¹åºå®ç°ä»¥éåºæ¨çéæ±ã