Advertisement
cincout

Untitled

Nov 29th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using ll = long long;
  4. using ld = long double;
  5.  
  6. using namespace std;
  7.  
  8. int N = 0;
  9.  
  10. struct node {
  11. int lb, rb;
  12. int lazy = 0, a = 0;
  13. node *l = 0, *r = 0;
  14. pair<int, int> maxim, minim;
  15.  
  16. node(int _lb, int _rb) {
  17. lb = _lb; rb = _rb;
  18. if (lb + 1 < rb) {
  19. int t = (lb + rb) / 2;
  20. l = new node(lb, t);
  21. r = new node(t, rb);
  22. }
  23. maxim = { lb, 0 };
  24. minim = { lb, 0 };
  25. }
  26.  
  27. void add(int x, int _l, int _r) {
  28. if (_r < lb || _l >= rb)
  29. return;
  30.  
  31. if (_l <= lb && rb - 1 <=_r)
  32. lazy += x;
  33. else {
  34. l->add(x, _l, _r); r->add(x, _l, _r);
  35. auto a = l->maxim, b = r->maxim;
  36. a.second += l->lazy;
  37. b.second += r->lazy;
  38. if (a.second < b.second)
  39. swap(a, b);
  40. maxim = a;
  41.  
  42. a = l->minim, b = r->minim;
  43. a.second += l->lazy;
  44. b.second += r->lazy;
  45. if (a.second > b.second)
  46. swap(a, b);
  47. minim = a;
  48. }
  49. }
  50.  
  51. void push() {
  52. maxim.second += lazy;
  53. minim.second += lazy;
  54. if (lb + 1 == rb) {
  55. lazy = 0;
  56. return;
  57. }
  58. l->lazy += lazy; r->lazy += lazy;
  59. lazy = 0;
  60. }
  61.  
  62. pair<int, int> max(int _l, int _r) {
  63. push();
  64.  
  65. if (_r < lb || _l >= rb)
  66. return{ -1, -1 };
  67.  
  68. if (_l <= lb && _r >= rb - 1)
  69. return maxim;
  70. else {
  71. auto a = l->max(_l, _r), b = r->max(_l, _r);
  72. if (a.second < b.second)
  73. swap(a, b);
  74. return a;
  75. }
  76. }
  77. pair<int, int> min(int _l, int _r) {
  78. push();
  79.  
  80. if (_r < lb || _l >= rb)
  81. return{ -1, -1 };
  82.  
  83. if (_l <= lb && _r >= rb - 1)
  84. return minim;
  85. else {
  86. auto a = l->min(_l, _r), b = r->min(_l, _r);
  87. if (a.second > b.second)
  88. swap(a, b);
  89. return a;
  90. }
  91. }
  92. };
  93.  
  94. int main() {
  95. ios::sync_with_stdio(0);
  96.  
  97. int n; cin >> n;
  98. vector<char> a(n);
  99. int it = 0;
  100. for (int i = 0; i < n; i++) {
  101. cin >> a[i];
  102. if (a[i] == 'R')
  103. it++;
  104. else if (a[i] == 'L')
  105. it--;
  106. else
  107. N = max(N, it);
  108. }
  109. N++;
  110. node *d = new node(0, N);
  111.  
  112. vector<char> arr(N);
  113. it = 0;
  114. int bal = 0;
  115. for (int i = 0; i < n; i++) {
  116. if (a[i] == 'L')
  117. it--;
  118. else if (a[i] == 'R')
  119. it++;
  120. else if (a[i] == '(') {
  121. if (a[i] == arr[it])
  122. continue;
  123. if (arr[it] == ')') {
  124. d->add(2, it, N - 1);
  125. bal += 2;
  126. }
  127. else {
  128. d->add(1, it, N - 1);
  129. bal++;
  130. }
  131. arr[it] = '(';
  132. }
  133. else if (a[i] == ')') {
  134. if (a[i] == arr[it])
  135. continue;
  136. if (arr[it] == '(') {
  137. d->add(-2, it, N - 1);
  138. bal -= 2;
  139. }
  140. else {
  141. d->add(-1, it, N - 1);
  142. bal--;
  143. }
  144. arr[it] = ')';
  145. }
  146. else {
  147. if (arr[it] == '(') {
  148. bal--;
  149. d->add(-1, it, N - 1);
  150. }
  151. else if (arr[it] == ')') {
  152. bal++;
  153. d->add(1, it, N - 1);
  154. }
  155. arr[it] = 'a';
  156. }
  157.  
  158. if (bal != 0 || d->min(0, N - 1).second)
  159. cout << -1;
  160. else
  161. cout << d->max(0, N - 1).second;
  162. cout << " ";
  163. }
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement