God-Like Problem
很牛的题。
容易发现
但是这样显然询问次数会爆炸,次数上限应该是
注意到,我们需要求解的表达式可以拍到一棵树上,而该树的中序遍历就是原来的表达式。每次新增一个未知量
首先,考虑一个二分的做法。假设我们判定其是否在
考虑将二分优化掉。发现每次只会在极右链新增一的深度,因此势能是定的。故我们可以每次直接暴力往上跳,因为有奇偶性的限制,这部分变成了
故总数为
注意表达式树需要一个虚根。
::::success[Code]
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using uint = unsigned int;
using i128 = __int128;
using PII = pair<int, int>;
using PLL = pair<ll, ll>;
#define fi first
#define se second
constexpr ll inf = (1ll << 62);
constexpr int N = 2e5 + 10;
template <typename T> void chkmax(T& a, T b) { a = max(a, b); }
template <typename T> void chkmin(T& a, T b) { a = min(a, b); }
void solve() {
int n, lst = 2;
cin >> n;
int tot = n + 2;
stack<int> st; st.push(n + 1), st.push(n + 2), st.push(2);
vector<int> l(2 * n + 2), r(2 * n + 2), leaf;
r[n + 1] = n + 2, l[n + 2] = 1, r[n + 2] = 2;
leaf.emplace_back(1); leaf.emplace_back(2);
function<void(int)> print = [&](int u) {
if (l[u]) cout << "(", print(l[u]);
if (u <= n) cout << "x";
if (u > n) cout << "-";
if (r[u]) print(r[u]), cout << ")";
};
int cnt = 0;
for (int i = 3; i <= n; i++) {
vector<int> qry(n + 1);
cnt += 2;
cout << "query ";
for (auto j : leaf) qry[j] = 1; qry[i] = 1;
for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
int flag;
cin >> flag;
if (!((int(leaf.size()) ^ flag) & 1)) st.pop(), leaf.pop_back();
fill(qry.begin(), qry.end(), 0);
cout << "query ";
for (int j = 0; j < int(leaf.size()) - 2; j++) qry[leaf[j]] = 1; qry[i] = 1;
for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
int x;
cin >> x;
// for (auto i : leaf) cout << i << " "; cout << endl;
if (!((x ^ max(int(leaf.size()) - 2, 0)) & 1)) {
// cerr << i << "\n";
l[++tot] = st.top(), r[tot] = i, st.pop(), r[st.top()] = tot, st.push(tot), st.push(i), leaf.emplace_back(i);
} else {
// cerr << "Cinema\n";
while (st.size() > 1) {
fill(qry.begin(), qry.end(), 0);
st.pop(), st.pop(), leaf.pop_back(), leaf.pop_back();
cnt++;
assert(cnt <= 500);
cout << "query ";
for (int j = 0; j < int(leaf.size()) - 2; j++) qry[leaf[j]] = 1; qry[i] = 1;
for (int j = 1; j <= n; j++) cout << qry[j]; cout << endl;
cin >> x;
if (!((x ^ max(int(leaf.size()) - 2, 0)) & 1)) {
l[++tot] = st.top(), r[tot] = i, st.pop(), r[st.top()] = tot, st.push(tot), st.push(i), leaf.emplace_back(i);
break;
}
}
if (st.size() == 1) l[++tot] = r[st.top()], r[st.top()] = tot, r[tot] = i, st.push(tot), st.push(i), leaf.emplace_back(i);
}
// print(r[n + 1]); cout << endl;
}
// cout << r[n + 1] << " " << r[r[n + 1]] << "\n"; return;
cout << "answer ";
print(r[n + 1]);
}
int main() {
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
::::