1. 程式人生 > >3577 Fast Arrangement 離散化

3577 Fast Arrangement 離散化

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 

題解: 把行程的區間用點來衡量 因為在2下車後 也可在2上車  資料太大,在預處理離散化一下即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define lowbit(x) (x&(-x))
const int N=1e6+10;
typedef long long ll;
struct node{
	int l,r;
	int laz;
	int maxx;
}tree[N<<2];
struct node1
{
    int l,r;
}a[N];
int k,q,ans[N],cnt,b[N];
void build(int l,int r,int cur)
{
	tree[cur].laz=0;
    tree[cur].maxx=0;
	tree[cur].l=l;
	tree[cur].r=r;
	if(l==r) return;
	int mid=(r+l)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
}
void pushdown(int cur)
{
    if(tree[cur].laz)
    {
        tree[cur<<1].laz+=tree[cur].laz;
        tree[cur<<1].maxx+=tree[cur].laz;
        tree[cur<<1|1].laz+=tree[cur].laz;
        tree[cur<<1|1].maxx+=tree[cur].laz;
        tree[cur].laz=0;
    }

}
void pushup(int cur)
{
    tree[cur].maxx=max(tree[cur<<1].maxx,tree[cur<<1|1].maxx);
}
int query(int pl,int pr,int cur)
{
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        return tree[cur].maxx;
    }
    pushdown(cur);
    int res=0;
    if(pl<=tree[cur<<1].r) res=max(res,query(pl,pr,cur<<1));
    if(pr>=tree[cur<<1|1].l) res=max(res,query(pl,pr,cur<<1|1));
    return res;
}
void update(int pl,int pr,int cur)
{
    if(pl<=tree[cur].l&&tree[cur].r<=pr)
    {
        tree[cur].maxx++;
        tree[cur].laz++;
        return;
    }
    pushdown(cur);
    if(pl<=tree[cur<<1].r) update(pl,pr,cur<<1);
    if(pr>=tree[cur<<1|1].l) update(pl,pr,cur<<1|1);
    pushup(cur);
}
int main()
{
	int T,nn=1;
	scanf("%d",&T);
    while(T--)
    {
    	scanf("%d%d",&k,&q);
    	for(int i=1;i<=q;i++) scanf("%d%d",&a[i].l,&a[i].r),b[2*i-1]=a[i].l,b[2*i]=a[i].r;
    	sort(b+1,b+1+2*q);
    	int len=unique(b+1,b+1+2*q)-(b+1);
    	build(1,len,1);
    	cnt=0;
    	for(int i=1;i<=q;i++)
        {
            int l=lower_bound(b+1,b+1+len,a[i].l)-b;
            int r=lower_bound(b+1,b+1+len,a[i].r)-b;
        //    cout<<l<<" "<<r<<endl;
            r--;
            if(query(l,r,1)<k) update(l,r,1),ans[cnt++]=i;
        }
        printf("Case %d:\n",nn++);
        for(int i=0;i<cnt;i++)printf("%d ",ans[i]);
        puts("");
        puts("");
    }
	return 0;
}