线段树懒惰写法板子

//对应d题目是HDU3577
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<string>
using namespace std;
int k;
const int maxn=1000000+10;
int an[100000+10];
struct tree
{
    int l;
    int r;
    int sum;
    int add;
}node[4*maxn];
void build(int l,int r,int x)
{
    node[x].l=l;
    node[x].r=r;
    node[x].add=0;
    if(l==r)
    {
        node[x].sum=0;
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,x<<1);
    build(mid+1,r,x<<1|1);
    node[x].sum=max(node[x<<1].sum,node[x<<1|1].sum);
}
void update(int x,int l,int r)
{
    if(node[x].l>=l&&node[x].r<=r)
    {
        node[x].add+=1;
        node[x].sum+=1;
        return ;
    }
    if(node[x].add)
    {
        node[x<<1].sum+=node[x].add;
        node[x<<1|1].sum+=node[x].add;
        node[x<<1].add+=node[x].add;
        node[x<<1|1].add+=node[x].add;
        node[x].add=0;
    }
    int mid=(node[x].l+node[x].r)>>1;
    if(l<=mid)
        update(x<<1,l,r);
    if(r>mid)
        update(x<<1|1,l,r);
    node[x].sum=max(node[x<<1].sum,node[x<<1|1].sum);
}
int querry(int x,int l,int r)
{
    if(node[x].l>=l&&node[x].r<=r)
    {
        return node[x].sum;
    }
    int ans=0;
    if(node[x].add)
    {
        node[x<<1].sum+=node[x].add;
        node[x<<1|1].sum+=node[x].add;
        node[x<<1].add+=node[x].add;

        node[x<<1|1].add+=node[x].add;
        node[x].add=0;
    }
    int mid=(node[x].l+node[x].r)>>1;
    if(l<=mid)
        ans=max(ans,querry(x<<1,l,r));
    if(r>mid)
        ans=max(ans,querry(x<<1|1,l,r));
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    int tot=1;
    while(t--)
    {

        int n;
        int cnt=0;
        scanf("%d%d",&k,&n);
        build(1,1000000,1);
        for(int i=1;i<=n;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            int temp=querry(1,a,b-1);
            if(temp<k)
            {
                an[cnt++]=i;
                update(1,a,b-1);
            }
        }
        printf("Case %d:\n",tot++);
        for(int i=0;i<cnt;i++)
        {
            printf("%d ",an[i]);
        }
      printf("\n\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值