HDU 1556 color the ball (tree array)

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1556


Problem descriptionn balloons are arranged in a row, numbered 1, 2, 3 .... n. given two integers a B (A <= B) each time, Lele colors each balloon one time from balloon a to balloon B, riding his "little pigeon" electric car. But after N times, Lele has forgotten how many times the I-th balloon has been painted. Can you help him figure out how many times each balloon has been painted?
Input the first behavior of each test instance is an integer N, (n <= 100000 ). next n rows, each row contains two integers, a B (1 <= A <= B <= N ).
When n = 0, the input ends.
Output each test instance outputs a row, which contains N integers. The number of I represents the total number of times that the I balloon is colored. Sample Input
31 12 23 331 11 21 30
Sample output
1 1 13 2 1

Solution:

Each node in the tree array represents a line segment. During each update, you can find all the segments previously included in B according to the features of the tree array, then add all the intervals before B to the number of dyeing times. Then, all the intervals before a are subtracted from the number of dyeing times. In this way, the number of dyeing times of [a, B] In the tree array is modified. When you query the total number of dyeing times of each vertex, you can directly calculate the value of each parent node, that is, the number of dyeing times of all intervals containing the vertex, which is in the tree array.Downward query and upward statisticsTypical applications

Tree edition:

# Include <cstdio> # include <cstring> # define maxn 100047 int C [maxn]; int N, T; int lowbit (int x) // 2 ^ K {return X & (-x);} void Update (int I, int X) // The I point increment is X {While (I> 0) {c [I] + = x; I-= lowbit (I) ;}} int sum (INT X) // sum of ranges [1, x] {int sum = 0; while (x <= N) {sum + = C [X]; x + = lowbit (x);} return sum;} int main () {int I, j, x, Y; while (scanf ("% d", & N) {memset (C, 0, sizeof (c); for (I = 1; I <= N; I ++) {scanf ("% d", & X, & Y); Update (Y, 1 ); // update the range below y (x-1,-1); // Replace the range below x-1} For (j = 1; j <N; j ++) {printf ("% d", sum (j) ;}printf ("% d \ n", sum (N) ;}return 0 ;}


Non-tree version: (in fact, the idea of using the tree version is also applied)

#include <cstdio>#include <cstring>int main(){int n, i, j, a[100047], x, y;while(scanf("%d",&n) && n){memset(a,0,sizeof(a));for(i = 1; i <= n; i++){scanf("%d%d",&x,&y);a[x]++,a[y+1]--;}int sum = a[1];printf("%d",a[1]);for(i = 2; i <= n; i++){sum+=a[i];printf(" %d",sum);}printf("\n");}return 0;}


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.