QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#487047#7736. Red Black TreeCSQWA 158ms53260kbC++143.7kb2024-07-22 15:34:042024-07-22 15:34:04

Judging History

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

  • [2024-07-22 15:34:04]
  • 评测
  • 测评结果:WA
  • 用时:158ms
  • 内存:53260kb
  • [2024-07-22 15:34:04]
  • 提交

answer

#pragma GCC optimize("Ofast") 
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define lb lower_bound
#define ub upper_bound
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
#define debug(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define time__(d) for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false);\
debug("%s time : %.4fs\n", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> PII;
typedef pair<int,int> pii;
typedef vector<vector<int>> vii;
typedef vector<vector<ll>> VII;
ll gcd(ll a,ll b){if(!b)return a;else return gcd(b,a%b);}
const int MAXN = 2e5+5;
vector<int>adj[MAXN];
string s;
vector<int>lf[MAXN],rg[MAXN];
int best[MAXN],sum[MAXN],ans[MAXN],tot[MAXN];
void dfs(int v){
    lf[v].clear();
    rg[v].clear();
    sum[v] = best[v] = tot[v] = 0;
    int mn = adj[v].empty()?-1:adj[v][0];
    for(int x:adj[v]){
        dfs(x);
        if(tot[mn] > tot[x])mn = x;
    }
    if(mn != -1){
        swap(lf[v],lf[mn]);
        swap(rg[v],rg[mn]);
        tot[v] = tot[mn];
        best[v] = best[mn];
        sum[v] = sum[mn];
        if(sz(adj[v])>1){
            for(int x:adj[v]){
                if(x==mn)continue;
                int p = 0;
                bool mode = 0;
                if(lf[x].empty()){
                    p = sz(rg[x])-1;
                    mode = 1;
                }
                for(int i=0;i<sz(lf[v]);i++){
                    if(!mode){
                        lf[v][i] += lf[x][p];
                        p++;
                        if(p == sz(lf[x])){
                            mode = 1;
                            p = sz(rg[x])-1;
                        }
                    }else{
                        lf[v][i] += rg[x][p];
                        p--;
                    }
                }

                for(int i=sz(rg[v])-1;i>=0;i--){
                    if(!mode){
                        rg[v][i] += lf[x][p];
                        p++;
                        if(p == sz(lf[x])){
                            mode = 1;
                            p = sz(rg[x])-1;
                        }
                    }else{
                        rg[v][i] += rg[x][p];
                        p--;
                    }
                }
                best[v]+=best[x];
                vector<int>nlf,nrg;
                sum[v] = 0;
                for(int x:lf[v]){
                    if(x<0){
                        nlf.pb(x);
                        sum[v] += x;
                    }
                    else nrg.pb(x);
                }
                reverse(all(rg[v]));
                for(int x:rg[v]){
                    if(x<0){
                        nlf.pb(x);
                        sum[v] += x;
                    }
                    else nrg.pb(x);
                }

                lf[v] = nlf;
                rg[v] = nrg;
                reverse(all(rg[v]));
            }
        }

    }

    tot[v] ++;
    if(s[v]=='0')rg[v].pb(1);
    else {
        lf[v].pb(-1);
        best[v]++;
        sum[v]--;
    }
    ans[v] = best[v] + sum[v];
}
int main()
{
    owo
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        cin>>s;
        for(int i=1;i<n;i++){
            int p;
            cin>>p;
            adj[p-1].pb(i);
        }
        dfs(0);
        for(int i=0;i<n;i++){
            cout<<ans[i];
            if(i!=n-1)cout<<" ";
        }
        cout<<'\n';
        for(int i=0;i<n;i++)adj[i].clear();
    }


}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 18808kb

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: 158ms
memory: 53260kb

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 0 0 0 0
5 3 ...

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'