URAL - 2014  Zhenya moves from parents (线段树)

Zhenya moved from his parents’ home to study in other city. He didn’t take any cash with him, he only took his father’s credit card with zero balance on it. Zhenya succeeds in studies at the University and sometimes makes a little money on the side as a Maths tutor. As he makes his own money he spends only it, and when it is over he uses the credit card. Every time he gets or spends money, he sends a letter to his father, where he puts the following two things.

  1. The date when it took place
  2. The sum of earned or spent money

Every time receiving a letter from Zhenya, his father calculates the debt on the credit card at the moment. But here a problem arises. The point is that Russian Post delivers letters in an order different to the one they were sent in.

For example, in the first Zhenya’s letter the father read that on September 10 Zhenya spent one thousand rubles. He thought that his son had used the credit card, and now the debt is one thousand rubles. However the next day came a letter with the information that on September 9 Zhenya earned five hundred rubles. It means that half of the money he spent on September 10 was his own, and the debt on the credit card is just five hundred rubles.

Help Zhenya’s father with his account management.

Input

The first line contains an integer n which is the number of Zhenya’s letters (1 ≤ n ≤ 100 000). These letters are listed in the next n lines. Description of each letter consists of the amount of money Zhenya spent or earned (in the form -c or +c accordingly, where c is an integer, 1 ≤ c ≤ 50 000) followed by both date and time when it took place (in the form of dd.MM hh:mm). All dates belong to the same year, which is not leap (i. e. there are 365 days in it). Any two letters contain either different dates or different time. The letters are listed in the order the father received them.

Output

After each received letter output what Zhenya’s father thinks the amount of the debt on the credit card is.

Example

inputoutput
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
-1000
-500
0
-500
-500

 题目大意:小明拿着父亲的信用卡在国外,小明有时挣钱,有时花钱,钱不够就会花信用卡的,但它不会还信用卡的债务,小明每当挣钱或花钱时,都会发一条信息给父亲,但是信息的时间被打乱。

求出以前 i 条信息信息推出的小明所欠最大的债务

用线段树,树的每个叶子节点是按照的时间顺序所对应的钱的信息,先建一棵空树,按照输入顺序每次添加一个点,就可以根据挣钱还钱的顺序计算求和,向上传递信息最后在根节点得到答案(欠债之前挣的钱可以还钱,欠债之后的钱不可以还)

#include<bits/stdc++.h>
using namespace std;
#define lson (num<<1)
#define rson (num<<1|1)
#define mid ((l+r)>>1)
struct node
{
    long long mon,cre;
} tree[400090];

struct node2
{
    long long id,data,time;
} a[100020];

bool cmp1(node2 a, node2 b)
{
    return a.time < b.time;
}
bool cmp2(node2 a, node2 b)
{
    return a.id < b.id;
}

void build(long long l, long long r, long long num)
{
    if(l == r)
    {
        tree[num].mon = tree[num].cre = 0;
        return ;
    }
    build(l,mid,lson);
    build(mid+1,r,rson);
    tree[num].mon = tree[num].cre = 0;
}

void add(long long l, long long r, long long num,long long po,long long data)
{
    if(l == r)
    {
        if(data < 0)
            tree[num].cre = data;
        else
            tree[num].mon = data;
        return ;
    }
    if(po <= mid)
        add(l,mid,lson,po,data);
    else
        add(mid+1,r,rson,po,data);
        
    if(tree[lson].mon + tree[rson].cre >= 0)//左儿子的mon可以还右儿子的cre
    {
        tree[num].cre = tree[lson].cre;
        tree[num].mon = tree[lson].mon+tree[rson].mon+ tree[rson].cre;
    }
    else
    {
        tree[num].cre = tree[lson].cre +tree[lson].mon + tree[rson].cre;
        tree[num].mon = tree[rson].mon;
    }


}
int main()
{

    long long i,n,m,d,h,mi;
    scanf("%lld",&n);
    for(i = 1; i <= n; i ++)
    {
        scanf("%lld %lld.%lld %lld:%lld",&a[i].data,&m,&d,&h,&mi);
        a[i].time = (m+d*31)*24*60+(h*60+mi);
        a[i].id = i;
    }
    sort(a+1,a+1+n,cmp1);
    for(i = 1; i <= n; i ++)
    {
        a[i].time = i;
    }
    sort(a+1,a+n+1,cmp2);
    build(1,n,1);

    for(i = 1; i <= n; i ++)
    {
        add(1,n,1,a[i].time,a[i].data);
        printf("%lld\n",tree[1].cre);
    }

    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值