Submission #1326101

#TimeUsernameProblemLanguageResultExecution timeMemory
1326101sporknivesHomework (CEOI22_homework)C++20
100 / 100
341 ms194744 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 cnt[5000004];
int nxt[5000004];
unordered_map<int,int> prv;

int cur = 0;
void process(int a, int b, int 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;
	}
	
	cur++;
	l[n]=cur;
	int idx=nxt[a+6];
	process(a+4, idx, cur);
	
	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 c=0;
	memset(nxt,-1,sizeof(nxt));
	for(int i=0;i<s.length();i++) {
		if(s[i]=='(')c++;
		if(s[i]==')')c--;
		cnt[i]=c;
		if(prv.find(c)!=prv.end()) nxt[prv[c]]=i;
		prv[c]=i;
	}
	
	/*for(int i=0;i<s.length();i++) {
		cout<<nxt[i]<<" ";
	}*/
	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...