HDU - 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.

InputThe 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.OutputFor 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个乘客在排队,问你哪几个乘客可以上车,乘客必须按顺序来处理(不能贪心)

然后下面就依次给出第i个乘客的上车时间和下车时间

其实就是求区间最值,该最值表示在这个区间代表的时间段内最多有多少名乘客

修改的话也是区间修改,依次处理乘客,能上车就修改该区间的值

最后行末空格不要去掉,我就是这样画蛇添足PE了好几发

代码

#include
#include
#include
using namespace std;
#define ls o<<1
#define rs o<<1|1
#define lson L,mid,ls
#define rson mid+1,R,rs
#define mdzz int mid=(L+R)>>1;
int N,M;
int tree[4000005];
int label[4000005];
int cnt[100005];
void pushup(int o){
    tree[o]=max(tree[ls],tree[rs]);    //求最值
}
void pushdown(int o){
    if(label[o])                       //修改lazy标记
    {
        label[ls]+=label[o];           //将标记传递至下一层
        label[rs]+=label[o];
        tree[ls]+=label[o];            //同时这一层的就得把值给更新了
        tree[rs]+=label[o];
        label[o]=0;                    //更新完那就没lazy标记什么事了,初始化为0
    }
}

/*void build(int L,int R,int o){//建树
    if(L==R){
        tree[o]=0;
        return;
    }
    mdzz;
    build(lson);
    build(rson);
    pushup(o);
}*/
/*void update(int p,int L,int R,int o,int v){ //点修改
    if(L==R){
        tree[o]=v;
        return;
    }
    pushdown(o);
    mdzz;
    if(p<=mid) update(p,lson,v);
    else update(p,rson,v);
    pushup(o);
}*/
void update(int l,int r,int L,int R,int o,int v){//[l,r]区间修改
    if(l<=L&&R<=r){
        label[o]++;                   //这里只能是++
        //tree[o]+=v*(R-L+1);
        tree[o]++;
        return;
    }
    pushdown(o);
    mdzz;
    if(l<=mid) update(l,r,lson,v);
    if(r>mid) update(l,r,rson,v);
    pushup(o);
}
long long query(int l,int r,int L,int R,int o){//[l,r]区间询问
    if(r>=R&&l<=L) return tree[o];
    pushdown(o);
    mdzz;
    long long ans=0;                      //那肯定是返回最大值啊
    if(l<=mid)
        ans=query(l,r,lson);
    if(r>mid)
        ans=max(ans,query(l,r,rson));
    return ans;
}

/*int query(int p,int L,int R,int o){//点询问
    if(L==R) return 0;
    pushdown(o);
    mdzz;
    if(p<=mid) return query(p,lson);
    else return query(p,rson);
}*/
int main()
{
    int T;
    scanf("%d",&T);
    char s;
    int a,b,z;
    int cas=1;
    while(T--)
    {
        int c=0;
        memset(label,0,sizeof label);
        memset(tree,0,sizeof tree);
        scanf("%d%d",&N,&M);
        for(int i=1;i<=M;i++)
        {
            scanf("%d%d",&a,&b);
            if(query(a,b-1,1,1000000,1)


你可能感兴趣的:(HDU - 3577 Fast Arrangement (线段树区间修改及查询模板题))