HDU 1166 enemy deployment (tree array entry)

Source: Internet
Author: User

Tree array introduction:

The requirements for query and modification are similar. The complexity of logn can be achieved by using a tree array.

The array c Represented by the red rectangle is a tree array. here, C [I] indicates the sum of a [I-2 ^ k + 1] to a [I], and K indicates the number of 0 at the end of I in binary, or the smallest index when I is expressed by the power of 2.

The so-called K is also the height of the node in the tree.

Modify the I-th element. To maintain the meaning of array C, you need to modify all the ancestors of C [I] and C [I, instead of the node of the C [I] ancestor, modifications to the I-th element will not change. The ancestor has a total of "tree height-C [I] node height"

The difference between the [p, q] elements and can be calculated as [1, q], [1, p. Then the question is how to query the elements and values of a range [1, p], that is, evaluate s [p]. For the sum of the First N values of a series, you only need to find all the largest Subtrees before N and add the C values of the root nodes.
The key to implementing a tree array is to calculate the number K at the end of a binary number P (the smallest index when expressed by the power of 2 ). 2 ^ K is the distance between the pointer sliding when it is modified (and Statistics). We define this value as the lowbit of P.
More specifically, the lowbit of the positive integer p is the result of extracting the last 1 in the binary p.
For example, the lowbit of 23 (10111) is 1 (00001), and The lowbit of 20 (10100) is 4 (00100 ).

Lowbit (p) = P & (P ^ (p-1 ))

According to the signed integer complement rules, we can find that (P ^ (p-1) is exactly equal to-P, that is, the lowbit formula can be more concise:
Lowbit (p) = P &-P

Void plus (int x, int num) {While (x <= N) {C [x] + = num; x + = lowbit (x );}} int sum (int x) {int S = 0; while (x) {S + = C [X]; X-= lowbit (x);} return s ;} // enemy deployment # include <cstdio> # include <algorithm> # include <queue> # include <stack> using namespace STD; int t; int N; int A [500010]; int lowbit (int x) {return X & (-x);} void Update (int x, int num) {While (x <= N) {A [x] = A [x] + num; X = x + lowbit (x) ;}} int query (int x) {int ans = 0; while (x> 0) {ans + = A [X]; X-= lowbit (x);} return ans;} int main () {// freopen ("D: \ out.txt "," W ", stdout); int I, j, k; char STR [100], TMP [10]; scanf (" % d ", & T); k = 1; while (t --) {scanf ("% d", & N); memset (A, 0, sizeof ()); for (I = 1; I <= N; I ++) {scanf ("% d", & J); Update (I, j );} printf ("case % d: \ n", K ++); While (1) {scanf ("% s", STR ); if (STR [0] = 'E') break; scanf ("% d", & I, & J ); if (STR [0] = 'A') Update (I, j); If (STR [0] = 's') Update (I,-j ); // you can use the-number... if (STR [0] = 'q') printf ("% d \ n", query (j)-query (I-1); // note that this is a I-1, because tree arrays are closed intervals} return 0 ;}



HDU 1166 enemy deployment (tree array entry)

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.