QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#241061#7523. Partially Free MealdayuxRE 0ms11836kbC++142.3kb2023-11-05 22:31:522023-11-05 22:31:52

Judging History

你现在查看的是最新测评结果

  • [2023-11-05 22:31:52]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:11836kb
  • [2023-11-05 22:31:52]
  • 提交

answer

#include <bits/stdc++.h>

const int maxn = 2e5, maxlg = 18, maxa = 1e9;

std::array<int, maxn + 1> a, b;
std::array<long long, maxn + 1> ans;
std::vector<int> vb;
std::array<std::vector<int>, maxn + 1> va;

namespace segt {
  int tot;
  std::array<int, maxn + 1> rt;

  struct node {
    int cnt, ls, rs;
    long long sum;
  };
  std::array<node, maxn * (maxlg + 1) + 1> t;

  void modify(int &x, int o, int p, int l = 1, int r = maxa) {
    t[x = ++tot] = t[o];
    ++t[x].cnt;
    t[x].sum += p;
    if (l < r) {
      int mid = (l + r) / 2;
      if (p <= mid)
        modify(t[x].ls, t[o].ls, p, l, mid);
      else
        modify(t[x].rs, t[o].rs, p, mid + 1, r);
    }
  }

  long long query(int x, int k, int l = 1, int r = maxa) {
    if (k > t[x].cnt)
      return std::numeric_limits<long long>().max() / 2;
    if (l == r)
      return (long long)k * l;
    
    int mid = (l + r) / 2;
    if (k <= t[t[x].ls].cnt)
      return query(t[x].ls, k, l, mid);
    else
      return t[t[x].ls].sum + query(t[x].rs, k - t[t[x].ls].cnt, mid + 1, r);
  }
}

void solve(int bl, int br, int kl, int kr) {
  int mid = (kl + kr) / 2, pos = bl;
  auto calc = [&](int x) {
    return vb[x - 1] + segt::query(segt::rt[x], mid);
  };
  ans[mid] = calc(pos);
  long long tmp;
  for (int i = bl + 1; i <= br; ++i)
    if ((tmp = calc(i)) < ans[mid]) {
      ans[mid] = tmp;
      pos = i;
    }

  if (kl <= mid - 1)
    solve(bl, pos, kl, mid - 1);
  if (mid + 1 <= kr)
    solve(pos, br, mid + 1, kr);
}

int main() {
  #ifndef ONLINE_JUDGE
    std::freopen("L.in", "r", stdin);
    std::freopen("L.out", "w", stdout);
  #endif
  std::cin.tie(nullptr)->sync_with_stdio(false);

  int n;
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> a[i] >> b[i];
    vb.push_back(b[i]);
  }
  std::sort(vb.begin(), vb.end());
  vb.erase(std::unique(vb.begin(), vb.end()), vb.end());
  for (int i = 1; i <= n; ++i)
    va[std::lower_bound(vb.begin(), vb.end(), b[i]) - vb.begin() + 1].push_back(a[i]);
  for (int i = 1; i <= (int)vb.size(); ++i) {
    segt::rt[i] = segt::rt[i - 1];
    for (int j : va[i])
      segt::modify(segt::rt[i], segt::rt[i], j);
  }
  
  solve(1, (int)vb.size(), 1, n);

  for (int i = 1; i <= n; ++i)
    std::cout << ans[i] << '\n';
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 11836kb

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Runtime Error

input:

200000
466436993 804989151
660995237 756645598
432103296 703610564
6889895 53276988
873617076 822481192
532911431 126844295
623111499 456772252
937464699 762157133
708503076 786039753
78556972 5436013
582960979 398984169
786333369 325119902
930705057 615928139
924915828 506145001
164984329 208212435...

output:


result: