zoukankan      html  css  js  c++  java
  • HDU-3577 Fast Arrangement(线段树、段变化模板)

    Fast Arrangement

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


    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
     
    题目大意:车上最多能装k个人,q次上下车。求哪几次是允许的。
     
    解题思路:有个坑是某个人从a上车在b下车,a~b-1在车上,到b是就不在了。幸亏有人提醒。。不然觉得自己也要wa好多次。。
         就是运用lazy思想,理解后几乎就是套模板。个人觉得重点是理解pushdown、pushup函数。上一篇说过了 (http://www.cnblogs.com/WWkkk/p/7366754.html)这里就不说了。。直接上代码
     
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    const int N=1e6+5;
    const int maxn = 4*N;
    int vis[N],sum[maxn],lazy[maxn];
    int k,m,a,b;
    
    void pushdown(int num)
    {
        if(lazy[num])
        {
            lazy[num*2] += lazy[num];
            lazy[num*2+1] += lazy[num];
    
            sum[num*2] += lazy[num];
            sum[num*2+1] += lazy[num];
    
            lazy[num] = 0;
        }
    }
    
    void pushup(int num)
    {
        sum[num] = max(sum[num*2],sum[num*2+1]);
    }
    
    void build(int num,int l,int r)
    {
        lazy[num] = 0;
        sum[num] = 0;
        if(l==r)
        {
            return ;
        }
        int mid = (l+r)/2;
        build(num*2,l,mid);
        build(num*2+1,mid+1,r);
        pushup(num);
    }
    
    void update(int num,int l,int r)
    {
        if(a<=l&&b>=r)
        {
            lazy[num]++;
            sum[num] ++;
            return ;
        }
        pushdown(num);
        int mid=(l+r)/2;
        if(a<=mid)
            update(num*2,l,mid);
        if(b>mid)
            update(num*2+1,mid+1,r);
        pushup(num);
    }
    
    int query(int num,int l,int r)
    {
        if(a<=l&&b>=r)
        {
            return sum[num];
        }
        int mid = (l+r)/2;
        int ans = 0;
        pushdown(num);
        if(mid>=a)
        {
            ans = max(ans,query(num*2,l,mid));
        }
        if(mid<b)
        {
            ans = max(ans,query(num*2+1,mid+1,r));
        }
        return ans;
    }
    
    int main()
    {
        int T,Case=1;
        scanf("%d",&T);
        while(T--)
        {
            //memset(vis,false,sizeof(vis));
            scanf("%d %d",&k,&m);
            build(1,1,1000000);
            int n =0;
            for(int i=1;i<=m;i++)
            {
                scanf("%d %d",&a,&b);
                b = b-1;
                int t=query(1,1,1000000);
                if(t<k)
                {
                    update(1,1,1000000);
                    vis[n++] = i;
                }
            }
            printf("Case %d:
    ",Case++);
            for(int i=0;i<n;i++)
            {
                printf("%d ",vis[i]);
            }
            printf("
    
    ");
        }
    }
  • 相关阅读:
    C#中Dictionary的用法
    安装 Hyperledger Explorer及翻译中文
    Centos7 区块链 HyperLedger Fabric1.0.0安装
    install Hyperledger Fabric Ubuntu 16.04 x64
    三大方案解决了Intellij IDEA 2017/2018.1.5 输入法 不跟随
    ### Cause: java.sql.SQLException: Connection is read-only. Queries leading to data modification are
    could not find implicit value for parameter param: org.apache.spark.AccumulatorParam
    面试感悟----一名3年工作经验的程序员应该具备的技能
    Jenkins Git Maven搭建自动化部署项目环境 邮件通知
    前缀、中缀、后缀表达式
  • 原文地址:https://www.cnblogs.com/WWkkk/p/7366848.html
Copyright © 2011-2022 走看看