QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#263639#7736. Red Black Treeucup-team987#AC ✓730ms57748kbC++236.6kb2023-11-25 00:03:412023-11-25 00:03:41

Judging History

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

  • [2024-02-19 10:14:05]
  • hack成功,自动添加数据
  • (/hack/538)
  • [2023-11-25 00:03:41]
  • 评测
  • 测评结果:AC
  • 用时:730ms
  • 内存:57748kb
  • [2023-11-25 00:03:41]
  • 提交

answer

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

namespace {

constexpr int INF = INT_MAX / 2;

void solve() {
  int n;
  scan(n);
  string s;
  scan(s);
  vector<int> p(n, -1);
  vector<vector<int>> g(n);
  for (int i : rep(1, n)) {
    scan(p[i]);
    --p[i];
    g[p[i]].push_back(i);
  }

  vector<int> ans(n);
  vector<SlopeTrick> d(n);
  fix([&](auto self, int i) -> void {
    if (g[i].empty()) {
      if (s[i] == '1') {
        d[i].add_amx(1);
      } else {
        d[i].add_xma(0);
      }
      d[i].add_amx(0, INF);
      d[i].add_xma(1, INF);
      return;
    }

    for (int j : g[i]) {
      self(j);
      d[i].merge(d[j]);
    }

    if (s[i] == '1') {
      d[i].shift_x(1);
      const auto tmp = d[i].getL();
      d[i].popL();
      if (1 < tmp.c) {
        d[i].pushL(tmp.x, tmp.c - 1);
      }
      d[i].shift_L(-1);
      d[i].pushL(tmp.x);
    } else {
      const auto tmp = d[i].getR();
      d[i].popR();
      if (1 < tmp.c) {
        d[i].pushR(tmp.x, tmp.c - 1);
      }
      d[i].shift_R(1);
      d[i].pushR(tmp.x);
    }
    ans[i] = d[i].get_min().second;
  })(0);

  print(ans);
}

}  // namespace

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  scan(t);
  while (t--) {
    solve();
  }
}

#else  // __INCLUDE_LEVEL__

#include <bits/stdc++.h>

using namespace std;

template <class T>
concept tuple_like = __is_tuple_like<T>::value && !ranges::range<T>;

template <class R>
concept nstr_range = ranges::range<R> && !convertible_to<R, string_view>;

namespace std {

istream& operator>>(istream& is, tuple_like auto&& t) {
  return apply([&is](auto&... xs) -> istream& { return (is >> ... >> xs); }, t);
}

istream& operator>>(istream& is, nstr_range auto&& r) {
  for (auto&& e : r) {
    is >> e;
  }
  return is;
}

ostream& operator<<(ostream& os, tuple_like auto&& t) {
  auto f = [&os](auto&... xs) -> ostream& {
    [[maybe_unused]] auto sep = "";
    ((os << exchange(sep, " ") << xs), ...);
    return os;
  };
  return apply(f, t);
}

ostream& operator<<(ostream& os, nstr_range auto&& r) {
  auto sep = "";
  for (auto&& e : r) {
    os << exchange(sep, " ") << e;
  }
  return os;
}

#define DEF_INC_OR_DEC(op) \
  auto& operator op(tuple_like auto&& t) { \
    apply([](auto&... xs) { (op xs, ...); }, t); \
    return t; \
  } \
  auto& operator op(nstr_range auto&& r) { \
    for (auto&& e : r) { \
      op e; \
    } \
    return r; \
  }

DEF_INC_OR_DEC(++)
DEF_INC_OR_DEC(--)

#undef DEF_INC_OR_DEC

}  // namespace std

void scan(auto&&... xs) { cin >> tie(xs...); }
void print(auto&&... xs) { cout << tie(xs...) << '\n'; }

template <class F>
class fix {
 public:
  explicit fix(F f) : f_(move(f)) {}

  template <class... Ts>
  decltype(auto) operator()(Ts&&... xs) const {
    return f_(ref(*this), forward<Ts>(xs)...);
  }

 private:
  F f_;
};

using views::drop;
using views::take;
inline constexpr auto rev = views::reverse;
inline constexpr auto len = ranges::ssize;
inline auto rep(int l, int r) { return views::iota(min(l, r), r); }
inline auto rep(int n) { return rep(0, n); }
inline auto rep1(int l, int r) { return rep(l, r + 1); }
inline auto rep1(int n) { return rep(1, n + 1); }

// https://nyaannyaan.github.io/library/data-structure/slope-trick.hpp
struct SlopeTrick {
  using ll = long long;
  struct P {
    ll x, c;
    P(ll _x, ll _c) : x(_x), c(_c) {}
    friend bool operator<(const P& a, const P& b) { return a.x < b.x; }
    friend bool operator>(const P& a, const P& b) { return a.x > b.x; }
  };
  ll addL, addR, min_y;
  priority_queue<P> L;
  priority_queue<P, vector<P>, greater<>> R;
  void pushL(ll x, ll c = 1) { L.emplace(x - addL, c); }
  void pushR(ll x, ll c = 1) { R.emplace(x - addR, c); }
  P getL() { return P{L.top().x + addL, L.top().c}; }
  P getR() { return P{R.top().x + addR, R.top().c}; }
  void popL() { L.pop(); }
  void popR() { R.pop(); }

