hdu 3577(线段树的成段更新)

这个题有一点要考虑到,就是某人从a站坐到b站,他在b站就下车了,他在车上乘坐的区间为a到b-1。

这里贡献一组测试数据

1
3 10
4 5
5 6
6 7
7 8
9 10
1 4
1 10
2 9
4 6
3 8

输出

Case 1:
1 2 3 4 5 6 7 8 


然后就是求区间最大覆盖值了。

线段树功能:updata:成段更新,query:区间最值

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define lc l,m,index<<1
#define rc m+1,r,index<<1|1
#define N 1000005
struct node
{
	int add,cnt;
}seg[N<<2];
int q,k;
void fadd(int add,int l,int r,int index)
{
	seg[index].add+=add;
	seg[index].cnt+=add;
}
void pushup(int l,int r,int index)
{
	node& father=seg[index];
	node& lson=seg[index<<1];
	node& rson=seg[index<<1|1];
	father.cnt=max(lson.cnt,rson.cnt);
}
void pushdown(int l,int r,int index)
{
	node& father=seg[index];
	node& lson=seg[index<<1];
	node& rson=seg[index<<1|1];
	if(father.add)
	{
		int m=(l+r)>>1;
		fadd(father.add,lc);
		fadd(father.add,rc);
		father.add=0;
	}
}
void build(int l,int r,int index)
{
	seg[index].add=0;
	seg[index].cnt=0;
	int m=(l+r)>>1;
	if(l==r)return;
	build(lc);
	build(rc);
}
int query(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)return seg[index].cnt;
	pushdown(l,r,index);
	if(R<=m)return query(L,R,lc);
	else if(L>m)return query(L,R,rc);
	else return max(query(L,m,lc),query(m+1,R,rc));
}
void updata(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)
	{
		fadd(1,l,r,index);
		return;
	}
	pushdown(l,r,index);
	if(R<=m)updata(L,R,lc);
	else if(L>m)updata(L,R,rc);
	else
	{
		updata(L,m,lc);
		updata(m+1,R,rc);
	}
	pushup(l,r,index);
}
int main()
{
//	freopen("F:\\out.txt","w",stdout);
	int t,tcase=1,i,a,b,cnt;
	scanf("%d",&t);
	while(t--)
	{
		printf("Case %d:\n",tcase++);
		scanf("%d%d",&k,&q);
		build(1,1000000,1);
		for(i=1;i<=q;i++)
		{
			scanf("%d%d",&a,&b);
			b--;
			cnt=query(a,b,1,1000000,1);
			if(cnt<k)
			{
				printf("%d ",i);
				updata(a,b,1,1000000,1);
			}
		}
		printf("\n\n");
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值