func_code_string: #include <bits/stdc++.h>   using namespace std;   typedef int arr[500005];   arr Next, a, Sa, Lcp, pos, tmp, S;   int n, len;   char P[500005];   vector<int> Val[2 * 500005];   bool cmp(int p, int q) {     if (pos[p] != pos[q]) return pos[p] < pos[q];     p += len;     q += len;     if (p <= n && q <= n) return pos[p] < pos[q];     return p > q;   }   void BuildSA() {     for (int i = 1; i <= n; i++) {       Sa[i] = i;       pos[i] = ((P[i] ==  ( ) ? 1 : 2);     }     for (len = 1; len <= n; len *= 2) {       sort(Sa + 1, Sa + n + 1, cmp);       for (int i = 1; i <= n; i++) tmp[i] = tmp[i - 1] + cmp(Sa[i - 1], Sa[i]);       for (int i = 1; i <= n; i++) pos[Sa[i]] = tmp[i];     }   }   void BuildLCP() {     int cur = 0;     for (int i = 1; i <= n; i++) {       int j = Sa[pos[i] - 1];       if (j == 0) continue;       while (P[i + cur] == P[j + cur]) cur++;       Lcp[pos[i]] = cur;       if (cur) cur--;     }   }   int Get(int V, int L, int R) {     int l = lower_bound(Val[V].begin(), Val[V].end(), L) - Val[V].begin();     int r = upper_bound(Val[V].begin(), Val[V].end(), R) - Val[V].begin();     return max(r - l, 0);   }   int main() {     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> P[i];       a[i] = a[i - 1];       if (P[i] ==  ( ) a[i]++;       if (P[i] ==  ) ) a[i]--;       Val[a[i] + 500005].push_back(i);     }     int top = 0;     S[0] = n + 1;     for (int i = n; i >= 0; i--) {       while (a[i] <= a[S[top]] && top) top--;       Next[i] = S[top];       S[++top] = i;     }     BuildSA();     BuildLCP();     long long ans = 0;     for (int i = 1; i <= n; i++) {       int L = Sa[i] + Lcp[i];       int R = Next[Sa[i] - 1] - 1;       ans += Get(a[Sa[i]] - 1 + 500005, L, R);     }     cout << ans;   }   