题目大意:在外打工的小明,带着父亲的一张并没有钱的信用卡,每次他花钱或者挣得钱时都要给父亲写信汇报,但是父亲收到信的顺序和他寄信的顺序不同,每次接到信后,父亲都要估算一下他信用卡已经透支多少了(本题的题意是小明只会花父亲的钱,并不会把自己挣得的钱存到父亲的卡里,但是他自己有钱,就不会继续透支父亲的卡)
解题思路:每次的写信时间看做是一个时间点,其花的钱或者是挣得钱都只能更新该时间点之后的时间对应的钱数,用一个线段树维护,最后查询只有查询根节点。时间卡的很紧,我用常规的线段树写超时了。
#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
*/