Fast Arrangement
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3561 Accepted Submission(s): 1024
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.
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.
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.
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
Author
Louty (Special Thanks Nick Gu)
Source
Recommend
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define maxn 1000005
#define INF 0.000000001
using namespace std;
/*
首先在build上面赋值的顺序问题,
其次就是懒惰标记不仅在update上,在query上也要更新,
再然后就是区间划分的细节问题了,
在与mid比较的过程中,
不能出现死循环的问题。
在然后就是何时返回的终点问题:
必须在该节点的区间范围成为目标范围的子区间时返回值即可。
*/
struct node
{
int l,r;
int maxv;
int add;
node()
{
l=r=0;
add=maxv=0;
}
};
node stree[maxn<<2];
void pushup(int rt)
{
stree[rt].maxv=max(stree[rt<<1].maxv,stree[rt<<1|1].maxv);
}
void pushdown(int rt)
{
stree[rt<<1].add+=stree[rt].add;
stree[rt<<1|1].add+=stree[rt].add;
stree[rt<<1].maxv+=stree[rt].add;
stree[rt<<1|1].maxv+=stree[rt].add;
stree[rt].add=0;
}
void build(int l,int r,int rt)
{
stree[rt].add=0;
stree[rt].l=l;
stree[rt].r=r;
if(l==r) {stree[rt].maxv=0; stree[rt].add=0; return ;}
int mid=(l+r)>>1;
build(l,mid,rt<<1);
build(mid+1,r,rt<<1|1);
pushup(rt);
}
void update(int l,int r,int rt)
{
if(stree[rt].l>=l&&stree[rt].r<=r)
{
//cout<<l<<" "<<r<<" "<<stree[rt].l<<" "<<stree[rt].r<<endl;
stree[rt].add+=1;
stree[rt].maxv+=1;
return ;
}
if(stree[rt].add)
pushdown(rt);
int mid=(stree[rt].l+stree[rt].r)>>1;
if(l<=mid) update(l,r,rt<<1);
if(r>mid) update(l,r,rt<<1|1);
pushup(rt);
}
int query(int l,int r,int rt)
{
if(stree[rt].l>=l&&stree[rt].r<=r)
return stree[rt].maxv;
if(stree[rt].add) pushdown(rt);
int mid=(stree[rt].l+stree[rt].r)>>1;
int ans=0;
if(l<=mid) ans=max(ans,query(l,r,rt<<1));
if(r>mid) ans=max(ans,query(l,r,rt<<1|1));
return ans;
}
int x,y,k,q;
long long n;
int ans[maxn],cnt=0;
int main()
{
int t;cin>>t;
for(int ca=1;ca<=t;ca++)
{
cnt=0;
scanf("%d%d",&k,&q);
build(1,1000000,1);
for(int i=0;i<q;i++)
{
scanf("%d%d",&x,&y);
int q=query(x,y-1,1);
//cout<<"yes"<<endl;
if(q<k)
{
ans[cnt++]=i+1;
update(x,y-1,1);
}
// cout<<query(1,1,1)<<endl;
}
printf("Case %d:\n",ca);
for(int i=0;i<cnt-1;i++)
printf("%d ",ans[i]);
printf("%d \n\n",ans[cnt-1]);
}
return 0;
}