URAL 2014 Zhenya moves from parents 線段樹
阿新 • • 發佈:2017-07-10
ber ria class pad nds mean ural ubuntu lower
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.
After each received letter output what Zhenya’s father thinks the amount of the debt on the credit card is.
Problem Source: NEERC 2014, Eastern subregional contest
線段樹,前綴和最小
Time Limit: 1000MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
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.- The date when it took place
- The sum of earned or spent money
Input
Output
Sample Input
input | output |
---|---|
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 |
Source
Problem Author: Nick Burlakov (prepared by Olga Soboleva)Problem Source: NEERC 2014, Eastern subregional contest
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long int LL; const int maxn=110000; int n; LL money[maxn]; LL Time[maxn],T[maxn]; LL add[maxn<<2],mx[maxn<<2]; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 void build(int l,int r,int rt) { add[rt]=0,mx[rt]=0; if(l==r) return ; int m=(l+r)/2; build(lson); build(rson); } void upd_add(int rt) { if(add[rt]) { mx[rt<<1]+=add[rt]; mx[rt<<1|1]+=add[rt]; add[rt<<1]+=add[rt]; add[rt<<1|1]+=add[rt]; add[rt]=0; } } void push_down(int rt) { upd_add(rt); } void push_up(int rt) { mx[rt]=min(mx[rt<<1],mx[rt<<1|1]); } void update(int L,int R,int V,int l,int r,int rt) { if(L<=l&&r<=R) { add[rt]+=V; mx[rt]+=V; return ; } push_down(rt); int m=(l+r)/2; if(L<=m) update(L,R,V,lson); if(R>m) update(L,R,V,rson); push_up(rt); } int main() { scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { char op; LL mo; int day,mouth,hh,mm; scanf("%c%I64d %d.%d %d:%d",&op,&mo,&day,&mouth,&hh,&mm); money[i]=mo; if(op==‘-‘) money[i]*=-1; Time[i]=1LL*(mouth*31+day)*24*60+hh*60+mm; T[i]=Time[i]; } sort(T,T+n); int m=unique(T,T+n)-T; build(1,n,1); for(int i=0;i<n;i++) { int id=lower_bound(T,T+m,Time[i])-T+1; update(id,n,money[i],1,n,1); printf("%I64d\n",mx[1]>=0?0:mx[1]); } return 0; }
URAL 2014 Zhenya moves from parents 線段樹