HDU 1556 color the ball (line segment tree | tree array, Interval Update)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1556


Question:

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.

Analysis and Summary:

Basic segment update and single point query are supported by line segment tree or tree array.

Code:

1. Tree Array

#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long int64;const int MAXN  = 100005;int64 C[MAXN];int n;inline int lowbit(int x){return x&(-x);}int64 sum(int x){    int64 ret=0;    while(x > 0){        ret += C[x];        x -= lowbit(x);    }    return ret;}void add(int x,int val){    while(x <= n) {        C[x] += val;        x += lowbit(x);    }}int main(){    int a,b;    while(~scanf("%d",&n) && n){        memset(C, 0, sizeof(C));        for(int i=0; i<n; ++i){            scanf("%d%d",&a,&b);            add(a,1);            add(b+1,-1);        }        for(int i=1; i<=n; ++i){            if(i==1)printf("%lld",C[i]);            else printf(" %lld",sum(i));        }        puts("");    }    return 0;}

2. Line Segment tree

#include<iostream>#include<cstdio>#include<cstring>#define mem(str,x) memset(str,(x),sizeof(str))#define mid  ((left+right)>>1)#define len  (right-left+1)#define lson rt<<1, left, m#define rson rt<<1|1, m+1, rightusing namespace std;const int MAXN = 100005;int sum[MAXN<<2],col[MAXN<<2],n;inline void push_up(int rt){    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}inline void push_down(int rt, int _len){    if(col[rt]){        int l=rt<<1, r=rt<<1|1, m=_len>>1;        col[l] += col[rt];        col[r] += col[rt];        sum[l] += (_len-m) * col[rt];        sum[r] += m * col[rt];        col[rt] = 0;    }}void update(int rt,int left,int right,int l,int r){    if(l<=left && right<=r){        ++col[rt];        sum[rt] += len;        return;    }    push_down(rt,len);    int m = mid;    if(l <= m) update(lson,l,r);    if(r > m) update(rson,l,r);    push_up(rt);}int query(int rt,int left,int right,int l,int r){    if(left==l && right==r) return sum[rt];    int m = mid;    push_down(rt,len);    if(r <= m) return query(lson, l, r);    else if(l > m) return query(rson,l,r);    else return query(lson,l,m) + query(rson,m+1,r);}int main(){    int a,b;    while(~scanf("%d",&n) && n) {        mem(sum,0); mem(col,0);        for(int i=0; i<n; ++i){            scanf("%d%d",&a,&b);            update(1,1,n,a,b);        }        for(int i=1; i<=n; ++i){            if(i==1) printf("%d",query(1,1,n,i,i));            else printf(" %d",query(1,1,n,i,i));        }        puts("");    }    return 0;}

 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)


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.