1. 程式人生 > >HDU 4614 Vases and Flowers (二分查詢+線段樹區間更新)

HDU 4614 Vases and Flowers (二分查詢+線段樹區間更新)

  Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers, she will try to put them in the vases, one flower in one vase. She randomly choose the vase A and try to put a flower in the vase. If the there is no flower in the vase, she will put a flower in it, otherwise she skip this vase. And then she will try put in the vase A+1, A+2, ..., N-1, until there is no flower left or she has tried the vase N-1. The left flowers will be discarded. Of course, sometimes she will clean the vases. Because there are too many vases, she randomly choose to clean the vases numbered from A to B(A <= B). The flowers in the cleaned vases will be discarded. Input  The first line contains an integer T, indicating the number of test cases. 
  For each test case, the first line contains two integers N(1 < N < 50001) and M(1 < M < 50001). N is the number of vases, and M is the operations of Alice. Each of the next M lines contains three integers. The first integer of one line is K(1 or 2). If K is 1, then two integers A and F follow. It means Alice receive F flowers and try to put a flower in the vase A first. If K is 2, then two integers A and B follow. It means the owner would like to clean the vases numbered from A to B(A <= B). Output  For each operation of which K is 1, output the position of the vase in which Alice put the first flower and last one, separated by a blank. If she can not put any one, then output 'Can not put any one.'. For each operation of which K is 2, output the number of discarded flowers. 
   Output one blank line after each test case.
Sample Input
2
10 5
1 3 5
2 4 5
1 1 8
2 3 6
1 8 8
10 6
1 2 5
2 3 4
1 0 8
2 2 5
1 4 4
1 2 3
Sample Output
3 7
2
1 9
4
Can not put any one.

2 6
2
0 9
4
4 5
2 3


題解:

題意就是有n個花瓶,一開始全空,然後又m次操作,如果第一個輸入的為1,然後輸入兩個數x,y,就是將x,y區間內插花,如果有花了就跳掉往後插,結束的條件是插到末尾了或者插夠了數量,如果一個都不能插就罵人qwq,如果輸入2,輸入x,y就是將x,y內的花清空

這題思路就是普通的lazy tag區間更新+二分查詢合適的位置插

程式碼:

#include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<math.h>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
using namespace std;
struct node
{
    int l,r;
    int tag;//如果為-1則為初始狀態,如果為0意思將子區間內的花清空,為1為插滿花
    int v;//記錄區間內插了多少花
}t[50005*4];
int n;
void Build(int l,int r,int k)
{
    t[k].l=l;
    t[k].r=r;
    t[k].v=0;
    t[k].tag=-1;
    if(l==r)
        return;
    int mid=(l+r)/2;
    Build(l,mid,k*2);
    Build(mid+1,r,k*2+1);
}
void pushdown(int k)//向下傳遞狀態
{
    t[k*2].tag=t[k*2+1].tag=t[k].tag;
    t[k*2].v=t[k].tag*(t[k*2].r-t[k*2].l+1);
    t[k*2+1].v=t[k].tag*(t[k*2+1].r-t[k*2+1].l+1);
    t[k].tag=-1;
}
void update(int l,int r,int v,int k)
{
    if(t[k].l==l&&t[k].r==r)
    {
        t[k].tag=v;
        t[k].v=v*(r-l+1);//插滿或者清空
        return;
    }
    if(t[k].tag!=-1)//要傳遞狀態
        pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        update(l,r,v,k*2);
    else if(l>mid)
        update(l,r,v,k*2+1);
    else
    {
        update(l,mid,v,k*2);
        update(mid+1,r,v,k*2+1);
    }
    t[k].v=t[k*2].v+t[k*2+1].v;//向上傳遞區間資訊
}
int query(int l,int r,int k)//查詢區間內插花的數目
{
    if(t[k].l==l&&t[k].r==r)
    {
        return t[k].v;
    }
    if(t[k].tag!=-1)//同樣lazy tag
        pushdown(k);
    int mid=(t[k].l+t[k].r)/2;
    if(r<=mid)
        return query(l,r,k*2);
    else if(l>mid)
        return query(l,r,k*2+1);
    else
    {
        return query(l,mid,k*2)+query(mid+1,r,k*2+1);
    }
    t[k].v=t[k*2].v+t[k*2+1].v;
}
int dive(int s,int num)//二分查詢,第一個為開始的位置,第二個引數為要插多少個
{
    int temp=query(s,n,1);
    if(temp==n-s+1)//如果一個都不能插
        return -1;
    if(n-s+1-temp<num)//如果空位小於要插的數目,改下要插的數目
        num=n-s+1-temp;
    int l=s;
    int r=n;
    int mid,d;
    int f=-1;
    while(r>=l)//二分
    {
        mid=(l+r)/2;
        d=mid-s+1-query(s,mid,1);//d為從s到mid的空位數目
        if(d>num)
        {
            r=mid-1;
        }
        else if(d<num)
            l=mid+1;
        else//如果空位的數目正好要一個個減
        {
            f=mid;//為了儲存第一個能夠滿足該數目空位的位置
            r=mid-1;
        }
    }
    return f;
}
int main()
{
    int i,j,test,x,y,d,m;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d%d",&n,&m);
        n--;
        Build(0,n,1);
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&d,&x,&y);
            if(d==1)
            {
                int temp=dive(x,1);//查詢插的第一個位置
                if(temp==-1)
                {
                    printf("Can not put any one.\n");
                }
                else
                {
                    int en=dive(x,y);//查詢插的第二個位置
                    printf("%d %d\n",temp,en);
                    update(temp,en,1,1);//更新插滿區間
                }
            }
            else
            {
                printf("%d\n",query(x,y,1));
                update(x,y,0,1);//清空區間
            }
        }
        printf("\n");
    }
    return 0;
}


