C - Zhenya moves from parents URAL - 2014 (线段树+离散化)

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 条信息信息推出的小明所欠最大的债务

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


//Full of love and hope for life
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <math.h>
#define inf 0x3f3f3f3f
#define lson (num<<1)
#define rson (num<<1|1)
#define mid ((l+r)>>1)
//https://paste.ubuntu.com/

using namespace std;

typedef long long ll;
const int N=1e5+100;

struct node{
   ll mon,cre;
}tree[4*N];

struct node1{
   ll id,data,time;
}n[N];

bool cmp1(node1 a,node1 b){
   return a.time<b.time;
}

bool cmp2(node1 a,node1 b){
   return a.id<b.id;
}

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

void add(ll l,ll r,ll num,ll po,ll 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(){
    ll a,b,d,h,mi;
    scanf("%lld",&a);
    for(ll i=1;i<=a;i++){
        scanf("%lld %lld.%lld %lld:%lld",&n[i].data,&b,&d,&h,&mi);
        n[i].time=(b+d*31)*24*60+(h*60+mi);
        n[i].id=i;
    }
    sort(n+1,n+a+1,cmp1);
    for(ll i=1;i<=a;i++){
        n[i].time=i;
    }
    sort(n+1,n+a+1,cmp2);
    built(1,a,1);
    for(ll i=1;i<=a;i++){
        add(1,a,1,n[i].time,n[i].data);
        printf("%lld\n",tree[1].cre);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值