  SlopeTrick() : addL(0), addR(0), min_y(0) {}

  void debug() {
    cerr << "addL, addR, min_y : ";
    cerr << addL << ", " << addR << ", " << min_y << endl;

    auto LL{L};
    cerr << "L : ";
    while (!LL.empty()) {
      cerr << "( " << LL.top().x + addL << ", " << LL.top().c << " ), ";
      LL.pop();
    }
    cerr << endl;

    auto RR{R};
    cerr << "R : ";
    while (!RR.empty()) {
      cerr << "( " << RR.top().x + addR << ", " << RR.top().c << " ), ";
      RR.pop();
    }
    cerr << endl;

    cerr << "min : ";
    cerr << "( " << get_min().first << ", " << get_min().second << " )" << endl;
  }

  pair<ll, ll> get_min() {
    ll x = L.empty() ? R.empty() ? 0 : getR().x : getL().x;
    return {x, min_y};
  }

  void shift_L(ll a) { addL += a; }
  void shift_R(ll a) { addR += a; }
  void shift_x(ll a) { addL += a, addR += a; }
  void shift_y(ll a) { min_y += a; }

  void add_xma(ll a, ll c = 1) {
    ll used = 0;
    while (used < c && !L.empty()) {
      auto [X, C] = getL();
      if (X <= a) break;
      popL();
      ll tmp = min(c - used, C);
      pushR(X, tmp);
      if (C != tmp) pushL(X, C - tmp);
      min_y += (X - a) * tmp;
      used += tmp;
    }
    if (used) pushL(a, used);
    if (c - used) pushR(a, c - used);
  }

  void add_amx(ll a, ll c = 1) {
    ll used = 0;
    while (used < c && !R.empty()) {
      auto [X, C] = getR();
      if (X >= a) break;
      popR();
      ll tmp = min(c - used, C);
      pushL(X, tmp);
      if (C != tmp) pushR(X, C - tmp);
      min_y += (a - X) * tmp;
      used += tmp;
    }
    if (used) pushR(a, used);
    if (c - used) pushL(a, c - used);
  }

  void add_abs_xma(ll a, ll c = 1) { add_xma(a, c), add_amx(a, c); }

  void chmin_right() { decltype(R){}.swap(R); }
  void chmin_left() { decltype(L){}.swap(L); }

  void merge(SlopeTrick& r) {
    if (L.size() + R.size() < r.L.size() + r.R.size()) swap(*this, r);
    while (!r.L.empty()) {
      auto [x, c] = r.getL();
      r.popL();
      add_amx(x, c);
    }
    while (!r.R.empty()) {
      auto [x, c] = r.getR();
      r.popR();
      add_xma(x, c);
    }
    shift_y(r.min_y);
  }

  ll eval(ll x) {
    ll y = min_y;
    auto LL{L};
    while (!LL.empty()) {
      auto [X, C] = LL.top();
      X += addL;
      if (X < x) break;
      y += (X - x) * C;
      LL.pop();
    }
    auto RR{R};
    while (!RR.empty()) {
      auto [X, C] = RR.top();
      X += addR;
      if (x < X) break;
      y += (x - X) * C;
      RR.pop();
    }
    return y;
  }
};

#endif  // __INCLUDE_LEVEL__

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

2
9
101011110
1 1 3 3 3 6 2 2
4
1011
1 1 3

output:

4 1 2 0 0 0 0 0 0
2 0 0 0

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 340ms
memory: 57748kb

input:

6107
12
000000001000
1 2 3 2 5 4 4 7 3 8 11
19
1100111101111011110
1 2 1 1 4 5 2 4 3 2 2 7 10 2 11 3 15 5
7
0111110
1 1 2 2 1 5
3
000
1 1
7
1000011
1 2 3 3 5 4
7
0001001
1 1 1 3 5 3
8
00111000
1 1 3 2 5 2 7
11
11111110111
1 1 1 4 5 4 5 2 5 1
15
110101101000010
1 2 3 2 1 5 2 5 6 5 8 7 9 14
10
0101000...

output:

1 1 1 1 0 0 0 0 0 0 0 0
6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0
0 0 0
0 0 0 0 0 0 0
2 0 1 0 0 0 0
2 1 0 0 0 0 0 0
4 0 0 2 1 0 0 0 0 0 0
4 3 0 0 2 0 0 0 0 0 0 0 0 0 0
2 0 1 0 0 0 0 0 0 0
6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0
1 1 0 0 0
5 1 0 1 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0
5 3 ...