相關推薦

HDU 4614 Vases and Flowers 二分查詢+線段區間更新

  Alice is so popular that she can receive many flowers everyday. She has N vases numbered from 0 to N-1. When she receive some flowers

HDU 4614 Vases and Flowers線段+二分

Vases and FlowersTime Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1336    Accepted

HDU 4614 Vases and Flowers 線段+二分

too others tput sometimes 標記傳遞 scan mean 二分 set 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 題意:N個花瓶,兩種操作。 操作1:從第a個花瓶開始放花,放最多f個,如果

HDU-6318 Swaps and Inversions求逆序數+狀陣列

Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth

HDU 5649 DZY Loves Sorting二分答案+線段線段合併+線段分割

題意 一個 \(1\) 到 \(n\) 的全排列,\(m\) 種操作,每次將一段區間 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作後的第 \(k\) 位。 \(1 \leq n \leq 10^5\) 思路 兩個 \(\log\) 的做法展現了二分答案的強大功能。首先二分列舉第 \(k

HDU 5649 DZY Loves Sorting二分答案+線段線段合並+線段分割

空間 namespace memset ons \n create name 題意 size 題意 一個 \(1\) 到 \(n\) 的全排列,\(m\) 種操作,每次將一段區間 \([l,r]\) 按升序或降序排列,求 \(m\) 次操作後的第 \(k\) 位。 \(1

HDU 1832 Luck and Love 二維線段

Problem Description 世界上上最遠的距離不是相隔天涯海角 而是我在你面前 可你卻不知道我愛你                 ―― 張小嫻 前段日子,楓冰葉子給Wiskey做了個徵婚啟事,聘禮達到500萬哦,天哪,可是天文數字了啊,不知多少MM蜂擁而至,頓

HDU 1698 Just a Hook線段-區間更新

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sti

HDU 1698 Just a Hook線段區間更新

題意: 屠夫是Dota中一個令所有英雄聞風喪膽的英雄。他有一個很長的鉤子,這個鉤子是用銅做的(剛剛開始都是1),現在他想要更改這些鉤子,把某個區間的鉤子改為金、銀或銅。 輸入 L, R,

hdu 1698 Just a Hook線段——區間更新

Just a Hook Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes

Just a Hook hdu 1698 線段區間更新

Just a Hook Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Problem Description In the game of DotA

hdu 4027 Can you answer these queries?線段——區間更新思路

Can you answer these queries? Problem Description A lot of battleships of evil are arranged in a line before the battle. Our comm

HDU 4027 線段區間更新

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 24587    Accepted Submission(s)

ZOJ - 1610 Count the Colors線段區間更新

線段樹 pan 遍歷 cst include stdin cstring syn bit https://cn.vjudge.net/problem/ZOJ-1610 題意 給一個n,代表n次操作,接下來每次操作表示把[l,r]區間的線段塗成k的顏色其中,l,r,k的範

POJ - 2528 Mayor's posters 離散化+線段區間修改

clu max 單位 cover rst unique ace output https https://cn.vjudge.net/problem/POJ-2528 題意 給定一些海報,可能相互重疊,告訴你每個海報的寬度(高度都一樣的)和先後疊放順序,問沒有被完全蓋

A Simple Problem with Integers線段區間更新

【題意】 給定長度為 NNN 的序列 AAA,有 QQQ次操作,操作分兩種 CabcC \ a \ b \ cCabc 表示將序列中 A[a,b]A[a,b]A[a,b]所有元素都加上一個數字 ccc QabQ \ a \ bQab 表示查詢序列 A[a,b]

URAL - 1989 Subpalindromes 字串hash + 線段區間合併

題目連結:http://acm.timus.ru/problem.aspx?space=1&num=1989 題目大意:給出一個長度為n的字串S,接下來進行n次操作。操作分為修改和查詢兩種,每次修改操作給出一個整數 i 和一個字元c,表示將第 i 位的字元變成字元c;每次查詢操作給出兩個

1698 Just a Hook 線段 區間更新

In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive metallic sti

HDU 1698 Just a Hook (線段區間更新

Problem Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made

POJ 1698 Just a Hook線段 區間更新

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