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个询问,询问a->b能否买到票
若能买到票就输出i,表示这是第几次询问
只要再[a,b)这个区间中的人数还小于k个,就能买到票
并且将这个区间中的最多的人数更新
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
int lazy[1000100<<2],seg[1000100<<2];
int ans[1000100];
void upGo(int rt)
{
seg[rt]=max(seg[rt<<1],seg[rt<<1|1]);
}
void pushDown(int rt,int len)
{
if(lazy[rt])
{
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
seg[rt<<1]+=lazy[rt];///直接计算更新后rt左右子树的值,而不是通过下向上累加来更新值
seg[rt<<1|1]+=lazy[rt];
lazy[rt]=0;
}
}
void build(int rt,int l,int r)
{
lazy[rt]=0;
seg[rt]=0;
if(l==r)return;
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
upGo(rt);
}
void upDate(int rt,int l,int r,int a,int b,int value)
{
if(a<=l&&r<=b)
{
lazy[rt]+=value;///lazy[rt]=value
seg[rt]+=value;
return;
}
pushDown(rt,r-l+1);
int mid=(l+r)>>1;
if(a<=mid)upDate(rt<<1,l,mid,a,b,value);
if(b>mid) upDate(rt<<1|1,mid+1,r,a,b,value);
upGo(rt);
}
int query(int rt,int a,int b,int l,int r)
{
if(a<=l&&r<=b)return seg[rt];
pushDown(rt,r-l+1);
int mid=(l+r)>>1;
int ans=0;
if(a<=mid)ans=max(ans,query(rt<<1,a,b,l,mid));
if(b>mid)ans=max(ans,query(rt<<1|1,a,b,mid+1,r));
return ans;
}
int main()
{
int t;scanf("%d",&t);
for(int kase=1;kase<=t;kase++)
{
int k,q;
printf("Case %d:\n",kase);
scanf("%d%d",&k,&q);
build(1,1,1000000);
int x,y,L=0;
for(int i=0;i<q;i++){
scanf("%d%d",&x,&y);
y--;
if(query(1,x,y,1,1000000)<k){
ans[L++]=i+1;
upDate(1,1,1000000,x,y,1);
}
}
for(int i=0;i<L;i++){
printf("%d ",ans[i]);
}
printf("\n\n");
}
}