Tree array algorithm analysis

Source: Internet
Author: User
/*************************************** **************************************** * ***** Tree arrays include one-dimensional tree arrays and two-dimensional tree arrays; the main problem model is the known array a [n]. The subscript starts from 1 and changes the elements in array a. The sum between I and j in array a must be obtained; in the tree array, S [k] stores the sum of the number elements represented by the first 1 on the right of the binary representation of the forward k starting from k; that is, if lowbit is k, it indicates the number represented by the first 1 on the right. What is stored in S [k] is the sum of lowbit elements starting from a [k; s1 = A1S2 = A1 + A2S3 = A3S4 = A1 + A2 + A3 + A4S5 = A5S6 = A5 + A6S7 = A7S8 = A1 + A2 + A3 + A4 + A5 + A6 + A7 + a8 time complexity analysis: modify element: for modifying an element, If the I-th element is modified, you can directly change it in the S array. For example, the modified element is a [2]. so it affects only S [2], S [4], and S [8] elements in the c array. You only need to modify them one by one, the worst complexity of this process is less than O (logN); query element: for search, such as search sum [k], you only need to search for the number of 1 in the Binary Expression of k to obtain the final result. For example, you can search for sum [7]. The Binary Expression of 7 contains 3 1, that is, you need to search for 3 times, that is, sum [7] = S [7] + S [6] + S [4]; implementation process: Taking 7 as an example, the binary value is 0111; starting from the first 1 on the right; when the first 1 on the right exists, it is 7 and the binary value is 0111, that is, S [7]. Then, remove this 1 and get 6. the binary value is 0110, that is, S [6]. then, discard the used 1 and get 4. The binary value is 0100, that is, S [4]. that is, sum [7] = S [7] + S [6] + S [4]; **************************************** *** **************************************** */# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> # include <algorithm> using namespace std; const int MAX = 50000; class Binary_Indexed_Tree {public: int *** s; int type; public: Binary_Indexed_Tree (int t );~ Binary_Indexed_Tree (); void clear (); int lowbit (int x) {return x & (-x) ;}; void modify (int x, int value ); void modify (int x, int y, int value); int sum (int x, int y) ;}; Binary_Indexed_Tree: Binary_Indexed_Tree (int t): type (t) {int I; s = new int * [MAX + 1]; // One-dimensional tree array if (type = 1) {for (I = 0; I <= MAX; I ++) s [I] = new int;} // two-dimensional tree array else if (type = 2) {for (I = 0; I <= MAX; I ++) s [I] = New int [MAX + 1] ;}} Binary_Indexed_Tree ::~ Binary_Indexed_Tree () {int I; for (I = 0; I <= MAX; I ++) delete [] s [I]; delete [] s;} void Binary_Indexed_Tree :: clear () {int I, j; for (I = 0; I <= MAX; I ++) {if (type = 1) s [I] [0] = 0; else {for (j = 0; j <= MAX; j ++) s [I] [j] = 0 ;}}} void Binary_Indexed_Tree: modify (int x, int value) {while (x <= MAX) {s [x] [0] + = value; x + = lowbit (x) ;}} void Binary_Indexed_Tree: modify (int x, int y, int value) {int t Emp = y; while (x <= MAX) {y = temp; while (y <= MAX) {s [x] [y] + = value; y = y + lowbit (y);} x = x + lowbit (x);} int Binary_Indexed_Tree: sum (int x) {int ans = 0; while (x> 0) {ans + = s [x] [0]; x-= lowbit (x);} return ans;} int Binary_Indexed_Tree: sum (int x, int y) {int ans = 0, temp = y; while (x> 0) {y = temp; while (y> 0) {ans + = s [x] [y]; y = y-lowbit (y);} x = x-lowbit (x);} return ans ;}/* **************************************** * ************ Appendix: HDU1541http: // acm.hdu.edu.cn/showproblem.php? Pid = 1541 question: how many stars are there in the lower left of a star ****************************** * *********************/int com [MAX], res [MAX], N; int lowbit (int x) {return x & (-x);} void modify (int pos, int val) {while (pos <= MAX) {com [pos] + = val; pos + = lowbit (pos) ;}} int quy (int x) {int sum = 0; while (x> 0) {sum + = com [x]; x-= lowbit (x);} return sum;} int main () {int x; while (scanf ("% d ", & N )! = EOF) {memset (com, 0, sizeof (com); memset (res, 0, sizeof (res); for (int I = 1; I <= N; ++ I) {scanf ("% d % * d", & x); x ++; res [quy (x)] ++; modify (x, 1) ;}for (int I = 0; I <N; ++ I) printf ("% d \ n", res [I]) ;}return 0 ;}

 

Related Article

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.