Meaning of the questions: to give you a bunch of set of instructions, when an instruction is $ `` L "$, represents the mouse cursor moves left one unit $ ($ If you have the far left does not move $) $, when the instruction is $ `` R "$, indicates that the cursor as the right to move a unit, other characters $ ($ only lowercase letters and left parenthesis, right parenthesis $) $ indicated that they will change the current cursor to the character to character input, for each instruction, a number of outputs, if the text in parentheses resulting sequence is legal, the maximum output current text nesting brackets, braces sequence rule output sub $ $ -1
Ideas: using $ 1 $ a left parenthesis, $ - 1 $ express right parenthesis, $ 0 $ represent other characters, maintain three values with a segment tree: interval and $ w $, the interval maximum prefix and $ imax $, the interval smallest prefix and $ imin $. For any legal parenthesis sequence, we need to meet the following two conditions:
- An equal number of left and right parentheses, i.e. $ w = 0 $
- Any prefix sequence brackets are greater than or equal to $ 0 and $, i.e. $ imin \ ge 0 $
For a legitimate sequence of brackets, parentheses largest nesting range is the largest prefix and $ imax $, the need to maintain a single point to modify and query interval tree line, but because each query range is $ [1, n-] $, the interval represented by the root node segment is just $ [1, n] $, it maintains only a single point of the wire segment tree, enter lowercase letters and left parenthesis, right parenthesis when the value of the cursor position to a value corresponding to, and at the same time updating interval, and the interval maximum prefix, the prefix and the minimum interval, the specific updated as follows:
- $ = $ Interval and the interval left and the right intervals and $ + $
- Prefix and the minimum $ = min ($ prefix and the minimum interval left $, $ interval left section and a right section minimum $ + $ and $ prefix) $
- Maximum prefix and $ = max ($ and $ maximum prefix interval left, left $ $ + $ interval section and a right section and a maximum prefix $) $
Analyzing updated $ [1, n] $ bracketed sequence legal, illegal output $ $ -1, legal directly output interval $ [1, n] values in $ $ $ of IMAX
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> using namespace std; const int N = 1000010; struct node { int l, r, w; int imin, imax; }; node tree[4 * N]; int n, res[N]; char s[N]; void build(int k, int lef, int rig) { tree[k].l = lef, tree[k].r = rig; if (tree[k].l == tree[k].r) { tree[k].w = tree[k].imin = tree[k].imax = 0; return; } int mid = (lef + rig) / 2; build(k * 2, lef, mid); build(k * 2 + 1, mid + 1, rig); tree[k].w = tree[k * 2].w + tree[k * 2 + 1].w; tree[k].imax = max(tree[k * 2].imax, tree[k * 2 + 1].imax); tree[k].imin = max(tree[k * 2].imin, tree[k * 2 + 1].imin); } void change_point(int k, int x, int y) { if (tree[k].l == tree[k].r) { tree[k].w = y; return; } int mid = (tree[k].l + tree[k].r) / 2; if (x <= mid) change_point(2 * k, x, y); else change_point(2 * k + 1, x, y); tree[k].w = tree[k * 2].w + tree[k * 2 + 1].w; tree[k].imax = max(tree[k * 2].imax, tree[k * 2].w + tree[k * 2 + 1].imax); tree[k].imin = min(tree[k * 2].imin, tree[k * 2].w + tree[k * 2 + 1].imin); } int main () { scanf("%d%s", &n, s + 1); build(1, 1, n); int cur = 1, len = strlen(s + 1); for (int i = 1; i <= len; i++) { if ('(' == s[i]) change_point(1, cur, 1); else if (')' == s[i]) change_point(1, cur, -1); else if ('L' == s[i]) { if (cur >= 2) cur--; } else if ('R' == s[i]) cur++; else change_point(1, cur, 0); if (0 != tree[1].w || 0 != tree[1].imin) res[i] = -1; else res[i] = tree[1].imax; } for (int i = 1; i <= len; i++) { printf("%d", res[i]); printf(i == len ? "\n" : " "); } return 0; }