Advertisement
dllbridge

Untitled

Nov 8th, 2022
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #ifdef LOCAL
  6. #include "algo/debug.h"
  7. #else
  8. #define debug(...) 42
  9. #endif
  10.  
  11. template <typename T>
  12.  
  13. ////////////////////////////////////////////
  14. class fenwick                            //
  15. {
  16.     public:
  17.     vector<T> fenw;
  18.    
  19.     int n;
  20.  
  21.    
  22.    fenwick(int _n) : n(_n)
  23.    {
  24.         fenw.resize(n);
  25.    }
  26.  
  27.  
  28.     void modify(int x, T v)
  29.     {
  30.      
  31.         while (x < n)
  32.         {
  33.             fenw[x] += v;
  34.             x |= (x + 1);
  35.         }
  36.     }
  37.  
  38.  
  39.   T get(int x)
  40.   {
  41.      
  42.         T v{};
  43.    
  44.         while (x >= 0)
  45.         {
  46.             v += fenw[x];
  47.             x = (x & (x + 1)) - 1;
  48.         }
  49.    
  50.         return v;
  51.    }
  52.  
  53. };
  54.  
  55. ////////////////////////////////////////////
  56. int main()                               //
  57. {
  58.    
  59.   ios::sync_with_stdio(false);
  60.  
  61.   cin.tie(0);
  62.  
  63.   int tt;
  64.  
  65.   cin >> tt;
  66.  
  67.   while (tt--)
  68.   {
  69.         int n;
  70.         cin >> n;
  71.         string s;
  72.        
  73.         cin >> s;
  74.        
  75.         vector<int> a(n + 1);
  76.        
  77.         for (int i = 0; i < n; i++)   a[i + 1] = a[i] + (s[i] == '(' ? 1 : -1);
  78.        
  79.         int mn = *min_element(a.begin(), a.end());
  80.    
  81.     for (int i = 0; i <= n; i++)
  82.     {
  83.         a[i] -= mn;
  84.         assert(0 <= a[i] && a[i] <= n);
  85.     }
  86.    
  87.     long long ans = 0;
  88.    
  89.     fenwick<long long> f0(n + 1);
  90.     fenwick<long long> f1(n + 1);
  91.    
  92.     for (int i = 0; i <= n; i++)
  93.     {
  94.         ans += f0.get(a[i]) * a[i] - f1.get(a[i]);
  95.         f0.modify(a[i], +1);
  96.         f1.modify(a[i], +a[i]);
  97.     }
  98.    
  99.     vector<long long> pref(n + 2);
  100.    
  101.     for (int i = 0; i <= n; i++)  pref[i + 1] = pref[i] + a[i];
  102.    
  103.     vector<int> pr(n + 1);
  104.     vector<int> ne(n + 1);
  105.    
  106.     for (int i = 0; i <= n; i++)
  107.     {
  108.         pr[i] = i - 1;
  109.         ne[i] = i + 1;
  110.     }
  111.    
  112.     vector<vector<int>> at(n + 1);
  113.    
  114.     for (int i = 0; i <= n; i++) at[a[i]].push_back(i);
  115.    
  116.     for (int val = n; val >= 0; val--)
  117.     {
  118.         for (int i : at[val])
  119.         {
  120.            
  121.             int L = pr[i] + 1;
  122.             int R = ne[i];
  123.             long long cur = (pref[i] - pref[L]) - (long long) a[i] * (i - L);
  124.        
  125.             cur *= R - i;
  126.             ans += cur;
  127.        
  128.             if (pr[i] != -1)    ne[pr[i]] = ne[i];
  129.        
  130.             if (ne[i] != n + 1) pr[ne[i]] = pr[i];
  131.        
  132.         }
  133.     }
  134.    
  135.     cout << ans << '\n';
  136.    
  137.   }
  138.  
  139.   return 0;
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement