2015 多校联赛 ——HDU5372(树状数组)

 

Sample Input
3 0 0 0 3 0 1 5 0 1 0 0 1 1 0 1 0 0
 

 

Sample Output
Case #1: 0 0 0 Case #2: 0 1 0 2

 

 

有0,1两个操作,0  x代表添加从x 到 x + i(带表第i次添加)的线段,每次添加时问被其覆盖的线段有多少。

                           1  x代表删除第i次添加的。

思路:每一次添加后,求出小于x的左节点个数x1,小于等于y的右节点个数x2。 x2- x1即可

改变单个节点,所以树状数组更加合适


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define N 200050
#define mod 258280327

int le[N],re[N],lr[N],rr[N];
int oper[N],Left[N],Right[N],id[N];
int c[N];
int tot;

int lowbit(int x)
{
    return x&-x;
}
void update(int *c,int n,int k,int v)
{
    while(k <= n)
    {
        c[k] += v;
        k += lowbit(k);
    }
}

int query(int *c ,int k)
{
    int ans = 0;
    while(k > 0)
    {
        ans += c[k];
        k -= lowbit(k);
    }
    return ans;
}


int main()
{
    //freopen("4.txt","r",stdin);
    int cas=1,L=1,l,ans,nul,nur,n,Q;
    while(scanf("%d",&n)!=EOF)
    {
        Q = nul = nur = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d",&oper[i],&l);
            if(oper[i] == 0)
            {
                id[++Q] = i;
                le[i] = l;
                re[i] = l+L;
                Left[nul++] = le[i];
                Right[nur++] = re[i];
                L++;
            }
            else
            {
                le[i] = l;
            }
        }
        printf("Case #%d:\n",cas++);

        memset(lr,0,sizeof(lr));
        memset(rr,0,sizeof(rr));

        sort(Left, Left + nul);
        nul = unique(Left, Left + nul) - Left;
        sort(Right, Right + nur);
        nur = unique(Right, Right + nur) - Right;

        for(int i = 1; i <= n; i++)
        {
            if(oper[i] == 0)
            {
                int x1 = lower_bound(Left,Left+nul,le[i]) - Left + 1;
                int x2 = lower_bound(Right,Right+nur,re[i]) - Right + 1;
                ans = query(rr,x2)-query(lr,x1-1);
                update(rr,nur,x2,1);
                update(lr,nul,x1,1);
                printf("%d\n",ans);
            }
            else
            {
                int v = id[le[i]];
                int x1 = lower_bound(Left,Left+nul,le[v]) - Left + 1;
                int x2 = lower_bound(Right,Right+nur,re[v]) - Right + 1;
                update(lr,nul,x1,-1);
                update(rr,nur,x2,-1);
            }
        }
    }
    return 0;
}

  









 

转载于:https://www.cnblogs.com/Przz/p/5409779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值