QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#486995 | #7736. Red Black Tree | CSQ | TL | 4ms | 19268kb | C++14 | 2.1kb | 2024-07-22 14:59:18 | 2024-07-22 14:59:18 |
Judging History
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;
multiset<int>dp[MAXN];
int best[MAXN],sum[MAXN],ans[MAXN];
void dfs(int v){
dp[v].clear();
sum[v] = best[v] = 0;
for(int x:adj[v]){
dfs(x);
if(dp[v].empty()){
best[v] = best[x];
sum[v] = sum[x];
dp[v] = dp[x];
}else{
if(sz(dp[v]) > sz(dp[x])){
swap(dp[v],dp[x]);
swap(best[v],best[x]);
}
vector<int>c;
auto it = dp[x].begin();
sum[v] = 0;
for(int val:dp[v]){
c.pb(val+*it);
if(val + *it <= 0)sum[v] += val + *it;
it++;
}
best[v]+=best[x];
dp[v] = multiset<int>(c.begin(),c.end());
}
}
if(s[v]=='0')dp[v].insert(1);
else {
dp[v].insert(-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]<<" ";
cout<<'\n';
for(int i=0;i<n;i++)adj[i].clear();
}
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 19268kb
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...