malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   long long inf = 2147483647;   const long long ll = 9223372036854775807, ninf = 1000000000;   const double eps = 1e-6;   const long long nll = 1000000000000000000;   class MulticoreProcessingEasy {    public:     int fastestTime(int jobLength, int corePenalty, vector<int> speed,                     vector<int> cores) {}   };   int main() {     int a;     while (cin >> a) {       int A[100005] = {};       for (int i = 1; i <= a; i++) cin >> A[i];       int no = 0;       for (int i = 1; i <= a; i++) {         if (A[i] != A[A[i]]) {           no = 1;         }       }       if (no) {         cout << -1 <<   n ;         continue;       }       int b[100005] = {};       int c[100005] = {};       int vis[100005] = {};       int now = 0;       for (int i = 1; i <= a; i++) {         if (!vis[A[i]]) {           now++;           vis[A[i]] = now;           c[now] = A[i];         }         b[i] = vis[A[i]];       }       cout << now <<   n ;       for (int i = 1; i <= a; i++) cout << b[i] <<    ;       cout <<   n ;       for (int i = 1; i <= now; i++) cout << c[i] <<    ;       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long powm(long long base, long long exp, long long mod = 1000000007) {     long long ans = 1;     while (exp) {       if (exp & 1) ans = (ans * base) % mod;       exp >>= 1, base = (base * base) % mod;     }     return ans;   }   long long ctl(char x, char an =  a ) { return (long long)(x - an); }   char ltc(long long x, char an =  a ) { return (char)(x + an); }   long long a[1000005], nxt[1000005], suff[1000005];   bool overflow(long long a, long long b) {     long double ta = a, tb = b;     if (3e18 / ta < tb) return 1;     return 0;   }   int main() {     long long n, k, ans = 0;     cin >> n >> k, nxt[n + 1] = n + 1;     for (long long i = 1; i < n + 1; i++) cin >> a[i];     for (long long i = n; i >= 1; i--) {       suff[i] = suff[i + 1] + a[i];       if (a[i] == 1)         nxt[i] = nxt[i + 1];       else         nxt[i] = i;     }     for (long long i = 1; i < n + 1; i++) {       long long p = 1, second = 0;       long long iter = 0;       for (long long j = i; j < n + 1; j++) {         if (a[j] == 1) {           long long tot_ones = nxt[j] - j;           if (p % k == 0) {             long long req_ones = p / k - second;             if (req_ones > 0 and req_ones <= tot_ones) {               ans++;             }           }           j = nxt[j] - 1;           second += tot_ones;         } else {           if (overflow(p, a[j])) break;           p *= a[j];           second += a[j];           if (p == k * second) ans++;         }         if (p > k * suff[i]) break;       }     }     cout << ans;     return 0;   }
#include<bits/stdc++.h>   using namespace std;   #define w(t) int t; cin>>t; while(t--)   #define int long long int   #define I int32_t   #define asc(i,a,n) for(int i=a;i<n;i++)   #define dsc(i,n,a) for(int i=n;i>=a;i--)   #define vi vector<int>   #define pi pair<int,int>   #define PB push_back   #define MP make_pair   I main(){       ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);       w(t){           int n;           cin>>n;           int a[n];           int f[2*n+1];           asc(i,0,2*n+1){               f[i]=1000000;           }           asc(i,0,n){               cin>>a[i];               f[a[i]]=i+1;           }           int ans=0;           asc(k,3,2*n){               int r=sqrt(k);               asc(j,1,r+1){                   if(k%j==0&&k!=j*j){                       if(f[j]+f[k/j]==k){                           ans++;                       }                   }               }           }           cout<<ans<<endl;       }   }
#include <bits/stdc++.h>   using namespace std;   struct team {     string nam;     int pt, diff, get;   } T[55];   int n;   void gao(string s, int pt, int diff, int get) {     for (int i = 0; i < n; i++) {       if (s == T[i].nam) {         T[i].pt += pt;         T[i].diff += diff;         T[i].get += get;         break;       }     }   }   bool cmp(team a, team b) {     if (a.pt != b.pt) return a.pt > b.pt;     if (a.diff != b.diff) return a.diff > b.diff;     return a.get > b.get;   }   void tostring(string &a, string &b, char *s) {     int l = strlen(s);     a =   ;     b =   ;     for (int i = 0; i < l; i++) {       if (s[i] ==  - ) {         for (int j = i + 1; j < l; j++) {           b += s[j];         }         return;       }       a += s[i];     }   }   int main() {     while (cin >> n) {       for (int i = 0; i < n; i++)         cin >> T[i].nam, T[i].pt = 0, T[i].diff = 0, T[i].get = 0;       ;       for (int i = 0; i < n * (n - 1) / 2; i++) {         char str[66];         string a, b;         int sa, sb;         scanf( %s %d:%d , str, &sa, &sb);         tostring(a, b, str);         if (sa > sb) {           gao(a, 3, sa - sb, sa);           gao(b, 0, sb - sa, sb);         } else if (sa == sb) {           gao(a, 1, sa - sb, sa);           gao(b, 1, sb - sa, sb);         } else {           gao(a, 0, sa - sb, sa);           gao(b, 3, sb - sa, sb);         }       }       sort(T, T + n, cmp);       vector<string> res;       res.clear();       for (int i = 0; i < n / 2; i++) {         res.push_back(T[i].nam);       }       sort(res.begin(), res.end());       for (int i = 0; i < res.size(); i++) {         cout << res[i] << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   char s[200005], t[200005];   vector<int> pos[200005];   int main() {     int T;     scanf( %d , &T);     while (T--) {       for (int i = 0; i <= 30; i++) pos[i].clear();       scanf( %s%s , s, t);       int l1 = strlen(s), l2 = strlen(t);       int now = -1, ans = 0;       for (int i = 0; i < l1; i++) pos[s[i] -  a ].push_back(i);       for (int i = 0; i < l2; i++) {         int nxt =             upper_bound(pos[t[i] -  a ].begin(), pos[t[i] -  a ].end(), now) -             pos[t[i] -  a ].begin();         if (nxt == pos[t[i] -  a ].size()) {           if (now == -1) {             ans = -1;             break;           } else {             now = -1;             ans++;             i--;           }         } else           now = pos[t[i] -  a ][nxt];       }       if (ans != -1 && now != -1) ans++;       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   struct node {     long long A, B;   };   bool func(node s1, node s2) { return s1.A < s2.A; }   int main() {     long long n;     cin >> n;     std::vector<node> b(n);     vector<long long> a(n);     ;     for (long long i = 0; i <= n - 1; i++) {       cin >> a[i];     }     for (long long j = 0; j <= n - 1; j++) {       cin >> b[j].A;       b[j].B = j;     }     sort(b.begin(), b.end(), func);     sort(a.begin(), a.end(), greater<int>());     vector<long long> c(n);     ;     for (long long i = 0; i <= n - 1; i++) {       c[b[i].B] = a[i];     }     for (long long i = 0; i <= n - 1; i++) {       cout << c[i] <<    ;     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, i, f = 0, a;     scanf( %d , &n);     for (i = 0; i < n; i++) {       scanf( %d , &a);       if (a == 1) f = 1;     }     if (f)       printf( %d , -1);     else       printf( %d , 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma comment(linker,  /stack:200000000 )   #pragma GCC optimize( Ofast )   #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native )   int32_t main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long a, b;     cin >> a >> b;     long long x1 = a, x2 = b, ans1 = 0, ans2 = 0;     if (a < b)       ans1 += a;     else       ans2 += b;     while (x1 != x2) {       if (x1 < x2) {         if ((x1 + a) < x2)           ans1 += a;         else if ((x1 + a) == x2) {           if (a < b)             ans2 += a;           else             ans1 += a;         } else           ans2 += x2 - x1;         x1 += a;       } else {         if ((x2 + b) < x1)           ans2 += b;         else if ((x2 + b) == x1) {           if (a < b)             ans2 += b;           else             ans1 += b;         } else           ans1 += x1 - x2;         x2 += b;       }     }     if (ans1 == ans2)       cout <<  Equal ;     else if (ans1 > ans2)       cout <<  Dasha ;     else       cout <<  Masha ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int t, tc;     cin >> t;     for (tc = 0; tc < t; tc++) {       int n, i;       cin >> n;       vector<int> v(n), mx(n + 1, 0), mn(n + 1, n + 5);       for (i = 0; i < n; i++) {         cin >> v[i];         mx[v[i]] = i;         mn[v[i]] = i;       }       for (i = 2; i <= n; i++) {         mx[i] = max(mx[i - 1], mx[i]);         mn[i] = min(mn[i - 1], mn[i]);       }       for (i = 1; i <= n; i++) {         if (mx[i] - mn[i] == i - 1) {           cout <<  1 ;         } else {           cout <<  0 ;         }       }       cout <<   n ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long lower, upper, res;   char num1[65536], num2[65536], num[65536];   void DFS(int depth, bool lower_flag, bool upper_flag) {     if (depth == 10) {       long long a = 0, b = 0;       bool flag = false;       for (int i = 0; i < 10; i++) {         if (num[i] !=  0 ) {           flag = true;         }         if (flag) {           a = a * 10 + num[i] -  0 ;           b = b * 10 + 9 - (num[i] -  0 );         }       }       if (lower <= a && a <= upper) {         res = max(res, a * b);       }       return;     }     char a, b;     if (lower_flag && upper_flag) {       a = min(num1[depth], num2[depth]);       b = max(num1[depth], num2[depth]);     } else if (lower_flag) {       a = num1[depth];       b =  9 ;     } else if (upper_flag) {       a =  0 ;       b = num2[depth];     } else {       a =  0 ;       b =  9 ;     }     if (a <=  4  &&  4  <= b) {       num[depth] =  4 ;       DFS(depth + 1, lower_flag && num1[depth] ==  4 ,           upper_flag && num2[depth] ==  4 );     }     if (a <=  5  &&  5  <= b) {       num[depth] =  5 ;       DFS(depth + 1, lower_flag && num1[depth] ==  5 ,           upper_flag && num2[depth] ==  5 );     }     num[depth] = a;     DFS(depth + 1, lower_flag && num1[depth] == a,         upper_flag && num2[depth] == a);     num[depth] = b;     DFS(depth + 1, lower_flag && num1[depth] == b,         upper_flag && num2[depth] == b);   }   int main() {     scanf( %lld%lld , &lower, &upper);     sprintf(num1,  %010lld , lower);     sprintf(num2,  %010lld , upper);     DFS(0, true, true);     printf( %lld n , res);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int power(long long int a, long long int b) {     long long int ans = 1;     while (b) {       if (b & 1) {         ans = ((ans % 1000000007) * (a % 1000000007)) % 1000000007;       }       a = ((a % 1000000007) * (a % 1000000007)) % 1000000007;       b = b >> 1;     }     return ans;   }   long long int powe(long long int a, long long int b) {     long long int ans = 1;     while (b) {       if (b & 1) {         ans = a * ans;       }       a = a * a;       b = b >> 1;     }     return ans;   }   void solve() {     string s;     cin >> s;     vector<pair<char, long long int> > v;     for (int i = 0; i < s.length(); i++) {       long long int curlen = 1;       while (i < s.length() - 1 && s[i + 1] == s[i]) {         curlen++;         i++;       }       v.push_back({s[i], curlen});     }     if (v.size() % 2 == 0) {       cout << 0 << endl;     } else {       int i = 0;       for (i = 0; i < v.size() / 2; i++) {         if (v[i].first == v[v.size() - 1 - i].first &&             v[i].second + v[v.size() - 1 - i].second >= 3) {         } else {           cout << 0 << endl;           return;         }       }       if (v[v.size() / 2].second >= 2)         cout << v[v.size() / 2].second + 1 << endl;       else         cout << 0 << endl;       return;     }   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     long long int t = 1;     while (t > 0) {       solve();       t--;     }   }
#include <bits/stdc++.h>   using namespace std;   vector<int> v(7);   int main() {     int n;     string s;     cin >> n;     for (int j = 0; j < n; j++) {       auto a = 0;       cin >> s;       for (auto &c : s) {         int r = c - 48;         v.at(a) += r;         a++;       }     }     auto result = *max_element(v.begin(), v.end());     cout << result;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 200010;   const long long int MOD = 998244353;   long long int mod(long long int n) { return (n % MOD + MOD) % MOD; }   void solve() {     long long int n;     cin >> n;     long long int a[n];     long long int sum = 0;     for (long long int i = 0; i < n; i++) {       cin >> a[i];       sum += a[i];     }     long long int bob = 0, alice = 0, prev_bob = 0, prev_alice = 0, sum1 = 0,                   sum2 = 0, curr1 = 0, curr2 = 0;     long long int i = 0, j = n - 1, f = 1, count = 0;     sum1 = a[0];     prev_alice = a[0];     count = 1;     i = 1;     while (i <= j) {       if (f == 0) {         while (alice <= prev_bob && i <= j) {           alice += a[i];           i++;         }         count++;         prev_alice = alice;         sum1 += alice;         alice = 0;         f = 1;       } else {         while (bob <= prev_alice && i <= j) {           bob += a[j];           j--;         }         count++;         prev_bob = bob;         sum2 += bob;         bob = 0;         f = 0;       }     }     cout << count <<     << sum1 <<     << sum2 << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long int T = 1;     cin >> T;     for (long long int i = 0; i < T; i++) solve();   }
#include <bits/stdc++.h>   using namespace std;   const int MAX_N = 2e5 + 1;   int main() {     int N, Q;     cin >> N >> Q;     set<int> all;     multiset<int> gaps;     for (int i = 0; i < N; ++i) {       int x;       cin >> x;       all.insert(x);     }     for (auto iter = all.begin(); iter != all.end(); ++iter) {       auto nxt = next(iter);       if (nxt != all.end()) {         gaps.insert(*nxt - *iter);       }     }     cout << ((all.size() > 2)                  ? ((*prev(all.end()) - *all.begin()) - *prev(gaps.end()))                  : 0)          << endl;     while (Q--) {       int type;       int x;       cin >> type >> x;       if (type) {         auto iter = all.insert(x).first;         if (all.size() > 1) {           if (iter == all.begin()) {             gaps.insert(*next(iter) - *iter);           } else if (iter == prev(all.end())) {             gaps.insert(*iter - *prev(iter));           } else {             int gap_l = *next(iter) - *iter;             int gap_r = *iter - *prev(iter);             auto gap_old = gaps.find(*next(iter) - *prev(iter));             gaps.erase(gap_old);             gaps.insert(gap_l);             gaps.insert(gap_r);           }         }       } else {         auto cur = all.find(x);         if (cur == all.begin() && all.size() > 1) {           gaps.erase(gaps.find(*next(cur) - *cur));         } else if (cur == prev(all.end()) && all.size() > 1) {           gaps.erase(gaps.find(*cur - *prev(cur)));         } else if (all.size() > 2) {           auto prv = prev(cur);           auto nxt = next(cur);           auto gap1_old = gaps.find(*cur - *prv);           gaps.erase(gap1_old);           auto gap2_old = gaps.find(*nxt - *cur);           gaps.erase(gap2_old);           gaps.insert(*nxt - *prv);         }         all.erase(x);       }       cout << ((all.size() > 2)                    ? ((*prev(all.end()) - *all.begin()) - *prev(gaps.end()))                    : 0)            << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     int n, m;     cin >> n >> m;     vector<vector<long long> > PRE(32, vector<long long>(n + 2, 0)),         ZEROS(32, vector<long long>(n + 2, 0));     vector<long long> L(m), R(m), Q(m);     for (int i = 0; i < m; ++i) {       cin >> L[i] >> R[i] >> Q[i];       for (int j = 0; j < 32; ++j) {         int ind = 1 << j;         if (ind & Q[i]) {           PRE[j][L[i]]++;           PRE[j][R[i] + 1]--;         }       }     }     for (int i = 0; i < 32; ++i) {       for (int j = 1; j < n + 2; ++j) {         PRE[i][j] += PRE[i][j - 1];         if (PRE[i][j] == 0) ZEROS[i][j]++;         ZEROS[i][j] += ZEROS[i][j - 1];       }     }     for (int i = 0; i < m; ++i) {       int l = L[i], r = R[i], q = Q[i];       for (int j = 0; j < 32; ++j) {         int ind = 1 << j;         if (!(ind & q) && ZEROS[j][r] - ZEROS[j][l - 1] == 0) {           cout <<  NO n ;           return 0;         }       }     }     cout <<  YES n ;     for (int i = 1; i <= n; ++i) {       if (i != 1) cout <<    ;       int aux = 0;       for (int j = 0; j < 32; ++j) {         int ind = 1 << j;         if (PRE[j][i]) {           aux |= ind;         }       }       cout << aux;     }     cout <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int toNumber(string s) {     int Number;     if (!(istringstream(s) >> Number)) Number = 0;     return Number;   }   string toString(int number) {     ostringstream ostr;     ostr << number;     return ostr.str();   }   int main() {     int n, m, k;     while (cin >> n >> m >> k) {       long long lo = 0;       long long hi = m + 1;       while (hi - lo > 1) {         long long mi = (lo + hi) / 2;         long long voy;         if (k >= mi) {           voy = (mi - 1) * mi / 2 + (k - mi);         } else {           voy = (mi - 1) * mi / 2 - (mi - k) * (mi - k + 1) / 2;         }         if (n - k + 1 >= mi) {           voy += (mi + 1) * mi / 2 + (n - k + 1 - mi);         } else {           voy +=               (mi + 1) * mi / 2 - (mi - (n - k + 1) + 1) * (mi - (n - k + 1)) / 2;         }         if (voy <= m) {           lo = mi;         } else {           hi = mi;         }       }       cout << lo << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int x = 0, y = 0;     int n;     char a[205] = {  0 };     scanf( %d , &n);     scanf( %s , a + 1);     for (int i = 1; i <= n; i++) {       if (a[i] ==  0 )         x++;       else         y++;     }     if (x == y) {       printf( 2 n );       printf( %c  , a[1]);       for (int i = 2; i <= n; i++) printf( %c , a[i]);       printf(  n );     } else {       printf( 1 n );       for (int i = 1; i <= n; i++) printf( %c , a[i]);       printf(  n );     }   }
#include <bits/stdc++.h>   using namespace std;   struct node {     int x, y, id;   };   node A[1000100];   int ans[1000100];   vector<int> P[1200];   int cmp1(node a, node b) { return a.x < b.x; }   int cmp2(int a, int b) { return A[a].y < A[b].y; }   const int Base = 1000;   int main() {     int n;     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d %d , &A[i].x, &A[i].y), A[i].id = i;     sort(A + 1, A + n + 1, cmp1);     for (int i = 1; i <= n; i++) {       P[(A[i].x - 1) / Base + 1].push_back(i);     }     int k = 0;     for (int i = 0; i <= 1018; i++) {       sort(P[i].begin(), P[i].end(), cmp2);       for (int j = 0; j < P[i].size(); j++) {         ans[++k] = A[P[i][j]].id;       }     }     for (int i = 1; i <= k; i++) printf( %d  , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 5e3;   vector<int> od;   vector<int> ev;   void manachar(string s) {     string x =  @# ;     for (auto c : s) x.push_back(c), x.push_back( # );     x.push_back( $ );     s = x;     int n = s.size();     int l = 0, r = 0;     vector<int> pp(n);     for (int i = 0; i < n; i++) {       pp[i] = min(r - i, pp[l + r - i]);       while (s[i - pp[i]] == s[i + pp[i]]) pp[i]++;       if (i + pp[i] > r) l = i - pp[i], r = i + pp[i];     }     for (int i = 2; i < n - 1; i += 2) od.push_back(pp[i] - 1);     for (int i = 3; i < n - 1; i += 2) ev.push_back(pp[i] - 1);   }   const int M = (int)(2e6 + 239);   int pref[M], c;   string solve_palindrome(const string& s) {     string a = s;     reverse(a.begin(), a.end());     a = s +  #  + a;     c = 0;     for (int i = 1; i < (int)a.size(); i++) {       while (c != 0 && a[c] != a[i]) c = pref[c - 1];       if (a[c] == a[i]) c++;       pref[i] = c;     }     return s.substr(0, c);   }   void solve(string t) {     int l = 0;     while (l < (int)t.size() - l - 1) {       if (t[l] != t[(int)t.size() - l - 1]) break;       l++;     }     if (l > 0) cout << t.substr(0, l);     if ((int)t.size() > 2 * l) {       string s = t.substr(l, (int)t.size() - 2 * l);       string a = solve_palindrome(s);       reverse(s.begin(), s.end());       string b = solve_palindrome(s);       if ((int)a.size() < (int)b.size()) swap(a, b);       cout << a;     }     if (l > 0) cout << t.substr((int)t.size() - l, l);     cout <<   n ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int t;     cin >> t;     while (t--) {       string s, r;       cin >> r;       s = r;       od.clear();       ev.clear();       reverse(r.begin(), r.end());       if (s == r) {         cout << s;         cout << endl;         continue;       }       int pt = 0;       for (int i = 0;; i++) {         if (s[i] == s[(int)s.size() - 1 - i])           pt++;         else           break;       }       r = s.substr(pt, (int)s.size() - pt - pt);       solve(s);       continue;       manachar(r);       int a = 0, b = 0;       for (int i = 0; i < od.size(); i++) {         if (i - od[i] / 2 == 0) a = max(a, od[i]);         if (i + od[i] / 2 == r.size() - 1) b = max(b, od[i]);       }       for (int i = 0; i < ev.size(); i++) {         if (i - ev[i] / 2 == -1) a = max(a, ev[i]);         if (i + ev[i] / 2 == r.size() - 1) b = max(b, ev[i]);       }       if (a >= b) {         for (int i = 0; i < pt; i++) cout << s[i];         for (int i = 0; i < a; i++) cout << r[i];         for (int i = pt - 1; i >= 0; i--) cout << s[i];         cout << endl;       } else {         reverse(r.begin(), r.end());         for (int i = 0; i < pt; i++) cout << s[i];         for (int i = 0; i < b; i++) cout << r[i];         for (int i = pt - 1; i >= 0; i--) cout << s[i];         cout << endl;       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MX = 400500;   int u_mask = (1 << 30) + ((1 << 30) - 1);   int a[MX];   int n;   namespace tree {   const int delta = 1 << 18;   int val[2 * delta];   int rev[2 * delta];   void init() {     val[delta] = rev[delta] = u_mask;     for (int i = 1; i <= n; i++) {       val[delta + i] = a[i];       rev[delta + i] = a[n + 1 - i];     }     for (int i = delta - 1; i > 0; i--) {       val[i] = val[2 * i] | val[2 * i + 1];       rev[i] = rev[2 * i] | rev[2 * i + 1];     }   }   int get_pos_before(int *val, int pos, int mask) {     mask ^= u_mask;     pos += delta;     while ((val[pos] & mask) == 0) {       if (pos & 1)         pos >>= 1;       else         pos--;     }     while (pos < delta) {       if (val[2 * pos + 1] & mask) {         pos = 2 * pos + 1;       } else         pos *= 2;     }     return pos - delta;   }   int get_pos_before(int pos) { return get_pos_before(val, pos, a[pos]); }   int get_pos_after(int pos) {     return n + 1 - get_pos_before(rev, n + 1 - pos, a[pos]);   }   }  // namespace tree   int main(int argc, char **argv) {     ios_base::sync_with_stdio(false);     cin.tie(0);     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> a[i];     }     tree::init();     long long ans = n;     ans *= n - 1;     ans /= 2;     map<int, int> min_pos;     for (int i = 1; i <= n; i++) {       long long l = tree::get_pos_before(i);       long long r = tree::get_pos_after(i);       auto &l2 = min_pos[a[i]];       if (l < l2) {         l = l2;       }       l2 = i;       ans -= (i - l) * (r - i);     }     cout << ans + n << endl;     return 0;   }