【线段树区间修改】hdu3577

Fast Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2228    Accepted Submission(s): 625


Problem Description
Chinese always have the railway tickets problem because of its' huge amount of passangers and stations. Now goverment need you to develop a new tickets query system.
One train can just take k passangers. And each passanger can just buy one ticket from station a to station b. Each train cannot take more passangers any time. The one who buy the ticket earlier which can be sold will always get the ticket.
 

Input
The input contains servel test cases. The first line is the case number. In each test case:
The first line contains just one number k( 1 ≤ k ≤ 1000 ) and Q( 1 ≤ Q ≤ 100000 )
The following lines, each line contains two integers a and b, ( 1 ≤ a < b ≤ 1000000 ), indicate a query.
Huge Input, scanf recommanded.
 

Output
For each test case, output three lines:
Output the case number in the first line.
If the ith query can be satisfied, output i. i starting from 1. output an blank-space after each number.
Output a blank line after each test case.
 

Sample Input
  
  
1 3 6 1 6 1 6 3 4 1 5 1 2 2 4
 

Sample Output
  
  
Case 1: 1 2 3 5
线段树区间修改题。
题意:k,q 一辆火车最多乘坐k个人,q个询问,问是否可以满足乘坐区间,可以的话就输出询问序列号 // 思路:更新区间,每次累加1,当区间值为k时拒绝累加 
每次先询问是否满足条件,然后如果满足条件就update线段树进行区间修改。
如果开始就考虑是否满足条件,如何在线段树里判断,就会有点烦。
和上一道题其实是一样的,只不过这次存储的是区间的最大值。还是用到了懒惰标记。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 1000005
#define N 1000000

int inc[maxn<<2];
int sum[maxn<<2];
int ans[N+5];


void pushup(int rt)
{
	sum[rt]=max(sum[rt<<1],sum[rt<<1|1]);
} 
void pushdown(int rt)
{
	if(inc[rt])
	{
		inc[rt<<1]+=inc[rt];
		inc[rt<<1|1]+=inc[rt];
		sum[rt<<1]+=inc[rt];
		sum[rt<<1|1]+=inc[rt];
		inc[rt]=0;
	}
}

void update(int L,int R,int val,int l,int r,int rt)
{
	sum[rt]+=val;
	if(L<=l && r<=R)
	{
		inc[rt]+=val;
		return ;
	}
	pushdown(rt);
	int m=(l+r)>>1;
	if(L<=m)update(L,R,val,l,m,rt<<1);
	if(m<R)update(L,R,val,m+1,r,rt<<1|1);
	pushup(rt);
}

int query(int L,int R,int l,int r,int rt)
{
	if(L<=l && r<=R)
	{
		return sum[rt];
	}
	pushdown(rt);
	int m=(l+r)>>1,ret=0;
	if(L<=m)  ret=max(ret,query(L,R,l,m,rt<<1));
	if(m<R)ret=max(ret,query(L,R,m+1,r,rt<<1|1));
	return ret;
	
}
int main()
{
	//freopen("q.in","r",stdin);
	int i,j;
	int k,q;
	int t=1,cas,len;
	int a,b;
	scanf("%d",&cas);
	while(cas--)
	{
		len=0;
		memset(inc,0,sizeof(inc));
		memset(sum,0,sizeof(sum));
		scanf("%d%d",&k,&q);
		for(i=1;i<=q;i++)
		{
			scanf("%d%d",&a,&b);
			b--;
			if(a>b)swap(a,b);
			int tmp=query(a,b,1,N,1);
		//	cout<<tmp<<endl;
			if(tmp<k)
			{
			//	cout<<i<<endl;
				update(a,b,1,1,N,1);
				ans[len++]=i;
			}
		}
		printf("Case %d:\n",t++);
		for(i=0;i<len;i++)printf("%d ",ans[i]);
		printf("\n\n");
	}
} 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值