Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using ll = long long;
- using ld = long double;
- using namespace std;
- int N = 0;
- struct node {
- int lb, rb;
- int lazy = 0, a = 0;
- node *l = 0, *r = 0;
- pair<int, int> maxim, minim;
- node(int _lb, int _rb) {
- lb = _lb; rb = _rb;
- if (lb + 1 < rb) {
- int t = (lb + rb) / 2;
- l = new node(lb, t);
- r = new node(t, rb);
- }
- maxim = { lb, 0 };
- minim = { lb, 0 };
- }
- void add(int x, int _l, int _r) {
- if (_r < lb || _l >= rb)
- return;
- if (_l <= lb && rb - 1 <=_r)
- lazy += x;
- else {
- l->add(x, _l, _r); r->add(x, _l, _r);
- auto a = l->maxim, b = r->maxim;
- a.second += l->lazy;
- b.second += r->lazy;
- if (a.second < b.second)
- swap(a, b);
- maxim = a;
- a = l->minim, b = r->minim;
- a.second += l->lazy;
- b.second += r->lazy;
- if (a.second > b.second)
- swap(a, b);
- minim = a;
- }
- }
- void push() {
- maxim.second += lazy;
- minim.second += lazy;
- if (lb + 1 == rb) {
- lazy = 0;
- return;
- }
- l->lazy += lazy; r->lazy += lazy;
- lazy = 0;
- }
- pair<int, int> max(int _l, int _r) {
- push();
- if (_r < lb || _l >= rb)
- return{ -1, -1 };
- if (_l <= lb && _r >= rb - 1)
- return maxim;
- else {
- auto a = l->max(_l, _r), b = r->max(_l, _r);
- if (a.second < b.second)
- swap(a, b);
- return a;
- }
- }
- pair<int, int> min(int _l, int _r) {
- push();
- if (_r < lb || _l >= rb)
- return{ -1, -1 };
- if (_l <= lb && _r >= rb - 1)
- return minim;
- else {
- auto a = l->min(_l, _r), b = r->min(_l, _r);
- if (a.second > b.second)
- swap(a, b);
- return a;
- }
- }
- };
- int main() {
- ios::sync_with_stdio(0);
- int n; cin >> n;
- vector<char> a(n);
- int it = 0;
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- if (a[i] == 'R')
- it++;
- else if (a[i] == 'L')
- it--;
- else
- N = max(N, it);
- }
- N++;
- node *d = new node(0, N);
- vector<char> arr(N);
- it = 0;
- int bal = 0;
- for (int i = 0; i < n; i++) {
- if (a[i] == 'L')
- it--;
- else if (a[i] == 'R')
- it++;
- else if (a[i] == '(') {
- if (a[i] == arr[it])
- continue;
- if (arr[it] == ')') {
- d->add(2, it, N - 1);
- bal += 2;
- }
- else {
- d->add(1, it, N - 1);
- bal++;
- }
- arr[it] = '(';
- }
- else if (a[i] == ')') {
- if (a[i] == arr[it])
- continue;
- if (arr[it] == '(') {
- d->add(-2, it, N - 1);
- bal -= 2;
- }
- else {
- d->add(-1, it, N - 1);
- bal--;
- }
- arr[it] = ')';
- }
- else {
- if (arr[it] == '(') {
- bal--;
- d->add(-1, it, N - 1);
- }
- else if (arr[it] == ')') {
- bal++;
- d->add(1, it, N - 1);
- }
- arr[it] = 'a';
- }
- if (bal != 0 || d->min(0, N - 1).second)
- cout << -1;
- else
- cout << d->max(0, N - 1).second;
- cout << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement