Submission #435803

# Submission time Handle Problem Language Result Execution time Memory
435803 2021-06-23T18:08:49 Z bicsi Distributing Candies (IOI21_candies) C++17
100 / 100
4699 ms 490424 KB
#include "candies.h"
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int bs = 333;
 
vector<int> distribute_candies(
    vector<int> c, vector<int> l,
    vector<int> r, vector<int> v) {
  int n = c.size();
 
  vector<int> f(n, 0), nf(n, 0);
  vector<vector<int>> ops(n / bs + 1);
  vector<int> order(n);
  iota(order.begin(), order.end(), 0);
  for (int i = 0; i < n; i += bs) {
    int j = min(i + bs, n);
    sort(order.begin() + i, order.begin() + j, [&](int a, int b) {
      return c[a] < c[b];
    });
  }
  vector<ll> stk;
  auto refresh = [&](int bi) {
    if (ops[bi].empty()) return;
 
    // cerr << "refresh: " << bi << endl;
    // cerr << "ops: ";
    // for (auto x : ops[bi]) cerr << x << " "; cerr << endl;
    stk.clear();
    stk.push_back(4e18);
    stk.push_back(2e18);
    for (auto delta : ops[bi]) {
      if (delta == 0) continue;
      if (delta > 0) {
        ll rem = delta, px = 0;
        while (true) {
          ll x = stk.back();
          if (x - px > rem) {
            stk.push_back(px + rem);
            stk.push_back(0);
            break;
          } 
          rem -= (x - px);
          stk.pop_back();
          px = stk.back();
          stk.pop_back();
        }
      } else {
        ll rem = -delta;
        while (true) {
          ll px = stk.back(); stk.pop_back();
          ll x = stk.back();
          if (x - px > rem) {
            stk.push_back(px + rem);
            break;
          }
          rem -= (x - px);
          stk.pop_back();
        }
      }
    }
    
    stk.push_back(0);
    stk.push_back(0);
    reverse(stk.begin(), stk.end());
    // cerr << "stk: "; for (auto x : stk) cerr << x << " "; cerr << endl;
 
    ll value = 0;
    int ptr = 0;
    for (int i = bi * bs; i < (bi + 1) * bs && i < n; ++i) {
      ll ci = c[order[i]];
      while (stk[ptr + 1] < ci) {
        if (ptr % 2 == 0)
          value += stk[ptr + 1] - stk[ptr];
        ++ptr;
      }
      if (ptr % 2 == 0) {
        nf[order[i]] = value + (ci - stk[ptr]);
      } else {
        nf[order[i]] = value;
      }
      // cerr << "  " << order[i] << " => " << nf[order[i]] << endl;
    }
 
    ll mind = 0, maxd = 0, nowd = 0;
    for (auto x : ops[bi]) {
      nowd += x;
      mind = min(mind, nowd);
      maxd = max(maxd, nowd);
    }
 
    for (int i = bi * bs; i < (bi + 1) * bs && i < n; ++i) {
      ll hi = min(c[i] - maxd, (ll)f[i]);
      ll lo = -mind;
      if (lo < hi) 
        nf[i] += hi - lo; 
    }
 
 
    for (int i = bi * bs; i < (bi + 1) * bs && i < n; ++i) 
      f[i] = nf[i];
 
    // for (int i = 0; i < n; ++i) cerr << f[i] << " "; cerr << endl;
 
    ops[bi].clear();
  };
 
  int q = l.size();
  for (int i = 0; i < q; ++i) {
    int lf = l[i], rt = r[i];
    int bl = l[i] / bs, br = r[i] / bs;
    refresh(bl); refresh(br); 
    for (int j = l[i]; j <= r[i]; ) {
      if (j % bs == 0 && (j + bs - 1) <= r[i]) {
        ops[j / bs].push_back(v[i]);
        j += bs;
      } else {
        f[j] = max(0, min(f[j] + v[i], c[j]));
        j += 1;
      }
    }
  }
 
  for (int i = 0; i < n; i += bs)
    refresh(i / bs);
  return f;
}

Compilation message

candies.cpp: In function 'std::vector<int> distribute_candies(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
candies.cpp:111:9: warning: unused variable 'lf' [-Wunused-variable]
  111 |     int lf = l[i], rt = r[i];
      |         ^~
candies.cpp:111:20: warning: unused variable 'rt' [-Wunused-variable]
  111 |     int lf = l[i], rt = r[i];
      |                    ^~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3558 ms 11832 KB Output is correct
2 Correct 4019 ms 11620 KB Output is correct
3 Correct 3961 ms 11620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1033 ms 5036 KB Output is correct
3 Correct 102 ms 5268 KB Output is correct
4 Correct 3500 ms 11628 KB Output is correct
5 Correct 3493 ms 11716 KB Output is correct
6 Correct 3069 ms 11808 KB Output is correct
7 Correct 2933 ms 11692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1388 ms 9432 KB Output is correct
4 Correct 102 ms 9128 KB Output is correct
5 Correct 4699 ms 490256 KB Output is correct
6 Correct 4697 ms 490236 KB Output is correct
7 Correct 4493 ms 490344 KB Output is correct
8 Correct 4614 ms 490424 KB Output is correct
9 Correct 3793 ms 490404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 12 ms 332 KB Output is correct
6 Correct 3558 ms 11832 KB Output is correct
7 Correct 4019 ms 11620 KB Output is correct
8 Correct 3961 ms 11620 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1033 ms 5036 KB Output is correct
11 Correct 102 ms 5268 KB Output is correct
12 Correct 3500 ms 11628 KB Output is correct
13 Correct 3493 ms 11716 KB Output is correct
14 Correct 3069 ms 11808 KB Output is correct
15 Correct 2933 ms 11692 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1388 ms 9432 KB Output is correct
19 Correct 102 ms 9128 KB Output is correct
20 Correct 4699 ms 490256 KB Output is correct
21 Correct 4697 ms 490236 KB Output is correct
22 Correct 4493 ms 490344 KB Output is correct
23 Correct 4614 ms 490424 KB Output is correct
24 Correct 3793 ms 490404 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 121 ms 5296 KB Output is correct
27 Correct 1141 ms 5032 KB Output is correct
28 Correct 4043 ms 11780 KB Output is correct
29 Correct 3851 ms 12164 KB Output is correct
30 Correct 3595 ms 11780 KB Output is correct
31 Correct 3276 ms 11676 KB Output is correct
32 Correct 3145 ms 11796 KB Output is correct