Submission #132225

#TimeUsernameProblemLanguageResultExecution timeMemory
132225johuthaMatch (CEOI16_match)C++14
10 / 100
15 ms376 KiB
#include <vector> #include <iostream> #include <string> #include <algorithm> #define int int64_t using namespace std; struct segtree { vector<int> table; int n; void update(int k, int v, int l, int r, int pos) { if (l == r) { table[pos] = v; return; } if (k <= (l + r)/2) update(k, v, l, (l + r)/2, 2*pos + 1); else update(k, v, (l + r)/2 + 1, r, 2*pos + 2); table[pos] = table[2*pos + 1] + table[2*pos + 2]; } void update(int k, int v) { update(k, v, 0, n - 1, 0); } int query(int ql, int qr, int l, int r, int pos) { if (ql <= l && r <= qr) return table[pos]; if (ql > r || qr < l) return 0; return query(ql, qr, l, (l + r)/2, 2*pos + 1) + query(ql, qr, (l + r)/2 + 1, r, 2*pos + 2); } int query(int ql, int qr) { return query(ql, qr, 0, n - 1, 0); } void print() { for (int i = 0; i < 4*n; i++) { if (((i + 1) & (i)) == 0) cout << "\n"; cout << table[i] << " "; } cout << "\n"; } void build(vector<int> &vals, int l, int r, int pos) { if (l == r) { table[pos] = vals[l]; return; } build(vals, l, (l + r)/2, 2*pos + 1); build(vals, (l + r)/2 + 1, r, 2*pos + 2); table[pos] = table[2*pos + 1] + table[2*pos + 2]; } }; struct brackseq { string s; int n; segtree st; vector<int> corr; bool check(int l, int r) { if (r < l) return false; if (st.query(l, l) == 1 || st.query(r, r) == -1) return false; if (l + 1 != r && st.query(l, r) != 0) return false; if (st.query(corr[l], corr[r]) != 0) return false; return true; } string calc() { vector<int> stack; vector<int> br(n, 0); corr.resize(n); string res; for (int i = 0; i < n; i++) { if (stack.size() == 0 || s[i] != s[stack.back()]) { stack.push_back(i); br[i] = 1; } else { corr[stack.back()] = i; corr[i] = stack.back(); stack.pop_back(); br[i] = -1; } } st.n = n; st.table.resize(4*n); st.build(br, 0, n - 1, 0); vector<vector<int>> chlist(256); for (int i = n - 1; i >= 0; i--) { chlist[s[i]].push_back(i); } if (!stack.empty()) return "-1"; for (int i = 0; i < n; i++) { for (int ot : chlist[s[i]]) { if (check(i, ot)) { st.update(i, 1); st.update(ot, -1); corr[corr[ot]] = corr[i]; corr[corr[i]] = corr[ot]; corr[i] = ot; corr[ot] = i; } } } for (int i = 0; i < n; i++) { if (st.query(i, i) == 1) res.push_back('('); else res.push_back(')'); } return res; } }; signed main() { brackseq bs; cin >> bs.s; bs.n = bs.s.size(); cout << bs.calc() << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...