Submission #295537

# Submission time Handle Problem Language Result Execution time Memory
295537 2020-09-09T18:15:46 Z Bruteforceman Potatoes and fertilizers (LMIO19_bulves) C++11
64 / 100
1000 ms 109048 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;
  scanf("%d", &n);
  for(int i = 1; i <= n; i++) {
    scanf("%d %d", &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]);
  }
  printf("%lld\n", ans);
}

Compilation message

bulves.cpp: In function 'int main()':
bulves.cpp:90:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   90 |   scanf("%d", &n);
      |   ~~~~~^~~~~~~~~~
bulves.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   92 |     scanf("%d %d", &a[i], &b[i]);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 4 ms 1024 KB Output is correct
4 Correct 61 ms 10944 KB Output is correct
5 Correct 130 ms 21496 KB Output is correct
6 Correct 353 ms 53240 KB Output is correct
7 Correct 748 ms 106080 KB Output is correct
8 Correct 696 ms 105972 KB Output is correct
9 Correct 706 ms 106104 KB Output is correct
10 Correct 680 ms 106104 KB Output is correct
11 Correct 677 ms 105976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 4 ms 1024 KB Output is correct
4 Correct 61 ms 10944 KB Output is correct
5 Correct 130 ms 21496 KB Output is correct
6 Correct 353 ms 53240 KB Output is correct
7 Correct 748 ms 106080 KB Output is correct
8 Correct 696 ms 105972 KB Output is correct
9 Correct 706 ms 106104 KB Output is correct
10 Correct 680 ms 106104 KB Output is correct
11 Correct 677 ms 105976 KB Output is correct
12 Correct 169 ms 26744 KB Output is correct
13 Correct 459 ms 63736 KB Output is correct
14 Correct 736 ms 105980 KB Output is correct
15 Correct 686 ms 105976 KB Output is correct
16 Correct 707 ms 106104 KB Output is correct
17 Correct 818 ms 106232 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 0 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 5 ms 1024 KB Output is correct
7 Correct 4 ms 1024 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 3 ms 928 KB Output is correct
10 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 4 ms 1024 KB Output is correct
4 Correct 0 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 5 ms 1024 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 4 ms 1024 KB Output is correct
10 Correct 3 ms 928 KB Output is correct
11 Correct 4 ms 768 KB Output is correct
12 Correct 7 ms 1024 KB Output is correct
13 Correct 5 ms 928 KB Output is correct
14 Correct 6 ms 1024 KB Output is correct
15 Correct 4 ms 1024 KB Output is correct
16 Correct 6 ms 1024 KB Output is correct
17 Correct 4 ms 1024 KB Output is correct
18 Correct 4 ms 1024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 384 KB Output is correct
2 Correct 3 ms 1024 KB Output is correct
3 Correct 4 ms 1024 KB Output is correct
4 Correct 0 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 5 ms 1024 KB Output is correct
8 Correct 4 ms 1024 KB Output is correct
9 Correct 4 ms 1024 KB Output is correct
10 Correct 3 ms 928 KB Output is correct
11 Correct 61 ms 10944 KB Output is correct
12 Correct 130 ms 21496 KB Output is correct
13 Correct 353 ms 53240 KB Output is correct
14 Correct 748 ms 106080 KB Output is correct
15 Correct 696 ms 105972 KB Output is correct
16 Correct 706 ms 106104 KB Output is correct
17 Correct 680 ms 106104 KB Output is correct
18 Correct 677 ms 105976 KB Output is correct
19 Correct 169 ms 26744 KB Output is correct
20 Correct 459 ms 63736 KB Output is correct
21 Correct 736 ms 105980 KB Output is correct
22 Correct 686 ms 105976 KB Output is correct
23 Correct 707 ms 106104 KB Output is correct
24 Correct 818 ms 106232 KB Output is correct
25 Correct 4 ms 768 KB Output is correct
26 Correct 7 ms 1024 KB Output is correct
27 Correct 5 ms 928 KB Output is correct
28 Correct 6 ms 1024 KB Output is correct
29 Correct 4 ms 1024 KB Output is correct
30 Correct 6 ms 1024 KB Output is correct
31 Correct 4 ms 1024 KB Output is correct
32 Correct 280 ms 26872 KB Output is correct
33 Correct 716 ms 67832 KB Output is correct
34 Execution timed out 1097 ms 109048 KB Time limit exceeded
35 Halted 0 ms 0 KB -