Color the ball (segment tree)

Source: Internet
Author: User

Time limit:9000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11355 Accepted Submission (s): 5655


Problem descriptionn a row of balloons, numbered from left to right to 1,2,3....N. Each time a given 2 integers a B (a <= B), Lele will be riding his "little Pigeon" brand electric car from balloon a start to balloon B to each balloon painted once 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 each test instance first behaves as 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 one row for each test instance, including n integers, and the number of numbers representing the number of times the balloon was painted in total.

Sample Input31 12 23 33 1 11 21 30

Sample OUTPUT1 1 13 2 1 Idea: Very typical segment tree, press test instructions to update the value of the interval
/*time memy 780ms 9132k by ORC 4 * * #include <cstdio> #include <cstring> #include <iostream>#    Include <algorithm>using namespace Std;const int N = 120005;int n,qans;struct node{int lt,rt,val; int add;}    S[4 * n];void Build (int v,int l,int r) {s[v].lt = l;    S[v].rt = R;    S[v].val = 0;    S[v].add = 0;    if (L = = r) return;    int m = (L + r) >> 1;    Build (V * 2, L, M); Build (V * 2 + 1, m + 1, R);} void update (int v,int l,int R) {if (s[v].lt = = L && s[v].rt = = r) {S[v].add + = 1; return;}        Only record increment, itself does not add, wait for the next query when first add if (s[v].add) {s[2 * v].add + = S[v].add;        S[2 * v + 1].add + = S[v].add;        S[v].val + = S[v].add;    S[v].add = 0;    } int m = (s[v].lt + s[v].rt) >> 1;    if (r <= m) update (2 * V, L, R);    else if (L > M) Update (2 * v + 1, L, R);        else {update (2 * V, L, M);    Update (2 * v + 1, m + 1, R); }}void query (int v,int l,int R) {if (s[v].add)//Query the time first look at the increment and pass   {s[2 * v].add + = S[v].add;        S[2 * v + 1].add + = S[v].add;    S[v].val + = S[v].add * (s[v].rt-s[v].lt + 1);//First add its own increment to its own s[v].add = 0;    } if (s[v].lt = = L && s[v].rt = = r) {Qans + = S[v].val; return;}    int m = (s[v].lt + s[v].rt) >> 1;    if (r <= m) query (2 * V, L, R);    else if (L > M) query (2 * v + 1, L, R);        else {query (2 * V, L, M);    Query (2 * v +1, M + 1, R);    }}void solve () {build (1, 1, N);    qans=0;    int A, B;        for (int i = 0;i < n; ++i) {cin>>a>>b;    Update (1, a, b);    } for (int i = 1;i < n; ++i) {qans=0;    Query (1, I, I);    cout<<qans<< ";    } qans=0;    Query (1, n, N); Cout<<qans<<endl;}    int main () {std::ios::sync_with_stdio (0);    while (Cin>>n && N) solve (); return 0;}

Color the ball (segment tree)

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.