HDU 1556 Color the ball(线段树) - dblank

HDU 1556 Color the ball(线段树)

<h1>Color the ball</h1>
Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12851    Accepted Submission(s): 6426

<div class="panel_title" align="left">Problem Description</div>
<div class="panel_content">N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?</div>
<div class="panel_bottom"></div>
 
<div class="panel_title" align="left">Input</div>
<div class="panel_content">每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。
当N = 0,输入结束。</div>
<div class="panel_bottom"></div>
 
<div class="panel_title" align="left">Output</div>
<div class="panel_content">每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。</div>
<div class="panel_content">这题可以用线段树叶也可以树状数组。</div>
<div class="panel_content">线段树:</div>
<div class="panel_content">

#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct node
{
    int l, r, sum, mark;
} s[100010<<2];
void pushup(int cnt)
{
    s[cnt].sum = s[cnt<<1].sum +  s[cnt<<1|1].sum;
}
void pushdown(int cnt, int len)
{
    if(s[cnt].mark)
    {
        s[cnt<<1].mark += s[cnt].mark;
        s[cnt<<1|1].mark += s[cnt].mark;
        s[cnt<<1].sum += s[cnt].mark * (len - (len>>1));
        s[cnt<<1|1].sum += s[cnt].mark * (len>>1);
        s[cnt].mark = 0;
    }
}
void build_tree(int l, int r, int cnt)
{
    s[cnt].l = l;
    s[cnt].r = r;
    s[cnt].sum = 0;
    s[cnt].mark = 0;
    if(l==r)
        return ;
    int mid = (l + r)>>1;
    build_tree(l, mid, cnt<<1);
    build_tree(mid+1, r, cnt<<1|1);
    pushup(cnt);
}
void update(int l, int r, int cnt)
{
    if(s[cnt].l >= l && s[cnt].r <= r)
    {
        s[cnt].sum += (s[cnt].r - s[cnt].l + 1);
        s[cnt].mark++;
        return ;
    }
    pushdown(cnt, s[cnt].r - s[cnt].l + 1);
    int mid = (s[cnt].r + s[cnt].l) >> 1;
    if(l<=mid)
        update(l, r, cnt<<1);
    if(r>mid)
        update(l, r, cnt<<1|1);
    pushup(cnt);
}
int query(int l, int r, int cnt)
{
    if(s[cnt].l >= l && s[cnt].r <= r)
    {
        return s[cnt].sum;
    }
    pushdown(cnt, s[cnt].r - s[cnt].l + 1);
    int ret = 0, mid = (s[cnt].r + s[cnt].l) >> 1;
    if(l <= mid)
        ret += query(l, r, cnt<<1);
    if(r > mid)
        ret += query(l, r, cnt<<1|1);
    return ret;
}
int main()
{
    int n, a, b;
    while(cin>>n && n)
    {
        int temp = n;
        build_tree(1, n, 1);
        while(temp--)
        {
            scanf("%d%d", &a, &b);
            update(a, b, 1);
        }
        for(int i = 1; i<=n; i++)
        {
            printf("%d", query(i, i, 1));
            if(i!=n)
                printf(" ");
        }
        printf("n");
    }
    return 0;
}

树状数组:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
int cnt[100010], n;
int lowbit(int x)
{
    return x & (-x);
}
void add(int x, int num)
{
    while(x<=n)
    {
        cnt[x]+=num;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int res = 0;
    while(x>=1)
    {
        res += cnt[x];
        x-=lowbit(x);
    }
    return res;
}
int main()
{
    int temp;
    while(cin>>n && n)
    {
        temp = n;
        memset(cnt, 0, sizeof(cnt));
        while(temp--)
        {
            int s, e;
            scanf("%d%d", &s, &e);
            add(s, 1);
            add(e+1, -1);
        }
     //   for(int i = 1; i<=n; i++)
      //      printf("%d ", cnt[i]);
      //  printf("n");
        printf("%d", sum(1));
        for(int i = 2; i<=n; i++)
            printf(" %d", sum(i));
        printf("n");
    }
}

 

</div>

相关文章

发表新评论