HDU 3577 Fast Arrangement

成段更新的线段树- -!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<string>
using namespace std;
#define maxn 1000010
#define l(i) ((i)<<1)
#define r(i) ((i)<<1|1)
struct node{
	int left,right,mid,max,add;
}tr[maxn<<2];
void build(int l,int r,int k){
	tr[k].max=0;
	tr[k].add=0;
	int mid=(l+r)>>1;
	tr[k].left=l;
	tr[k].right=r;
	tr[k].mid=mid;
	if(l==r)
		return;
	build(l,mid,l(k));
	build(mid+1,r,r(k));
}
int max(int a,int b)
{
	return a>b?a:b;
}
int check(int left,int right,int ind){
	if(left<=tr[ind].left&&right>=tr[ind].right) 
		return tr[ind].max;
	if(tr[ind].add){
		tr[l(ind)].max+=tr[ind].add;
		tr[l(ind)].add+=tr[ind].add;
		tr[r(ind)].max+=tr[ind].add;
		tr[r(ind)].add+=tr[ind].add;
		tr[ind].add=0;
	}
	int tmp=0;
	if(left<=tr[ind].mid) tmp=max(tmp,check(left,right,l(ind)));
	if(tr[ind].mid<right) tmp=max(tmp,check(left,right,r(ind)));
	return tmp;
}
void update(int left,int right,int ind){
	if(left<=tr[ind].left&&tr[ind].right<=right){
		tr[ind].max++;
		tr[ind].add++;
		return;
	}
	if(tr[ind].add)
	{
		tr[l(ind)].max+=tr[ind].add;
		tr[l(ind)].add+=tr[ind].add;
		tr[r(ind)].max+=tr[ind].add;
		tr[r(ind)].add+=tr[ind].add;
		tr[ind].add=0;
	}
	if(left<=tr[ind].mid) update(left,right,l(ind));
	if(tr[ind].mid<right) update(left,right,r(ind));
	tr[ind].max=max(tr[l(ind)].max,tr[r(ind)].max);
}
int main()
{
	int t,n,i,j,a,b,tot,c=0,q;
	//freopen("sample.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		printf("Case %d:\n",++c);
		tot=0;
		scanf("%d%d",&n,&q);
		build(1,1000000,1);
		for(i=1;i<=q;i++){
			scanf("%d%d",&a,&b);
			--b;
			if(check(a,b,1)<n)
			{
				update(a,b,1);
				printf("%d ",i);
			}
		}
		printf("\n\n");
	}
//	system("pause");
	return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值