Tree-like array

Source: Internet
Author: User

Tree-like array

A detailed introduction to the tree-like array is visible Rujia "Algorithmic Competition Primer: Training Guide"

For an n-element array a[n], do the following:

Add (I, D): let A[i] become a[i]+d.

Query (L, R): Returns A[L]+A[L+1]+...+A[R].

Note: The tree array can only calculate a[1] start and , a[0] This element is not available. The complexity of the above operations is O (LOGN).

In fact, the tree-like array can also handle the interval update, single-point query problems . such as Hdu 1556 Color the ball, but this kind of problem is still more intuitive with the line tree.

Here's the code:

const int maxn=10000+5;//maximum number of elements int n;//element int c[maxn];//c[i]==a[i]+a[i-1]+...+a[i-lowbit (i) +1]//returns the value of the binary rightmost 1 of i int Lowbit (int i) {    return i& (-i);} Return a[1]+ ... A[i] and int sum (int i) {    int res=0;    while (i>0)    {        res + = C[i];        I-= lowbit (i);    }    return res;} Order A[i] + = valvoid Add (int i,int val) {    while (i<=n)    {        C[i] + = val;        i + = Lowbit (i);    }}

Notice that the first C array is initially all 0, and then each read into a number of a[i] executes one step add (I, A[i]) for true initialization.

and the array processed by the tree array A[n] is starting from 1, or a[0] is an unused element.

Tree-like arrays: basic applications

HDU 1166 enemy soldiers (Simple Tree Array): basic applications. Problem Solving Report!

tree-like array: Tagged array , single-point update

UVA 1428 Pingpong (tree-like array): basic questions, but you need to change your mind. Problem Solving Report!

POJ 2299ultra-quicksort (tree array + discretization or merge sort for reverse order): discretization data is required and then solved with a tree array, but the idea of merging sorting is more convenient. Problem Solving Report!

POJ 2352hdu1541 Stars (tree-like array): basic types. Problem Solving Report!

POJ 2481 Cows (tree array): Basic types, but note that when two nodes are identical, but we can not count the same node in the number of required nodes inside what to do? Problem Solving Report!

POJ 3067Japan (tree-like array): After the order is converted to the number of reverse sequence. Problem Solving Report!

HDU 1394Minimum Inversion Number (tree-like array): Still in reverse order. Problem Solving Report!

POJ 3378 crazythairs (Data set +dp+ tree array + high precision): Comprehensive application But DP is not difficult, the data set is not difficult, the tree array is not difficult, high precision is not difficult. Problem Solving Report!

HDU 3450Counting sequences (tree-like array +dp+ discretization): Comprehensive problem, step by step disassembly. Problem Solving Report!

HDU 3743 Froshweek (tree-like array or merge sort for reverse order): Another problem to reverse. Problem Solving Report!

HDU 2838 cowsorting (tree-like array): very ingenious thinking. Problem Solving Report!

POJ 2182 lostcows (tree array, Brute force solution): A better understanding of the brute force solution. Problem Solving Report!

POJ 1990MooFest (tree array + off-line processing): Some conversion thinking is needed. Problem Solving Report!

Tree-like array: Interval update, single point query

HDU 1556 colorthe Ball (tree-shaped array): Interval update. Problem Solving Report!

Tree array: Single point update, delete element

HDU 3874Necklace (tree array + offline processing): The element needs to be cleverly removed. Problem Solving Report!

Tree-like array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.