HDU-1556 Color the ball "Difference score Group"

Source: Internet
Author: User

problem Description
N Balloons are lined up in a row, numbered from left to right in 1,2,3....N. Given 2 integers a B (a <= B) each time, Lele to ride his "Little Pigeon" brand electric car from balloon A to balloon B in turn to each balloon painted color. But n times after Lele has forgotten the first balloon has been painted several times, can you help him to figure out how many times each balloon has been painted color?
Input
The
first behavior of each test instance is an integer n, (n <= 100000). The next n rows, each line consists of 2 integers a B (1 <= a <= b <= N).
When n = 0, the input ends.
Output
each test instance outputs one row, including n integers, and number I indicates the number of times the balloon was painted in total.
Sample Input
From
331
Sample Output
1 1 1 3 2 1
The following:interval Modification query problems generally think of using line tree or tree-like array to do, but found that the characteristics of this problem is multiple changes, and finally only one query, is offline query, so you can use the difference score Group;Difference Score Group: for array a[i], we make d[i]=a[i]-a[i-1] (d[1]=a[1]), then D[i] is a difference fraction group, we find the statistics D array prefix and sum array, then there isSum[i]=d[1]+d[2]+d[3]+...+d[i]=a[1]+a[2]-a[1]+a[3]-a[2]+...+a[i]-a[i-1]=a[i], i.e. prefixes and sum[i]=a[i];Therefore, each time you add or subtract x in the interval [l,r] only need to make d[l]+x,d[r+1]-x, you can guarantee [L,r] increased x, and [1,l-1] and [r+1,n] have no effect. The degree of complexity is O (n). (The code to reflect the idea, the question A and sum array can be omitted)
Code:
1#include <bits/stdc++.h>2 using namespacestd;3 #defineINF 0x3f3f3f3f4 #defineM (A, B) memset (A, B, sizeof (a))5 Const intN =100000+Ten;6 intA[n], d[n], sum[n];7 8 intMain () {9     intN;Ten      while(SCANF ("%d", &N), N) { OneM (A,0); Ad[1] = a[1]; -          for(inti =2; I <= N; ++i) D[i] = a[i]-a[i-1]; -         intL, R; the          for(inti =1; I <= N; ++i) { -scanf"%d%d", &l, &R); -D[l] + =1; -d[r+1] -=1; +         } -M (SUM,0); +          for(inti =1; I <= N; ++i) Sum[i] = sum[i-1] +D[i]; A          for(inti =1; I < n; ++i) printf ("%d", Sum[i]); atprintf"%d\n", Sum[n]); -     } -  -     return 0; -}

HDU-1556 Color the ball "Difference score Group"

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.