답안 #295535

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
295535 2020-09-09T18:13:48 Z Bruteforceman Potatoes and fertilizers (LMIO19_bulves) C++11
30 / 100
1000 ms 102136 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 10;
const long long inf = 1e16;
long long pre[maxn];
long long aux[maxn];
int a[maxn], b[maxn];

struct node {
  long long val, lazy;
  int index;
  node () { val = lazy = index = 0; }
  node (long long val) : val(val) { lazy = index = 0; }
  void add(long long x) {
    val += x;
    lazy += x;
  }
};
struct segtree {
  vector <node> t;
  int size;
  long long useless;
  function <long long(long long, long long)> func;
  void merge(node &cur, node l, node r) {
    cur.val = func(l.val, r.val);
    if(cur.val == r.val) cur.index = r.index;
    if(cur.val == l.val) cur.index = l.index;
  }
  void pushdown(int c) {
    int l = c << 1;
    int r = l + 1;
    t[l].add(t[c].lazy);
    t[r].add(t[c].lazy);
    t[c].lazy = 0;
  }
  void build(int c, int b, int e) {
    t[c].val = 0;
    t[c].lazy = 0;
    if(b == e) {
      t[c].index = b;
      return ;
    }
    int m = (b + e) >> 1;
    int l = c << 1;
    int r = l + 1;
    build(l, b, m);
    build(r, m + 1, e);
    merge(t[c], t[l], t[r]);
  }
  segtree(int size, long long useless, function <long long(long long, long long)> func) : size(size), useless(useless), func(func) {
    t.resize(4 * size + 1);
    build(1, 1, size);
  }
  void update(int x, int y, long long val, int c, int b, int e) {
    if(x <= b && e <= y) {
      t[c].add(val);
      return ;
    }
    if(y < b || e < x) return ;
    pushdown(c);
    int m = (b + e) >> 1;
    int l = c << 1;
    int r = l + 1;
    update(x, y, val, l, b, m);
    update(x, y, val, r, m + 1, e);
    merge(t[c], t[l], t[r]);
  }
  node query(int x, int y, int c, int b, int e) {
    if(x <= b && e <= y) return t[c];
    if(y < b || e < x) return node(useless);
    pushdown(c);
    int m = (b + e) >> 1;
    int l = c << 1;
    int r = l + 1;
    node ans;
    merge(ans, query(x, y, l, b, m), query(x, y, r, m + 1, e));
    return ans;
  }
  void update(int x, int y, long long val) {
    update(x, y, val, 1, 1, size);
  }
  pair <long long, int> query(int x, int y) {
    node ans = query(x, y, 1, 1, size);
    return make_pair(ans.val, ans.index);
  }
};

int main() {
  int n;
  cin >> n;
  for(int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    pre[i] = pre[i - 1] + a[i] - b[i];
  }
  segtree profit (n, -inf, [] (long long i, long long j) { return i < j ? j : i; } );
  segtree rmq (n, inf, [] (long long i, long long j) { return i < j ? i : j; } );
  int cnt = 0;
  for(int i = n; i >= 1; i--) {
    if(pre[i] > 0) {
      ++cnt;
      rmq.update(i, i, pre[i]);
    }
    else {
      --cnt;
      rmq.update(i, i, inf);
    }
    profit.update(i, i, cnt);
  }
  long long total = 0;
  while(pre[n] > total) {
    int id = profit.query(1, n).second;
    long long minVal = rmq.query(id, n).first;
    total += minVal;
    rmq.update(id, n, -minVal);
    aux[id] -= minVal;
    while(true) {
      auto var = rmq.query(1, n);
      if(var.first == 0) {
        rmq.update(var.second, var.second, inf);
        profit.update(1, var.second, -2);
      } else {
        break;
      }
    }
  }
  long long ans = 0;
  for(int i = 1; i <= n; i++) {
    aux[i] += aux[i - 1];
    ans += abs(pre[i] + aux[i]);
  }
  cout << ans << endl;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 6 ms 1024 KB Output is correct
4 Correct 87 ms 10872 KB Output is correct
5 Correct 178 ms 21496 KB Output is correct
6 Correct 583 ms 53164 KB Output is correct
7 Execution timed out 1108 ms 102136 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 6 ms 1024 KB Output is correct
4 Correct 87 ms 10872 KB Output is correct
5 Correct 178 ms 21496 KB Output is correct
6 Correct 583 ms 53164 KB Output is correct
7 Execution timed out 1108 ms 102136 KB Time limit exceeded
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 2 ms 512 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
6 Correct 7 ms 1024 KB Output is correct
7 Correct 4 ms 1024 KB Output is correct
8 Correct 5 ms 1024 KB Output is correct
9 Correct 4 ms 1024 KB Output is correct
10 Correct 5 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 6 ms 1024 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 7 ms 1024 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 5 ms 1024 KB Output is correct
10 Correct 4 ms 1024 KB Output is correct
11 Correct 6 ms 768 KB Output is correct
12 Correct 8 ms 1024 KB Output is correct
13 Correct 8 ms 1024 KB Output is correct
14 Correct 7 ms 1024 KB Output is correct
15 Correct 6 ms 1024 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 7 ms 1024 KB Output is correct
18 Correct 5 ms 1024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 6 ms 1024 KB Output is correct
3 Correct 6 ms 1024 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 2 ms 512 KB Output is correct
6 Correct 3 ms 640 KB Output is correct
7 Correct 7 ms 1024 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 5 ms 1024 KB Output is correct
10 Correct 4 ms 1024 KB Output is correct
11 Correct 87 ms 10872 KB Output is correct
12 Correct 178 ms 21496 KB Output is correct
13 Correct 583 ms 53164 KB Output is correct
14 Execution timed out 1108 ms 102136 KB Time limit exceeded
15 Halted 0 ms 0 KB -