【线段树区间修改】hdu3577

来源:互联网 发布:北京政务数据资源网 编辑:程序博客网 时间:2024/06/05 08:20

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
13 61 61 63 41 51 22 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 1000000int 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
原创粉丝点击