A - Color the ball(分块1)(分块法,,,,线段树法,,,,不知道啥法)

A - Color the ball(分块1)

 

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。 
当N = 0,输入结束。

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。

Sample Input

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0

Sample Output

1 1 1
3 2 1

不知道是啥法:


//Full of love and hope for life
/*
                                                              *               ******
      *****************                                     **             *******
   ***********************                                ***                 ******
 ***************************                    **      ****
*****************************                         *****
*****************************                       ******
*****************************                     *******            *********
 ***************************                 ***********           *********             **********
   ***********************                        *******            *********
     *******************                            ******
       ***************                                *****
         ***********                            **      ****
           *******                                        ***                 ******
             ***                                            **             *******
              *                                               *               ******
*/

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define inf 0x3f3f3f3f

using namespace std;

int main()
{
    int a;
    while(scanf("%d",&a)&&a)
    {
        int n[100010]= {},t=a;
        while(t--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            n[x]++;
            n[y+1]--;
        }
        for(int i=1; i<a; i++)
        {
            n[i]+=n[i-1];
            printf("%d ",n[i]);
        }
        printf("%d\n",n[a]+n[a-1]);
    }
    return 0;
}

分块:


//Full of love and hope for life
/*
                                                              *               ******
      *****************                                     **             *******
   ***********************                                ***                 ******
 ***************************                    **      ****
*****************************                         *****
*****************************                       ******
*****************************                     *******            *********
 ***************************                 ***********           *********             **********
   ***********************                        *******            *********
     *******************                            ******
       ***************                                *****
         ***********                            **      ****
           *******                                        ***                 ******
             ***                                            **             *******
              *                                               *               ******
*/

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define inf 0x3f3f3f3f

using namespace std;

const int INF=0x3f3f3f3f;
const int maxn=1e5+5;

int belong[maxn],l[maxn],r[maxn],block,num,ans[maxn],add[maxn];
int n;

void build()
{
    memset(ans,0,sizeof(ans));
    memset(add,0,sizeof(add));
    int block=sqrt(n);
    num=n/block;
    if(n%block) num++;
    for(int i=1; i<=num; i++)
    {
        l[i]=(i-1)*block+1,r[i]=i*block;
    }
    r[num]=n;
    for(int i=1; i<=n; i++)
    {
        belong[i]=(i-1)/block+1;
    }
}

void update(int a,int b)
{
    if(belong[a]==belong[b])
    {
        for(int i=a; i<=b; i++) ans[i]++;
    }
    else
    {
        for(int i=a; i<=r[belong[a]]; i++)
        {
            ans[i]++;
        }
        for(int i=belong[a]+1; i<belong[b]; i++)
        {
            add[i]++;
        }
        for(int i=l[belong[b]]; i<=b; i++)
        {
            ans[i]++;
        }
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    while(cin>>n&&n)
    {
        build();
        int a,b;
        for(int i=0; i<n; i++)
        {
            cin>>a>>b;
            update(a,b);
        }
        cout<<ans[1]+add[belong[1]];
        for(int i=2; i<=n; i++)
        {
            cout<<" "<<ans[i]+add[belong[i]];
        }
        cout<<endl;
    }
    return 0;
}

线段树:

#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>

using namespace std;

#define MAXN 440000
//Maxn 代表结点个数最大值
struct Node
{
    int left;
    int right;
    long long cnt;//涂的次数
}tree[MAXN];
long long add[MAXN];//延迟标记
long long father[MAXN];//记录叶子节点的下标
void buildtree(int rt,int l,int r)
{
    tree[rt].left = l;
    tree[rt].right = r;
    tree[rt].cnt = 0;
    if(l==r)//到达叶子节点 递归结束
    {
        return ;
    }
    //递归左子树 区间范围是[l,(l+r)/2];
    buildtree(rt*2,l,(l+r)/2);
    //递归创建右子树 区间范围是[(l+r)/2 +1.r]
    buildtree(rt*2+1,(l+r)/2+1,r);
}
void PushDown(int rt,int num)
{
    if(add[rt])//如果延迟标记不为0 呢么就继续往下传递延迟
    {
        add[rt*2] += add[rt];
        add[rt*2 +1] += add[rt];
        tree[rt*2].cnt += add[rt] * (num - num/2);//其中左区间在更新cnt (num - num/2) 为左区间的个数
        tree[rt*2+1].cnt += add[rt] * (num / 2);//其中右区间在更新cnt
        add[rt] =0;//并将当前的延迟标记置0
    }
}
void update(int rt,int l,int r,int c)
{
    if(l== tree[rt].left && r == tree[rt].right)
    {
        add[rt] ++;
        tree[rt].cnt +=(r - l +1);//这里一定是c*(r-l+1) 不是add[rt] *(r - l+1) 
        return;
    }
    if(tree[rt].left==tree[rt].right) return; //不更新叶子节点

    PushDown(rt,tree[rt].right - tree[rt].left +1);//设置延迟标记
    //如果这个待更新的区间
    int mid = (tree[rt].left + tree[rt].right)/2;
    if(r<= mid)
        update(rt*2,l,r,c);//更新左子树
    else if(l > mid)
        update(rt*2+1,l,r,c);//更新右子树
    else
    {
        update(rt*2,l,mid,c);
        update(rt*2+1,mid+1,r,c);
    }
    tree[rt].cnt = (tree[rt*2].cnt + tree[rt*2 +1].cnt);//递归之后进行pushup
}
long long query(int rt,int l,int r)//查询区间
{
    if(l== tree[rt].left && r == tree[rt].right)
    {
        return tree[rt].cnt;
    }

    PushDown(rt,tree[rt].right - tree[rt].left +1);
    int mid = (tree[rt].left + tree[rt].right)/2;
    long long ans =0;
    if(r<= mid)
      ans =  query(rt *2, l, r);//继续在左区间进行更新
    else if(l > mid )
    {
       ans = query(rt *2+1,l, r);//继续在右区间进行更新
    }
    else
    {
        ans += query(rt *2, l, mid);//位于两个区间
        ans += query(rt *2+1, mid+1, r);
    }
    return ans;
}
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        buildtree(1,1,n);//建树
        for(int i =1;i<=n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            update(1,a,b,1);//数目加一
        }
        printf("%lld",query(1,1,1));
        for(int i = 2;i<=n;i++)
        {
            printf(" %lld",query(1,i,i));
        }
        printf("\n");
        memset(add,0,sizeof(add));
        memset(tree,0,sizeof(tree));
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值