1. 程式人生 > >HDU 3577 Fast Arrangement(線段樹功能:區間更新,查詢區間的最大覆蓋次數)

HDU 3577 Fast Arrangement(線段樹功能:區間更新,查詢區間的最大覆蓋次數)

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

題意:

給出K,和Q,有Q次詢問,火車最多同時不能坐超過 K 個乘客,每個乘客都是按次序登車,

若乘客能滿足上火車的條件則輸出編號,不能就不輸出!

PS:

線段樹查詢區間覆蓋次數!

注意:要求的是輸入的編號的每個數字後面都有空格,最後一個數字也不例外!

程式碼如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lson l , mid , rt << 1
#define rson mid+1 , r , rt << 1 | 1
const int maxn = 1000010;
const int MAXN = 1000000;
int add[maxn<<2];
int sum[maxn<<2];
int ans[100010];
void PushUp(int rt)
{
    sum[rt] = max(sum[rt<<1], sum[rt<<1|1]);
}
void PushDown(int rt)
{
    if(add[rt])
    {
        add[rt<<1] += add[rt];
        add[rt<<1|1] += add[rt];
        sum[rt<<1] += add[rt];
        sum[rt<<1|1] += add[rt];
        add[rt] = 0;
    }
}

void build(int l, int r, int rt)
{
    add[rt] = 0;
    sum[rt] = 0;//開始都是零
    if(l == r)
    {
        return ;
    }
    int mid = (l+r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        add[rt] += c;
        sum[rt] += c;
        return ;
    }
    PushDown(rt);
    int mid = (l + r) >> 1;
    if(L <= mid)
        update(L, R, c, lson);
    if(mid < R)
        update(L, R, c, rson);
    PushUp(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        return sum[rt];
    }
    PushDown(rt);
    int mid = (l+r) >> 1;
    int ret = 0;
    if(L <= mid)
        ret = max(ret,query(L, R, lson));
    if(mid < R)
        ret = max(ret,query(L, R, rson));
    return ret;
}
int main()
{
    int t;
    int K, Q;
    int cas = 0;
    scanf("%d",&t);
    while(t--)
    {
        memset(ans,0,sizeof(ans));
        scanf("%d%d",&K,&Q);
        int a, b;
        build(1, MAXN, 1);
        int l = 0;
        for(int i = 1; i <= Q; i++)
        {
            scanf("%d%d",&a,&b);
            b--;
            int tt = query(a, b, 1, MAXN, 1);
            if(tt < K)
            {
                ans[++l] = i;
                update(a, b, 1, 1, MAXN, 1);
            }
        }
        printf("Case %d:\n",++cas);
        //printf("%d",ans[1]);
        for(int i = 1; i <= l; i++)
        {
            printf("%d ",ans[i]);
        }
        printf("\n\n");
    }
    return 0;
}


相關推薦

HDU 3577 Fast Arrangement線段功能區間更新查詢區間覆蓋次數

Problem Description Chinese always have the railway tickets problem because of its' huge amount of passangers and stations. Now gove

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 ticket

Fast Arrangement 線段延遲標誌

down station its pan tick 完全 ket one ger 個人心得:線段樹的延遲標誌確實是減少了很多時間,思想比較簡單,但是實現得時候和建立延遲的時候比較麻煩。 按照我的一些理解,就是更新時找到完全覆蓋的區間時,更新延遲標誌,不再往下更新,但此時父節

Fast Arrangement 線段 維護區間值 lazy標記

Fast Arrangement 題意: 有一列火車同一時刻只能承載K個人,然後有Q個人要買票(給出Q組區間表示要買票的區間),(注意: 火車行駛區間為a-b的範圍),問那個人可以成功買到票(先到先得)

HDU1698 Just a Hook 區間更新查詢區間線段

Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15129 

HDU 4553 約會安排線段區間合並+雙重標記

stack 基本 pushd 一段 ret problem 代碼 () vector 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4553 題目大意:就是有三種操作:      ①DS x,安排一段長度為x的空閑時間跟屌

HDU 1540 Tunnel Warfare線段 區間合並

tle def ons main logs tmp 區間合並 http 合並 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 題意:n個村子,相鄰兩個村子初始都是聯通的,詢問經過一些操作後村子x最多和幾個村子聯通(包

hdu 6183 Color it 線段 動態開點

appears sub TP data- PE class pri operation count Do you like painting? Little D doesn‘t like painting, especially messy color paintings.

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 ticket

HDU 1540 Tunnel Warfare線段

題意:一條線上的點,D x是破壞這個點,Q x是表示查詢以x所在的最長的連續的點的個數,R是恢復上一次破壞的點。 分析:還是最大連續區間的板題,不過就是變成了單個節點進行更新。直接套用板子稍微更改就好, #include<cstdio> #include<cstring&g

2018 Multi-University Training Contest hdu 6315 Naive Operations線段

題意 有兩個數列a和b,a的初始值為0,b陣列是1~n的一個排列,有兩個操作:  1. 將a陣列區間[l,r]的每個數+1。  2. 輸出Σa[i]/b[i](l<=i<=r)Σa[i]/b[i](l<=i<=r)。 題解 由於運算元一共10^

hdu 6315 Naive Operations線段 加ok標記

題目連結 看大佬部落格:https://www.cnblogs.com/chenquanwei/p/9374234.html 題意:有兩個操作: 1:a陣列l到r的區間都加1; 2:計算l到r內a[i]/b[i]的值的和; 可以只用一個b陣列,每次進行1操作的時候就給相應位置減一

hdu 1166敵兵佈陣線段 模板 單點更新加區間查詢

#include<iostream> #include<cstdio> #include<cstring> #include<cstring> #include<algorithm> using namespac

2017多校訓練賽第一場 HDU 6039 Gear Up線段+並查集

Gear Up Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 59    Accepted Submiss

hdu Minimum Inversion Number線段求逆序數有關問題的一個小歸納

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904  

杭電多校第二場 1007 HDU-6315 Naive Operations線段

Naive Operations Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others) Total Submission(s): 820    Accepte

HDU 5316】Magician線段

一開始度錯題了,題意是求一段和最大的【子序列】,要求相鄰兩個元素的位置必須互為奇偶。 這樣我們可以使用線段樹維護4個值: 一段區間內開頭結尾元素為: 奇奇 奇偶 偶奇 偶偶 的最大值 之後在pushup的的時候根據題目所給的意思進行合併。 #include<cstdi

HDU 4358 Boring counting線段

用C++交會棧溢位,而G++不會。 更新和查詢我用的是線段樹,1500+ms,用樹狀陣列應該會快一些。 將樹形結構轉換成線性結構後,等價於求指定區間內恰好出現k次的數有多少個。 #include <iostream> #include <cstring&

hdu 3016 Man Down線段區間更新+dp

題意: 是男人就下100層相信很多人都玩過,這題就是簡單的模擬這個遊戲。 有n塊木板,每塊木板有4個屬性,高h(h>0),左邊界,右邊界,以及掉落在它上面,獲得多少生命值,一個人從最

線段 功能:update:單點替換 query:區間值 hdu1754

hud 1754   http://acm.hdu.edu.cn/showproblem.php?pid=1754 (0)定義 #include <cstdio>  #include <algorithm> using&