1+ /*
2+
3+ Problem : Even Position 1997C
4+
5+ Approach : Use stack to keep track of all '(' and use them as soon as possible
6+
7+ Time Complexity : O(n)
8+ Space Complexity : O(n)
9+
10+ Submission Link : https://codeforces.com/problemset/submission/1997/355694114
11+
12+ */
13+
14+ #include < bits/stdc++.h>
15+ #include < bits/stdc++.h>
16+ #include < ext/pb_ds/assoc_container.hpp>
17+ #include < ext/pb_ds/tree_policy.hpp>
18+ using namespace std ;
19+ using namespace __gnu_pbds ;
20+
21+ #define fast_io ios::sync_with_stdio (false ); cin.tie(nullptr );
22+ #define int long long
23+ #define ull unsigned long long
24+ #define ld long double
25+ #define vi vector<int >
26+ #define vb vector<bool >
27+ #define vs vector<string>
28+ #define pii pair<int ,int >
29+ #define pll pair<long long ,long long >
30+ #define vpi vector<pii>
31+ #define vpl vector<pll>
32+ #define vvi vector<vi>
33+ #define mii map<int ,int >
34+ #define si set<int >
35+ #define osi ordered_set<int >
36+ #define loop (i,n ) for (int i = 0 ; i < n; i++)
37+ #define rloop (i,n ) for (int i = n - 1 ; i >= 0 ; i--)
38+ #define loop1 (i,a,b ) for (int i = a; i <= b; i++)
39+ #define rloop1 (i,a,b ) for (int i = a; i >= b; i--)
40+ #define pb push_back
41+ #define ppb pop_back
42+ #define mp make_pair
43+ #define F first
44+ #define S second
45+ #define lb lower_bound
46+ #define ub upper_bound
47+ #define all (x ) (x).begin(), (x).end()
48+ #define rall (x ) (x).rbegin(), (x).rend()
49+ #define sz (x ) ((int )(x).size())
50+ #define findbo (x ) find_by_order(x) // returns iterator to the k-th largest element (0-based)
51+ #define orderbk (x ) order_of_key(x) // returns number of items strictly smaller than x
52+ #define endl ' \n '
53+ #define printy cout<<" YES" <<endl
54+ #define printn cout<<" NO" <<endl
55+ #define vout (a ) for (int i = 0 ; i < a.size(); i++) cout << a[i] << ' ' ; cout << endl;
56+ #define vpout (a ) loop(i,sz(a)) cout <<a[i].F<<' ' << a[i].S<< endl; cout << endl;
57+ #define rt (x ){ cout<<x<<endl; return ; }
58+ template <typename T>
59+ using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // less_equal<T> for multiset, greater<T> for decreasing order
60+
61+ const int MOD = 1e9 + 7 ;
62+ const int INF = 1e9 ;
63+
64+ #ifndef ONLINE_JUDGE
65+ #define debug (x ) cerr<<#x<<" = " ; _print(x); cerr<<endl;
66+ template <typename T> void _print (const T& x){ cerr<<x; }
67+ template <typename T,typename U> void _print (const pair<T,U>& p){ cerr<<" (" ; _print (p.first ); cerr<<" , " ; _print (p.second ); cerr<<" )" ; }
68+ template <typename T> void _print (const vector<T>& v){ cerr<<" [" ; for (size_t i=0 ;i<v.size ();++i){ _print (v[i]); if (i+1 !=v.size ()) cerr<<" , " ; } cerr<<" ]" ; }
69+ template <typename T> void _print (const set<T>& s){ cerr<<" {" ; for (auto it=s.begin (); it!=s.end (); ++it){ _print (*it); if (next (it)!=s.end ()) cerr<<" , " ; } cerr<<" }" ; }
70+ template <typename T,typename U> void _print (const map<T,U>& m){ cerr<<" {" ; for (auto it=m.begin (); it!=m.end (); ++it){ _print (it->first ); cerr<<" : " ; _print (it->second ); if (next (it)!=m.end ()) cerr<<" , " ; } cerr<<" }" ; }
71+ #else
72+ #define debug (x )
73+ #endif
74+
75+ #define sumV (a ) accumulate(all(a), 0LL )
76+ #define minV (a ) min_element(all(a))
77+ #define maxV (a ) max_element(all(a))
78+
79+ int log2f (long long n) {
80+ if (n==0 ) return -1 ;
81+ int res = 0 ;
82+ while (n > 1 ){
83+ n >>= 1 ;
84+ res++;
85+ }
86+ return res;
87+ }
88+
89+ int binpow (int a, int n) {
90+ int result = 1 ;
91+ a %= MOD;
92+ while (n > 0 ) {
93+ if (n & 1 )
94+ result = (result * a) % MOD;
95+ a = (a * a) % MOD;
96+ n >>= 1 ;
97+ }
98+ return result;
99+ }
100+
101+ void solve (){
102+ int n;
103+ cin>>n;
104+ string s;
105+ cin>>s;
106+
107+ int ans = 0 ;
108+ stack<int > st;
109+ loop (i,n){
110+ if (s[i]==' (' ) st.push (i);
111+ else if (s[i]==' _' ){
112+ if (st.empty ()) st.push (i);
113+ else {
114+ ans += (i-st.top ());
115+ st.pop ();
116+ }
117+ }else {
118+ ans += (i-st.top ());
119+ st.pop ();
120+ }
121+ }
122+
123+ cout<<ans<<endl;
124+ }
125+
126+ int32_t main (){
127+ fast_io;
128+ int t=1 ;
129+ cin>>t;
130+ while (t--){
131+ solve ();
132+ }
133+ return 0 ;
134+ }
0 commit comments