답안 #435856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
435856 2021-06-23T19:53:13 Z bicsi 사탕 분배 (IOI21_candies) C++17
100 / 100
4421 ms 10184 KB
#include "candies.h"
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int BS = 333;
 
 
vector<ll> compute_function(const vector<int>& ops) {
  vector<ll> stk;
  stk.push_back(4e18);
  stk.push_back(2e18);
  for (auto delta : ops) {
    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());
  return stk;
}
 
vector<int> distribute_candies(
    vector<int> c, vector<int> l,
    vector<int> r, vector<int> v) {
  int n = c.size();
 
  
  // Sort each bucket increasingly by c[i] to facilitate easy evaluation of f.
  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;
  vector<int> a(n, 0), na(n, 0);
  vector<vector<int>> ops(n / BS + 1);
 
  auto commit = [&](int bi) {
    if (ops[bi].empty()) return;
    int l = bi * BS, r = min(n, (bi + 1) * BS);
 
    // compute f(.) and evaluate it in all c[i] of this bucket
    auto stk = compute_function(ops[bi]);
    ll value = 0;
    int ptr = 0;
    for (int i = l; i < r; ++i) {
      int ci = c[order[i]];
      while (stk[ptr + 1] < ci) {
        if (ptr % 2 == 0)
          value += stk[ptr + 1] - stk[ptr];
        ++ptr;
      }
      if (ptr % 2 == 0) {
        na[order[i]] = value + (ci - stk[ptr]);
      } else {
        na[order[i]] = value;
      }
    }
  
    // Adapt for non-zero initial values.
    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 = l; i < r; ++i) {
      ll hi = min(c[i] - maxd, (ll)a[i]);
      ll lo = -mind;
      if (lo < hi) 
        na[i] += hi - lo; 
    }
 
    // Housekeeping.
    for (int i = l; i < r; ++i) 
      a[i] = na[i];
    ops[bi].clear();
  };
 
  int q = l.size();
  for (int i = 0; i < q; ++i) {
    int bl = l[i] / BS, br = r[i] / BS;
    commit(bl); commit(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 {
        a[j] = max(0, min(a[j] + v[i], c[j]));
        j += 1;
      }
    }
    if (i % BS == 0) 
      for (int i = 0; i < n; i += BS)
        commit(i / BS);
  }
 
  for (int i = 0; i < n; i += BS)
    commit(i / BS);
  return a;
}
# 결과 실행 시간 메모리 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 8 ms 372 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3464 ms 9576 KB Output is correct
2 Correct 3843 ms 9596 KB Output is correct
3 Correct 3540 ms 9668 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 588 ms 5032 KB Output is correct
3 Correct 105 ms 4676 KB Output is correct
4 Correct 2685 ms 9496 KB Output is correct
5 Correct 2791 ms 9596 KB Output is correct
6 Correct 2794 ms 9480 KB Output is correct
7 Correct 2713 ms 9592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 694 ms 5052 KB Output is correct
4 Correct 106 ms 5504 KB Output is correct
5 Correct 3638 ms 10176 KB Output is correct
6 Correct 3445 ms 10180 KB Output is correct
7 Correct 2991 ms 10176 KB Output is correct
8 Correct 3681 ms 10184 KB Output is correct
9 Correct 3351 ms 10176 KB Output is correct
# 결과 실행 시간 메모리 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 8 ms 372 KB Output is correct
6 Correct 3464 ms 9576 KB Output is correct
7 Correct 3843 ms 9596 KB Output is correct
8 Correct 3540 ms 9668 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 588 ms 5032 KB Output is correct
11 Correct 105 ms 4676 KB Output is correct
12 Correct 2685 ms 9496 KB Output is correct
13 Correct 2791 ms 9596 KB Output is correct
14 Correct 2794 ms 9480 KB Output is correct
15 Correct 2713 ms 9592 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 694 ms 5052 KB Output is correct
19 Correct 106 ms 5504 KB Output is correct
20 Correct 3638 ms 10176 KB Output is correct
21 Correct 3445 ms 10180 KB Output is correct
22 Correct 2991 ms 10176 KB Output is correct
23 Correct 3681 ms 10184 KB Output is correct
24 Correct 3351 ms 10176 KB Output is correct
25 Correct 1 ms 204 KB Output is correct
26 Correct 128 ms 4748 KB Output is correct
27 Correct 730 ms 5032 KB Output is correct
28 Correct 4421 ms 9488 KB Output is correct
29 Correct 3920 ms 9512 KB Output is correct
30 Correct 3525 ms 9564 KB Output is correct
31 Correct 3248 ms 9488 KB Output is correct
32 Correct 2893 ms 9572 KB Output is correct