Hi everyone! All that you can do it with BIT can be done with segment tree?
# | User | Rating |
---|---|---|
1 | tourist | 3993 |
2 | jiangly | 3743 |
3 | orzdevinwang | 3707 |
4 | Radewoosh | 3627 |
5 | jqdai0815 | 3620 |
6 | Benq | 3564 |
7 | Kevin114514 | 3443 |
8 | ksun48 | 3434 |
9 | Rewinding | 3397 |
10 | Um_nik | 3396 |
# | User | Contrib. |
---|---|---|
1 | cry | 167 |
2 | Um_nik | 163 |
3 | maomao90 | 162 |
3 | atcoder_official | 162 |
5 | adamant | 159 |
6 | -is-this-fft- | 158 |
7 | awoo | 156 |
8 | TheScrasse | 154 |
9 | Dominater069 | 153 |
9 | nor | 153 |
Hi everyone! All that you can do it with BIT can be done with segment tree?
Name |
---|
Yep
Sometimes you can only get AC with Fenwick only (due to constant factor).
I had never learnt Fenwick and never ever had the need to implement one because I would always say that Segment tree is better, this post made me learn Fenwick in order to prove a point (or disprove).
Problem to solve is given N numbers and 2 type of queries: type 0 means add r to lth number and type 1 means get sum from l to r inclusive.
I tried random cases and extreme cases (Q=N=10^5) and segment tree
In my computer I get:
1000 Extreme cases:
Total Segment tree: 14.434
Total Fenwick tree: 16.162
10000 random cases:
Total Segment tree: 21.677
Total Fenwick tree: 24.464
In code forces custom run I get (had to run less tests because it gets timed out):
300 Extreme cases:
Total Segment tree: 2.140
Total Fenwick tree: 1.423
2000 random cases:
Total Segment tree: 1.977
Total Fenwick tree: 1.446
Basically results are inconsistent. So, which one is better I guess it depends, either way, differences are minimal to the point where I/O would actually have more impact than the data structure.
My conclusion is Segment tree is better, simply because it can be easily modified to achieve other thing's which BIT doesn't seem to be able to.
In order to learn segment tree I would recommend to start with recursive (to understand it), and then iterative (how to optimize it).
My code: http://pastebin.com/fFRY5FQE
Note: I used the iterative implementation of segment tree, since obviously recursive is slower.
I am sorry if I mislead anyone . I have never learnt about the iterative segment tree ( I am too lazy for it (Pun!) ).
I was talking about recursive implementation of segment tree.
Sometimes you are forced to use BIT due to memory limit, esp when the queries are now in 2D.
I used to stick with segment tree but I have to use BIT for a 1000*1000 matrix query.
Really thank you!
Here you can find everything you need to know about BIT Vs Fenwick tree: https://discuss.codechef.com/questions/73815/segment-tree-vs-bit
Maybe BIT Vs Segment tree :)
Yes, haha! Wrote that in a hurry. :P
BIT faster than segment tree . this submit http://mirror.codeforces.com/contest/703/submission/19672070 got TLE when i used segment tree , but when i used BIT it got AC http://mirror.codeforces.com/contest/703/submission/19672254
I can't really think of anything than can be done with a BIT and can't be done with a Segment Tree. Although it may seem useless if you look at it this way, it is really handy when it can be used. BIT is easier and faster to code, and it uses a lot less memory. It also works slightly faster in most the cases I have tried both of them :)
Spoj — DQUERY
Spoj — RATING
May these two problems help you to know time , effort and runtime difference between both trees !
Well, I have also never seen a problem where you can use only BIT. As said in comments above Fenwick Tree is
very fast
as compared to segment tree. Also, it usesless memory
. But I still use segment tree most of the times.The final sentence contradicted to the other :p
Actually, I think BIT lets you do multi-dimensional structures much more easily than traditional segment tree implementations.
just like the old saying:"Segment Tree can do everything BIT can do and can't do." But sometimes it is very difficult to code.