Pku3321 apple tree-tree Array

Source: Internet
Author: User

Description:

There are n nodes on the apple tree. Apple can only grow on the node. One node either has one or no apple. The structure of the tree is provided. One operation is to crop or grow the apple of a node, and the other is to ask how many apples are in the subtree with a node as the root.

Analysis:

The question is very distorted, but can be used.Tree Array.

We set the nodeRenameSo that the number of all child nodes is less than the father's day number, then when you ask how many apples the subtree has in total, it is to ask how many apples the node has smaller than the node number. That is, the tree array model.

Numbering rules: Number the node according to the subsequent Traversal method. The obtained number must be the largest Max in the subtree, and the minimum number min under the node is recorded. Then, after being converted to a tree array model, when querying node v, sum (V. max)-sum (V. min-1) is answered ).

Of course, this question can also be numbered and then usedLine Segment tree.

 

  1. /*
  2. Pku3321 apple tree
  3. */
  4. # Include <stdio. h>
  5. # Include <memory. h>
  6. # Include <malloc. h>
  7. # Define CLR (a) memset (A, 0, sizeof ())
  8. # Define n 100005
  9. Typedef struct linknode {
  10. Int ID;
  11. Struct linknode * next;
  12. } Link;
  13. Typedef struct {
  14. Int index;
  15. Int min;
  16. } Index;
  17. // Vars
  18. Link mem [N * 2];
  19. Int pmem;
  20. Int n, m;
  21. Link * A [n];
  22. Int e [N];
  23. Index idx [N];
  24. Int nidx;
  25. Int B [N];
  26. Int C [N];
  27. Void addnode (int I, Int J ){
  28. Link * P = & mem [pmem ++];
  29. P-> id = J;
  30. P-> next = A [I];
  31. A [I] = P;
  32. }
  33. Void traversal (INT v ){
  34. Link * P;
  35. E [v] = 1;
  36. Idx [v]. min = nidx;
  37. P = A [v];
  38. While (P! = NULL ){
  39. If (! E [p-> id]) {
  40. Traversal (p-> ID );
  41. }
  42. P = p-> next;
  43. }
  44. Idx [v]. Index = nidx ++;
  45. }
  46. Int lowbit (int x ){
  47. Return-X & X;
  48. }
  49. Void add (int I, int t ){
  50. While (I <= N ){
  51. C [I] + = T;
  52. I + = lowbit (I );
  53. }
  54. }
  55. Int sum (int I ){
  56. Int S = 0;
  57. While (I> 0 ){
  58. S + = C [I];
  59. I-= lowbit (I );
  60. }
  61. Return S;
  62. }
  63. Int main ()
  64. {
  65. While (scanf ("% d", & N )! = EOF ){
  66. Int I, J, K;
  67. // Input
  68. CLR (a); pmem = 0;
  69. For (k = 0; k <n-1; k ++ ){
  70. Scanf ("% d", & I, & J );
  71. Addnode (I, j );
  72. Addnode (J, I );
  73. }
  74. // Search Tree
  75. CLR (E );
  76. Nidx = 1;
  77. Traversal (1 );
  78. // Init tree
  79. For (k = 1; k <= N; k ++ ){
  80. B [k] = 1;
  81. Add (k, 1 );
  82. }
  83. // Work
  84. Char ch [5];
  85. Int sum;
  86. Scanf ("% d", & M );
  87. While (M --){
  88. Scanf ("% S % d", CH, & K );
  89. I = idx [K]. index;
  90. J = idx [K]. min;
  91. If (CH [0] = 'C') {// change
  92. B [I] =! B [I];
  93. Add (I, B [I]? 1:-1 );
  94. }
  95. If (CH [0] = 'q') {// inquiry
  96. Sum = sum (I)-sum (J-1 );
  97. Printf ("% d/N", sum );
  98. }
  99. }
  100. }
  101. Return 0;
  102. }

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.