QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#345501#7736. Red Black TreeSorting#TL 1ms3596kbC++202.9kb2024-03-07 01:50:242024-03-07 01:50:25

Judging History

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

  • [2024-03-07 01:50:25]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3596kb
  • [2024-03-07 01:50:24]
  • 提交

answer

#include<bits/stdc++.h>

using namespace std;

using pii = pair<int, int>;
using vi = vector<int>;
using vpii = vector<pii>;
using vvi = vector<vi>;

vi lazy(int lz_black, int lz_red, vi &v) {
    int n = v.size();
    bool black_done = false;
    bool red_done = false;
    int delta = lz_black;

    vi out;
    out.push_back(v[0] + delta);

    auto f = [&](int d, int cur) {
        if(d >= -1 && delta > 0) {
            for(int i = 0; i < lz_black; i++) {
                out.push_back(cur + delta - i - 1);
            }
            delta = 0;
        }
        if(d >= 1 && delta >= 0) {
            for(int i = 0; i < lz_red; i++) {
                out.push_back(cur + i + 1);
            }
            delta = -lz_red;
        }
    };

    for(int i = 1; i < n; i++) {
        // if(v[i+1] - v[i] >= -1 && delta < 0) {
        f(v[i] - v[i-1], v[i-1]);
        out.push_back(v[i] + delta);
    }
    f(1e9, v[n-1]);

    assert(out.size() == v.size() + lz_black + lz_red);

    return out;
}

void dfs(int u, string &s, vi &par, vvi &child, vvi &dp, vi &lz_black, vi &lz_red, vi &ans, vi &outdeg) {
    for(int v : child[u]) {
        dfs(v, s, par, child, dp, lz_black, lz_red, ans, outdeg);
    }

    if(outdeg[u] == 0) {
        dp[u] = {0};
        ans[u] = 0;
    } else if(outdeg[u] == 1) {
        ans[u] = ans[child[u][0]];
        swap(dp[u], dp[child[u][0]]);
        lz_red[u] = lz_red[child[u][0]];
        lz_black[u] = lz_black[child[u][0]];
    } else {
        int min_sz = 1e9;
        for(int v : child[u]) {
            dp[v] = lazy(lz_black[v], lz_red[v], dp[v]);
            min_sz = min(min_sz, (int)dp[v].size());
        }
        dp[u].resize(min_sz);
        fill(dp[u].begin(), dp[u].end(), 0);
        for(int i = 0; i < min_sz; i++) {
            for(int v : child[u]) {
                dp[u][i] += dp[v][i];
            }
        }
        ans[u] = *min_element(dp[u].begin(), dp[u].end());
    }
    lz_red[u] += (s[u] == '0');
    lz_black[u] += (s[u] == '1');

    cerr << "node " << u << ":";
    for(int i : dp[u]) {
        cerr << ' ' << i;
    }
    cerr << " lz_black " << lz_black[u] << " lz_red " << lz_red[u];
    cerr << endl;

}

void solve() {
    int n;
    string s;
    cin >> n >> s;
    vi par(n); // 0 = red  1 = black
    vvi child(n);
    vi outdeg(n);
    for(int i = 1; i < n; i++) {
        int u;
        cin >> u;
        u--;
        par[i] = u;
        child[u].push_back(i);
        outdeg[u]++;
    }
    
    vvi dp(n);
    vi ans(n);
    vi lz_black(n);
    vi lz_red(n);
    dfs(0, s, par, child, dp, lz_black, lz_red, ans, outdeg);

    for(int i = 0; i < n; i++) {
        cout << ans[i] << " \n"[i == n-1];
    }
}

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

    int tc;
    cin >> tc;
    while(tc--) {
        solve();
    }

    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3596kb

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
Time Limit Exceeded

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: