제출 #1326083

#제출 시각아이디문제언어결과실행 시간메모리
1326083sporknivesHomework (CEOI22_homework)C++20
53 / 100
1095 ms8640 KiB
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
// 1 - max, 2 - min
string s;
int l[2000004],r[2000004];
int op[2000004];
int cur = 0;
void process(int a, int b, int n) {
	//cout<<a<<" "<<b<<"\n";
	//cout<<"processing string: "<<s.substr(a,b-a+1)<<" "<<n<<'\n';
	int cnt=0;
	if(s[a]=='?') {
		op[n]=-1; return;
	}
	else if(s[a+1]=='i')op[n]=0;
	else op[n]=1;
	
	if(s[a+4]=='?') {
		cur++;
		l[n]=cur;
		//cout<<"call 1\n";
		process(a+4,a+4,cur);
		cur++;
		r[n]=cur;
		//cout<<"call 2\n";
		process(a+6,b-1, cur);
		return;
	}
	
	int idx=-1;
	for(int i=a+4;i<b+1;i++) {
		if(s[i]=='(') cnt++;
		else if(s[i]==')') {
			cnt--;
			if(cnt==0) {
				cur++;
				l[n]=cur;
				//cout<<"call 3\n";
				process(a+4, i, cur);
				idx=i;
				break;
			}
		}
	}
	
	cur++;
	r[n]=cur;
	//cout<<"call 4\n";
	process(idx+2, b-1,cur);
}

pii range[2000004];
int leaves[2000004];
void dfs(int x) {
	if(op[x]==-1) {
		range[x]={1,1};
		leaves[x]=1;
		return;
	}
	dfs(l[x]);
	dfs(r[x]);
	
	leaves[x] = leaves[l[x]] + leaves[r[x]];
	if(op[x]==1) {
		range[x].first = range[l[x]].first + range[r[x]].first;
		range[x].second = leaves[x] - min(leaves[l[x]]-range[l[x]].second, 
					leaves[r[x]]-range[r[x]].second);
	}
	else {
		range[x].first = min(range[l[x]].first, range[r[x]].first);
		range[x].second = leaves[x] - (leaves[l[x]]-range[l[x]].second) -
						(leaves[r[x]]-range[r[x]].second) - 1;
	}
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(nullptr);
	cin>>s;
	int n = 0;
	for(char c: s) {
		if(c=='?')n++;
	}
	
	process(0, s.length()-1, 0);
	
	dfs(0);
	cout<<range[0].second-range[0].first+1;
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...