URAL 2014 Zhenya moves from parents (线段树)

题意:儿子身无分文出去玩,只带了一张他爸的信用卡,当他自己现金不足的时候就会用信用卡支付,然后儿子还会挣钱,挣到的钱都是现金,

也就是说他如果有现金就会先花现金,但是有了现金他不会还信用卡的钱。他每花一次钱和挣一次钱都会给他爸发一条短信,告诉他挣/花的钱和时间,

但是给出的短信顺序时间可能不是按顺序来的,然后他爸要根据现有的短信信息推测信用卡现在的负债是多少。

析:我们可以把它们按时间点进行排序,然后用一棵线段树来维护最小值,然后就每次求全区间的最小值,每次更新从发现的时间到最后一个时间点。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}

LL addv[maxn<<2], minv[maxn<<2];

struct Node{
  int id, val, day, mon, hour, minute;
  bool operator < (const Node &p) const{
    if(mon != p.mon)  return mon < p.mon;
    if(day != p.day)  return day < p.day;
    if(hour != p.hour)  return hour < p.hour;
    return minute < p.minute;
  }
};
Node a[maxn];

bool cmp(const Node &lhs, const Node &rhs){
  return lhs.id < rhs.id;
}

void push_up(int rt){ minv[rt] = min(minv[rt<<1], minv[rt<<1|1]); }

void push_down(int rt){
  if(addv[rt]){
    int l = rt<<1, r = rt<<1|1;
    addv[l] += addv[rt];
    addv[r] += addv[rt];
    minv[l] += addv[rt];
    minv[r] += addv[rt];
    addv[rt] = 0;
  }
}

void update(int L, int R, int val, int l, int r, int rt){
  if(L <= l && r <= R){
    addv[rt] += val;
    minv[rt] += val;
    return ;
  }
  push_down(rt);
  int m = l+r >> 1;
  if(L <= m)  update(L, R, val, lson);
  if(R > m)   update(L, R, val, rson);
  push_up(rt);
}
map<int, int> mp;

int main(){
  scanf("%d", &n);
  for(int i = 1; i <= n; ++i){
    scanf("%d %d.%d %d:%d", &a[i].val, &a[i].day, &a[i].mon, &a[i].hour, &a[i].minute);
    a[i].id = i;
  }
  sort(a + 1, a + n + 1);
  for(int i = 1; i <= n; ++i)  mp[a[i].id] = i;
  sort(a + 1, a + n + 1, cmp);
  memset(addv, 0, sizeof addv);
  memset(minv, 0, sizeof minv);

  for(int i = 1; i <= n; ++i){
    update(mp[i], n, a[i].val, 1, n, 1);
    printf("%I64d
", min(0LL, minv[1]));
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/6793515.html