工大培訓——day5 C題 線段樹變形應用

life4711發表於2014-11-24

        

題目大意:在外打工的小明,帶著父親的一張並沒有錢的信用卡,每次他花錢或者掙得錢時都要給父親寫信匯報,但是父親收到信的順序和他寄信的順序不同,每次接到信後,父親都要估算一下他信用卡已經透支多少了(本題的題意是小明只會花父親的錢,並不會把自己掙得的錢存到父親的卡里,但是他自己有錢,就不會繼續透支父親的卡)

解題思路:每次的寫信時間看做是一個時間點,其花的錢或者是掙得錢都只能更新該時間點之後的時間對應的錢數,用一個線段樹維護,最後查詢只有查詢根節點。時間卡的很緊,我用常規的線段樹寫超時了。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define ls (x<<1)
#define rs ((x<<1)|1)
const int N=111111;
int n,a[N],b[N],v[N];
long long lazy[N<<2],ans[N<<2];

void pushdown(int x)
{
    if(lazy[x])
    {
        lazy[ls]+=lazy[x],lazy[rs]+=lazy[x];
        ans[ls]+=lazy[x],ans[rs]+=lazy[x];
        lazy[x]=0;
    }
}
void pushup(int x)
{
    ans[x]=min(ans[ls],ans[rs]);
}
void insert(int x,int v,int l,int r,int loc)
{
    if(r<loc)
        return;
    if(loc<=l)
       lazy[x]+=v,ans[x]+=v;
    else if(l!=r)
    {
        pushdown(x);
        int mid=(l+r)/2;
        insert(ls,v,l,mid,loc);
        insert(rs,v,mid+1,r,loc);
        pushup(x);
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<n;i++)
    {
        int day,mouth,hour,minute;
        scanf("%d %d.%d %d:%d",v+i,&day,&mouth,&hour,&minute);
        a[i]=b[i]=((mouth*31+day)*24+hour)*60+minute;
    }
    sort(b,b+n);
    for(int i=0;i<n;i++)
    {
        int loc=lower_bound(b,b+n,a[i])-b;
        insert(1,v[i],0,n-1,loc);
        cout << min(0LL,ans[1])<<endl;
    }
    return 0;
}
/*
5
-1000 10.09 21:00
+500 09.09 14:00
+1000 02.09 00:00
-1000 17.09 21:00
+500 18.09 13:00
*/



相關文章