QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#720112 | #7736. Red Black Tree | OIer_kzc# | RE | 0ms | 16492kb | C++17 | 3.6kb | 2024-11-07 10:42:27 | 2024-11-07 10:42:30 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fod(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
#define lx edge[x][i]
int T;
int n;
int big[100011],fa[100011],sz[100011];
vector<int>edge[100011];
struct convex{
multiset<int>L;
multiset<int>R;
int x,y;
void clear(){
x=0,y=0;
L.clear();
R.clear();
}
void operator += (const convex &A) {
vector<int>a;
a.resize(max((int)A.L.size()+(int)A.R.size()+1,(int)L.size()+(int)R.size()+1));
fo(i,0,(int)a.size()-1){
a[i]=0;
}
int u=A.x,v=A.y;
a[u]+=A.y;
for(auto it=A.L.begin();it!=A.L.end();++it){
--u;
v+=*it;
a[u]+=v;
}
u=A.x,v=A.y;
for(auto it=A.R.begin();it!=A.R.end();++it){
++u;
v+=*it;
a[u]+=v;
}
u=x,v=y;
a[u]+=y;
for(auto it=L.begin();it!=L.end();++it){
--u;
v+=*it;
a[u]+=v;
}
u=x,v=y;
for(auto it=R.begin();it!=R.end();++it){
++u;
v+=*it;
a[u]+=v;
}
int len=min((int)A.L.size()+(int)A.R.size()+1,(int)L.size()+(int)R.size()+1);
while(a.size()>len)a.pop_back();
u=0,v=1e9;
fo(i,0,len-1){
if(v>a[i]){
v=a[i],u=i;
}
}
clear();
x=u,y=v;
fod(i,u-1,0){
L.insert(a[i]-a[i+1]);
}
fo(i,u+1,len-1){
R.insert(a[i]-a[i-1]);
}
}
void print(){
vector<int>a;
a.resize((int)L.size()+(int)R.size()+1);
int u=x,v=y;
a[u]+=y;
for(auto it=L.begin();it!=L.end();++it){
--u;
v+=*it;
a[u]+=v;
}
u=x,v=y;
for(auto it=R.begin();it!=R.end();++it){
++u;
v+=*it;
a[u]+=v;
}
cout<<a.size()<<endl;
fo(i,0,a.size()-1)cout<<a[i]<<" ";
cout<<endl;
}
}c[100011];
char col[100011];
int ans[100011],p[100011];
void solve(){
scanf("%d",&n);
scanf("%s",col);
fo(i,2,n){
scanf("%d",&fa[i]);
sz[i]=0;
edge[i].clear();
}
fod(i,n,1){
++sz[i];
if(i>1)edge[fa[i]].push_back(i);
if(i>1)sz[fa[i]]+=sz[i];
}
fo(i,1,p[0])c[i].clear();
p[0]=0;
fo(x,1,n){
if(!p[x])p[x]=++p[0];
big[x]=0;
fo(i,0,(int)edge[x].size()-1){
if(sz[big[x]]<sz[lx])big[x]=lx;
}
if(big[x])p[big[x]]=p[x];
}
fod(x,n,1){
int y=p[x];
if(!edge[x].size()){
if(col[x-1]=='0'){
c[y].x=0;
c[y].y=0;
c[y].R.insert(1);
}
else {
c[y].x=1;
c[y].y=0;
c[y].L.insert(1);
}
}
else{
fo(i,0,(int)edge[x].size()-1){
if(lx!=big[x])c[y]+=c[p[lx]];
}
if(col[x-1]=='0'){
c[y].R.insert(1);
}
else {
c[y].x++;
c[y].L.insert(1);
}
}
ans[x]=c[y].y;
}
fo(i,1,n-1){
printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}
int main(){
scanf("%d",&T);
while(T){
--T;
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 16492kb
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
Runtime Error
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...