QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#378061#7736. Red Black TreephtniitWA 224ms165688kbC++142.3kb2024-04-05 23:53:582024-04-05 23:53:58

Judging History

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

  • [2024-04-05 23:53:58]
  • 评测
  • 测评结果:WA
  • 用时:224ms
  • 内存:165688kb
  • [2024-04-05 23:53:58]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std ;

const int maxn = 100010;
const int inf = 1e9+7;

using pii = pair<int, int>;

struct S {
  int len;
  int l, r, v;
  deque<int> rig;
  deque<int> lef;
} s[maxn];

vector<int> g[maxn];
char col[maxn];

void dfs(int u) {
  auto& su = s[u];
  if (g[u].empty()) {
    su.len = 1;
    su.v = 0;
    su.lef.clear();
    su.rig.clear();
    if (col[u] == '1') {
      su.l = su.r = 1;
      su.lef.emplace_back(1);
    } else {
      su.l = su.r = 0;
      su.rig.emplace_back(1);
    }
    return;
  }

  int L = inf;
  for (auto v: g[u]) {
    dfs(v);
    L = min(L, s[v].len);
  }

  if (g[u].size() == 1) {
    int v = g[u][0];
    auto& sv = s[v];
    su.l = sv.l;
    su.r = sv.r;
    su.v = sv.v;
    swap(su.lef, sv.lef);
    swap(su.rig, sv.rig);
  } else {
    vector<int> dp(L+1);
    for (auto v: g[u]) {
      auto& sv = s[v];
      int w = sv.v, sr = sv.r, sl = sv.l;
      for (int i = sl; i <= sr && i <= L; ++i) dp[i] += w;
      while (not sv.rig.empty()) {
        w += sv.rig.front();
        sv.rig.pop_front();
        if (++sr <= L) {
          dp[sr] += w;
        }
      }
      w = sv.v;
      while (not sv.lef.empty()) {
        w += sv.lef.back();
        sv.lef.pop_back();
        if (--sl <= L) {
          dp[sl] += w;
        }
      }
    }

    su.r = 0;
    for (int i = 1; i <= L; ++i) if (dp[i] < dp[su.r]) {
      su.r = i;
    }

    su.l = L;
    for (int i = L-1; i >= 0; --i) if (dp[i] < dp[su.l]) {
      su.l = i;
    }

    su.v = dp[su.l];

    su.lef.clear();
    su.rig.clear();
    for (int i = su.l-1; i >= 0; --i) su.lef.emplace_front(dp[i] - dp[i+1]);
    for (int i = su.r+1; i <= L; ++i) su.rig.emplace_back(dp[i] - dp[i-1]);
  }

  su.len = L+1;
  if (col[u] == '1') { // black
    su.l++;
    su.r++;
    su.lef.emplace_back(1);
  } else { // red
    su.rig.emplace_front(1);
  }
}

void once() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    g[i].clear();
  }
  scanf("%s", col+1);
  for (int i = 2, p; i <= n; ++i) {
    scanf("%d", &p);
    g[p].push_back(i);
  }
  dfs(1);
  for (int i = 1; i <= n; ++i) printf("%d ", s[i].v);
  puts("");
}

int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    once();
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 24ms
memory: 142380kb

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: -100
Wrong Answer
time: 224ms
memory: 165688kb

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 
7 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 
1 1 0 0 0 
6 1 0 1 0 0 0 0 0 0 0 0 0 0 
1 0 0 0 0 0 0 0 0...

result:

wrong answer 11th lines differ - expected: '6 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0', found: '7 5 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 '