QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#627141#7736. Red Black Tree000226WA 328ms44536kbC++171.6kb2024-10-10 14:55:512024-10-10 14:55:51

Judging History

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

  • [2024-10-10 14:55:51]
  • 评测
  • 测评结果:WA
  • 用时:328ms
  • 内存:44536kb
  • [2024-10-10 14:55:51]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)

using i64 = long long;
using i128 = __int128;

const int N = 1e5 + 5;

int n;
char s[N];
vector<int> e[N];
int fir[N], negsum[N];
multiset<int> st[N];

void dfs(int x, int fx) {
    for (auto y : e[x]) dfs(y, x);

    for (auto y : e[x]) {
        fir[x] += fir[y];
        if (st[x].size() == 0) {
            swap (st[x], st[y]);
            negsum[x] = negsum[y];
            continue; 
        }

        int lim = min(st[x].size(), st[y].size());
        multiset<int> tmp;
        int neg = 0;
        auto it1 = st[x].begin(), it2 = st[y].begin();
        for (int i = 0; i < lim; i ++, it1 ++, it2 ++) {
            int v = *it1 + *it2;
            if (v < 0) neg += v;
            else tmp.insert(v);
        }
        negsum[x] = neg;
        st[x] = tmp;
    }
    
    fir[x] += s[x] - '0';
    if (s[x] == '0') st[x].insert(1);
    else st[x].insert(-1), negsum[x] += -1;
}

void solve() {
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> s[i];
    for (int i = 1; i <= n; i ++) e[i].clear(), fir[i] = negsum[i] = 0, st[i].clear();
    for (int i = 2; i <= n; i ++) {
        int x;
        cin >> x;
        e[x].push_back(i);
    }
    dfs (1, 0);
    for (int i = 1; i <= n; i ++) {
        cout << fir[i] + negsum[i] << " \n"[i == n];
    }
}

int main() {
    ios :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int Case; 
    cin >> Case;
    while (Case --) solve();
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 328ms
memory: 44536kb

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
12 6 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 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
7 0 0 2 1 0 0 0 0 0 0
4 4 0 0 3 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
2 2 0 0 0
9 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
6 5...

result:

wrong answer 2nd lines differ - expected: '6 2 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0', found: '12 6 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0'