Hdu 4967 Handling the Past (線段樹)

來源:互聯網
上載者:User

標籤:os   io   for   ar   問題   amp   時間   size   

題目大意:

關於網路阻塞命令延遲的處理。

命令就分為三種對棧的處理。

但是如果接收到一個操作,它後面的操作都要先取消不做,再做這個操作,再做之前取消了的操作。


思路分析:

題目也就轉化成了,給出一個時間t接收到peak操作,找到第一個最大的 l ,使得 sum[l - t] > 0...

然後的問題我們就是如何確定最大的l。

我們記錄sum的同時再記錄一個右邊最大。

然後我們在查詢的時候,就通過右邊最大和sum ,確定區間。

當s==e的時候就是所求的。


#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define maxn 50005using namespace std;typedef long long ll;struct node{    char type;    int t,val;}save[maxn];int res[maxn<<2];int rsum[maxn<<2];int id[maxn];int n;void pushup(int num){    res[num]=res[num<<1]+res[num<<1|1];    rsum[num]=max(rsum[num<<1|1],res[num<<1|1]+rsum[num<<1]);}void update(int num,int s,int e,int pos,int val,int tid){    if(s==e)    {        res[num]+=val;        rsum[num]+=val;        id[s]=tid;        if(val==-1)id[s]=0;        return;    }    int mid=(s+e)>>1;    if(pos<=mid)update(lson,pos,val,tid);    else update(rson,pos,val,tid);    pushup(num);}int query(int num,int s,int e,int l,int r){    if(l<=s && r>=e)        return res[num];    int mid=(s+e)>>1;    if(r<=mid)return query(lson,l,r);    else if(l>mid)return query(rson,l,r);    else return query(lson,l,mid)+query(rson,mid+1,r);}int peak(int num,int s,int e,int l,int r,int t){    if(l<=s && r>=e)    {        if(s==e)        {            return id[s];        }        else {            int mid=(s+e)>>1;            if(rsum[num<<1|1]>t)            {                int tmp = peak(rson,l,r,t);                if(tmp)return tmp;            }            else if(rsum[num<<1]>t-res[num<<1|1]){                int tmp = peak(lson,l,r,t-res[num<<1|1]);                if(tmp)return tmp;            }        }        return 0;    }    int mid=(s+e)>>1;    if(r<=mid)    {        int tmp = peak(lson,l,r,t);        if(tmp)return tmp;    }    else if(l>mid){        int tmp = peak(rson,l,r,t);        if(tmp)return tmp;    }    else {        int tmp = peak(rson,mid+1,r,t);        if(tmp)return tmp;        tmp = peak(lson,l,mid,t-query(1,1,n,mid+1,r));        if(tmp)return tmp;    }}int x[maxn];int main(){    int CASE=1;    while(scanf("%d",&n)!=EOF && n)    {        memset(res,0,sizeof res);        memset(rsum,0,sizeof rsum);        memset(id,0,sizeof id);        printf("Case #%d:\n",CASE++);        char str[10];        for(int i=1;i<=n;i++)        {            int v,time;            scanf("%s",str);            if(str[1]=='u')scanf("%d%d",&v,&time);            else scanf("%d",&time);            save[i].type=str[1];save[i].val=v;save[i].t=time;            x[i]=save[i].t;        }        sort(x+1,x+1+n);        for(int i=1;i<=n;i++)            save[i].t=lower_bound(x+1,x+1+n,save[i].t)-x;        for(int i=1;i<=n;i++)        {            if(save[i].type=='u')                update(1,1,n,save[i].t,1,i);            else if(save[i].type=='o')                update(1,1,n,save[i].t,-1,i);            else {                int pos = save[i].t-1;                if(pos<1)puts("-1");                else {                    if(query(1,1,n,1,pos)>0)                    {                        printf("%d\n",save[peak(1,1,n,1,pos,0)].val);                    }                    else puts("-1");                }            }        }    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.