HDU 3577Fast Arrangement(线段树模板之区间增减更新 区间求和查询)

Fast Arrangement

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3995    Accepted Submission(s): 1141


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
 

 

Author
Louty (Special Thanks Nick Gu)
 

 

Source
 
Recommend
zhouzeyong   |   We have carefully selected several similar problems for you:  3578 2795 3581 1166 1698 
 
题目意思:
现在有一辆列车,可以坐k个人
先买到票的就有座位
现在给你q个买票问询:a站到b站
如果此票持有者路上车,则输出问询编号
线段树解决
如果此时a,b区间内最大值是小于k的,那么
说明此人可以上车
可以上车的话
将区间a,b内部的值加1
完全符合线段树的基本操作
区间更新 区间增减
区间查询 找最值
#include<stdio.h>
#include<iostream>
#include<vector>
#include <cstring>
#include <stack>
#include <cstdio>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include<string>
#include<math.h>
using namespace std;
const int max_v=1000005;
int ans[max_v];
struct node
{
    int l,r,v,lazy;
}tree[max_v<<2];
void build(int l,int r,int root)
{
    tree[root].l=l;
    tree[root].r=r;
    tree[root].v=0;
    tree[root].lazy=0;

    if(l==r)
        return ;

    int mid=(l+r)>>1;
    build(l,mid,root<<1);
    build(mid+1,r,root<<1|1);
}
void push_up(int root)//往上更新父节点数据
{
    tree[root].v=max(tree[root<<1].v,tree[root<<1|1].v);// 最值
}
void push_down(int root)//向下往左右儿子方向更新数据
{
    tree[root<<1].lazy+=tree[root].lazy;
    tree[root<<1|1].lazy+=tree[root].lazy;

    tree[root<<1].v+=tree[root].lazy;
    tree[root<<1|1].v+=tree[root].lazy;

    tree[root].lazy=0;//更新之后lazy清零
}
void update(int l,int r,int root)
{
    if(tree[root].l==l&&tree[root].r==r)//如果区间完全重合,则不需要继续往下更新了
    {
        tree[root].v+=1;
        tree[root].lazy+=1;
        return;
    }
    if(tree[root].lazy)//因为没有找到完全重合的区间,所以要先更新下一层区间;
        push_down(root);

    int mid=(tree[root].l+tree[root].r)>>1;
    if(l>mid)
        update(l,r,root<<1|1);
    else if(r<=mid)
        update(l,r,root<<1);
    else
    {
        update(l,mid,root<<1);
        update(mid+1,r,root<<1|1);
    }

    push_up(root);//最后还得往上面更新父节点区间
}
int getmax(int l,int r,int root)//查询区间最值
{
    if(tree[root].l==l&&tree[root].r==r)
        return tree[root].v;

    if(tree[root].lazy)//因为没有找到完全重合的区间,所以要先更新下一层区间
        push_down(root);

    int mid=(tree[root].l+tree[root].r)>>1;
    if(l>mid)
        return getmax(l,r,root<<1|1);
    else if(r<=mid)
        return getmax(l,r,root<<1);
    else
    {
        return max(getmax(l,mid,root<<1),getmax(mid+1,r,root<<1|1));
    }
}
int main()
{
    int t;
    scanf("%d",&t);
    int c=1;
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        int k,q;
        scanf("%d %d",&k,&q);
        build(1,1000000,1);
        int m=0;
        for(int i=0;i<q;i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            b--;
            if(getmax(a,b,1)<k)
            {
                ans[m++]=i+1;
                update(a,b,1);
            }
        }
         printf("Case %d:\n",c++);
        for(int i=0;i<m;i++)
            printf("%d ",ans[i]);
        printf("\n\n");
    }
    return 0;
}
/*
题目意思:
现在有一辆列车,可以坐k个人
先买到票的就有座位
现在给你q个买票问询:a站到b站
如果此票持有者路上车,则输出问询编号
线段树解决
如果此时a,b区间内最大值是小于k的,那么
说明此人可以上车
可以上车的话
将区间a,b内部的值加1

完全符合线段树的基本操作
区间更新 区间增减
区间查询 找最值
*/

 

posted @ 2018-08-14 18:16  西*风  阅读(185)  评论(0编辑  收藏  举报