现在的位置: 首页 > 综合 > 正文

hdu3577Fast Arrangement (线段树+离散化,记录最大值的变形)

2018年02月22日 ⁄ 综合 ⁄ 共 2550字 ⁄ 字号 评论关闭
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
题目意思是:有t个case,每个case每一行有两个值k,q,要求每列车装的人数不超过k,接下来有q行,每一行有两个数a,b.表示第I个人要从a站到b站。如果列车能载这个人则输出I。每个值后都跟一个空格。每个case后跟一个空行。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
#define N 200010
struct node
{
    int w,sum;//分别是节点的孩子要加的值,当前节点的区间最大覆盖的次数
}tree[4*N];
int K,flog;//K是最大不能超过和值,flog為1時表能载这个人从a站到b站,否则不能
int max(int a,int b){return a>b?a:b;}
void builde(int l,int r,int k)
{
    int m=(l+r)>>1;
    tree[k].sum=0; tree[k].w=0;
    if(l==r)
        return ;
    builde(l,m,k<<1);
    builde(m+1,r,k<<1|1);
}
void updata(int l,int r,int k,int L,int R)//插入
{
    if(L<=l&&r<=R)
    {
        tree[k].sum++;tree[k].w++;
        if(tree[k].sum>K) flog=0;//只要满足则为0
         return ;
    }
    if(tree[k].w)
    {
        tree[k<<1].sum+=tree[k].w;
        tree[k<<1].w+=tree[k].w;
        tree[k<<1|1].sum+=tree[k].w;
        tree[k<<1|1].w+=tree[k].w;
    }
    tree[k].w=0;
    int m=(l+r)>>1;
    if(L<=m)  updata(l,m,k<<1,L,R);
     if(R>m)  updata(m+1,r,k<<1|1,L,R);
     tree[k].sum=max(tree[k<<1].sum,tree[k<<1|1].sum);
}
void delet(int l,int r,int k,int L,int R)//插入不成则执行
{
    if(L<=l&&r<=R){
        tree[k].sum--;
        tree[k].w--;
         return ;
    }
    int m=(l+r)>>1;
    if(L<=m) delet(l,m,k<<1,L,R);
    if(R>m) delet(m+1,r,k<<1|1,L,R);
    tree[k].sum=max(tree[k<<1].sum,tree[k<<1|1].sum);
}
int sor[N*2],sn;
map<int,int>loc;//记录离散化后每个值的位置
int cmp(int a,int b){return a<b;}
int resort()//离散化,并反回有多少个点
{
    int i,j;
    sort(sor+1,sor+sn+1,cmp);
    loc[sor[1]]=1;
    for(i=2,j=1;i<=sn;i++)
    if(sor[i]!=sor[j])
        {
            sor[++j]=sor[i];
            loc[sor[j]]=j;
        }
    return j;
}
struct nn
{
    int l,r;
}ans[N];
int main()
{
    int m,t,i,c=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&K,&m); sn=0;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&ans[i].l,&ans[i].r);
            sor[++sn]=ans[i].l; sor[++sn]=ans[i].r;
        }
        sn=resort();
        builde(1,sn,1);
        printf("Case %d:\n",++c);
        for(i=1;i<=m;i++)
        {
            flog=1;
            updata(1,sn,1,loc[ans[i].l],loc[ans[i].r]-1);//下车的那一站不算入
            if(flog) printf("%d ",i);
            else delet(1,sn,1,loc[ans[i].l],loc[ans[i].r]-1);
        }
        printf("\n\n");
    }
}

抱歉!评论已关闭.