result:

ok 6107 lines

Test #3:

score: 0
Accepted
time: 730ms
memory: 40768kb

input:

10
100000
10001010000001100001000100001000010100010101100001001110110001000010000110001000000010000011000001000001010001110100000000000000000010011011100000000000001000000000100100100110000000100001010011000000110000000111010100100001100000100100101001000000010000000011100000000000000010001100011100...

output:

22440 21414 19422 13454 5328 2719 1421 1168 1478 661 4662 5037 418 183 2304 501 2008 1643 692 2211 570 1003 967 950 504 124 894 333 775 523 905 197 12 337 195 310 1325 1016 638 50 907 361 179 336 313 102 309 555 733 871 490 414 375 318 66 625 336 267 276 162 203 25 112 216 252 146 42 233 232 333 27 ...

result:

ok 10 lines

Test #4:

score: 0
Accepted
time: 238ms
memory: 32620kb

input:

10
100000
01010111111101011100011111111010011001111111110001100111111101011111110011101111110110111011010111011011010011111110101111111011111111011101011111011001110101111011011010110100011111001101001011111101111101111111111100101101000111111110111101111111111011111100111011101110110101111010101101...

output:

25019 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 10 lines

Test #5:

score: 0
Accepted
time: 178ms
memory: 46080kb

input:

10
100000
00111110110011111111111010011111011111101010110111111110011110111111111111000110101110110111111101011111111111010101111111011001110110011101111001110111101101110110101000011111110100101110110100111110001111011100111101011010111111011011100011111011110111111110011110111111001111111010011100...

output:

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 10 lines

Test #6:

score: 0
Accepted
time: 280ms
memory: 37812kb

input:

10
100000
00111100100100001111011000100000000000111001100000000000100000101001001010010000001000010010111000001011010000000000001000000000010100000010010010000001000010001000000100000001010000000000000000000001000110000010100100000010000011000000000010010000100010100000000100000100100011000000001000...

output:

4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 4415 ...

result:

ok 10 lines

Test #7:

score: 0
Accepted
time: 181ms
memory: 36788kb

input:

10
100000
00111101111001101110101101111110100001010100011011100001011100000110000100100010111010011001111011100010010011111100000011111011001001000110000101101001011110000000011100001010100001000110110101111010000100000111001110001100001001001000101110100111111000101101100000011001110111001111101011...

output:

210 210 210 210 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 301ms
memory: 30868kb

input:

10
100000
00010011111100101111110000110010101110000000001111011110011010011011101011010000111100001001111111111001000110100010001111010111101100101111101100001001100110000011010100110110010101000010111001001010110111011000010100110101110001100110101010101001100010100000100000100101011110000100001001...

output:

6360 6360 4803 4803 1549 1549 1555 1555 1555 1595 1555 1549 1549 1555 1555 1600 1555 1549 1549 1549 1555 1555 1555 1555 1595 1549 1555 1555 1555 1555 1595 1595 1600 1555 1600 1600 1595 1549 1555 1555 1549 1600 1595 1600 1555 1595 1595 1549 1549 1549 1549 1555 1549 1549 1600 1595 1555 1549 1549 1555 ...

result:

ok 10 lines

Test #9:

score: 0
Accepted
time: 558ms
memory: 49016kb

input:

10
100000
11000111101111111101001011010110110111010011000111011111011111110111110000110101111111011101111011111111101110011100011101111111001011111101011111110010011111101111111011101101100110101010011111110111111101100101010011111111111100101111111101111100011100111110111111011111011001111110011101...

output:

18217 12003 6214 6012 5991 3232 2982 3008 3004 2973 3017 1780 1451 1499 1483 1513 1495 1486 1516 1499 1474 1509 1508 1037 743 738 713 722 776 752 731 741 771 759 735 731 755 740 776 733 765 736 738 753 756 739 769 678 359 362 380 364 374 342 370 356 364 393 382 367 385 360 371 370 371 383 387 387 37...

result:

ok 10 lines

Test #10:

score: 0
Accepted
time: 298ms
memory: 36232kb

input:

10
100000
10111111011011110010111111111111110111101110110001011111101110111011111111111101101011111101101001100111011011011101001110110110101010010001010111111111111111111011011011101011011101100001111101111110111110010011011111111011101110111111111110010011110011111011011011111111101111001110111111...

output:

50037 0 50035 0 50034 0 50033 0 50033 0 50032 0 50030 0 50029 0 50029 0 50027 0 50025 0 50024 0 50023 0 50022 0 50021 0 50020 0 50019 0 50019 0 50018 0 50017 0 50015 0 50014 0 50012 0 50012 0 50011 0 50011 0 50010 0 50009 0 50008 0 50006 0 50005 0 50003 0 50002 0 50000 0 49999 0 49998 0 49997 0 4999...

result:

ok 10 lines

Extra Test:

score: 0
Extra Test Passed