代码
#include<bits/stdc++.h>
using namespace std;
string s,p;
int n,res,acnt[1000006],ocnt[1000006],pcnt,tcnt,f[1000006];
struct NODE{int id,v,lc,rc;} t[1000006];
char s1[1000006];int top1;
int s2[1000006],top2;
void in_to_tree(){
for(int i=0;i<n;i++){
if(s[i]=='0'||s[i]=='1'){
p[pcnt++]=s[i];
}else if(s[i]=='|'){
while(top1&&s1[top1]!='('){
p[pcnt++]=s1[top1];
s1[top1]=0,top1--;
}
top1++,s1[top1]='|';
}else if(s[i]=='&'){
while(top1&&s1[top1]=='&'){
p[pcnt++]=s1[top1];
s1[top1]=0,top1--;
}
top1++,s1[top1]='&';
}else if(s[i]=='('){
top1++,s1[top1]='(';
}else if(s[i]==')'){
while(s1[top1]!='('){
p[pcnt++]=s1[top1];
s1[top1]=0,top1--;
}
s1[top1]=0,top1--;
}
}
while(top1){
p[pcnt++]=s1[top1];
s1[top1]=0,top1--;
}
for(int i=0;i<pcnt;i++){
if(p[i]=='1'||p[i]=='0'){
t[++tcnt]=(NODE){tcnt,p[i]-'0',-1,-1};
top2++,s2[top2]=tcnt;
}else{
if(p[i]=='|'){
int t1=s2[top2];s2[top2]=0,top2--;
int t2=s2[top2];s2[top2]=0,top2--;
t[++tcnt]=(NODE){tcnt,-1,t2,t1};
top2++,s2[top2]=tcnt;
}else if(p[i]=='&'){
int t1=s2[top2];s2[top2]=0,top2--;
int t2=s2[top2];s2[top2]=0,top2--;
t[++tcnt]=(NODE){tcnt,-2,t2,t1};
top2++,s2[top2]=tcnt;
}
}
}
}
int calc_tree(int u){
if(t[u].v>=0){
return f[u]=t[u].v;
}
if(t[u].v==-1){
bool f1=calc_tree(t[u].lc);
bool f2=calc_tree(t[u].rc);
if(f1){
ocnt[u]=ocnt[t[u].lc]+1;
acnt[u]=acnt[t[u].lc];
}else{
ocnt[u]=ocnt[t[u].lc]+ocnt[t[u].rc];
acnt[u]=acnt[t[u].lc]+acnt[t[u].rc];
}
return f[u]=f1|f2;
}
if(t[u].v==-2){
bool f1=calc_tree(t[u].lc);
bool f2=calc_tree(t[u].rc);
if(!f1){
acnt[u]=acnt[t[u].lc]+1;
ocnt[u]=ocnt[t[u].lc];
}else{
acnt[u]=acnt[t[u].lc]+acnt[t[u].rc];
ocnt[u]=ocnt[t[u].lc]+ocnt[t[u].rc];
}
return f[u]=f1&f2;
}
}
int main(){
cin>>s;
n=s.length();
in_to_tree();
res=calc_tree(pcnt);
cout<<res<<"\n"<<acnt[pcnt]<<" "<<ocnt[pcnt];
return 0;
}
谢谢大佬orz