CodeForces - 1263E Editor(线段树)
include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+10;
const int maxm = 2e5+10;
char str[maxn], res[maxn];
int n;
struct I {
ll sum, maxx, minn, lz;
} tree[maxn<<2];
inline void push_down(int rt, int len) {
if (tree[rt].lz) {
I &ls = tree[rt<<1];
I &rs = tree[rt<<1|1];
ll lz = tree[rt].lz;
ls.lz += lz;
ls.sum += len/2lz;
ls.maxx += lz;
ls.minn += lz;
rs.lz += lz;
rs.sum += (len - len/2)lz;
rs.maxx += lz;
rs.minn += lz;
tree[rt].lz = 0;
}
}
inline void push_up(int rt) {
tree[rt].maxx = max(tree[rt<<1].maxx, tree[rt<<1|1].maxx);
tree[rt].minn = min(tree[rt<<1].minn, tree[rt<<1|1].minn);
tree[rt].sum = tree[rt<<1].sum+tree[rt<<1|1].sum;
}
void update(int rt, int l, int r, int a, int b, int val) {
if (l>=a && r<=b) {
tree[rt].lz += val;
tree[rt].sum += (r-l+1)*val;
tree[rt].maxx += val;
tree[rt].minn += val;
return;
}
push_down(rt, r-l+1);
int mid = (l+r)>>1;
if (a<=mid) update(rt<<1, l, mid, a, b, val);
if (b>mid) update(rt<<1|1, mid+1, r, a, b, val);
push_up(rt);
}
int main() {
cin >> n >> str+1;
int p = 1, tot = 0;
for (int i = 1; i<=n; ++i) {
if (str[i]'R') ++p;
else if (str[i]'L') {
if (p!=1) --p;
}
else {
if (res[p]'(') update(1, 1, n, p, n, -1), --tot;
else if (res[p]')') update(1, 1, n, p, n, 1), ++tot;
res[p] = str[i];
if (res[p]'(') update(1, 1, n, p, n, 1), ++tot;
else if (res[p]')') update(1, 1, n, p, n, -1), --tot;
}
if (tree[1].minn < 0 || tot) printf(in ? "%d\n":"%d ", -1);
else printf(in ? "%d\n":"%d ", tree[1].maxx);
}
return 0;
}