Advertisement
Ahmed_Negm

Untitled

Mar 5th, 2023
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. #define OO 2'000'000'000
  8. #define ull unsigned long long
  9. #define nl '\n'
  10. #define sz(x) (ll)(x.size())
  11. #define all(x) x.begin(),x.end()
  12. #define rall(s)  s.rbegin(), s.rend()
  13. #define getline(s) getline(cin>>ws,s)
  14. #define ceill(n, m) (((n) / (m)) + ((n) % (m) ? 1 : 0))
  15. #define pi  3.141592653589793
  16. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  17. #define multi_ordered_set tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
  18.  
  19.  
  20. void Fast_IO(){
  21. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  22. // freopen("filename.in", "r", stdin);
  23. // freopen("filename.txt", "w", stdout);
  24. #ifndef ONLINE_JUDGE
  25. freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  26. #endif
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int dx[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
  33. int dy[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  34.  
  35.  
  36.  
  37.  
  38. void solve(){
  39.  
  40.  
  41. ll n; cin>>n;
  42. vector<ll>v(n);
  43. for(auto&i:v) cin>>i;
  44.  
  45.  
  46.  
  47. ll mini = *min_element(all(v));
  48. ll maxi = *max_element(all(v));
  49.  
  50. if(mini == maxi){
  51.     cout<<n*(n+1)/2<<nl;
  52.     return;
  53. }
  54.  
  55. int pos_min=0;
  56. ll ans = 0;
  57. for(int i=0;i<n;i++){  
  58.     if(v[i]==mini) pos_min=i+1;
  59.     if(v[i] == maxi){
  60.         ans += pos_min*(n-i);
  61.         break;
  62.     }
  63. }
  64. int pos_max=0;
  65.  
  66.  
  67. for(int i=pos_min;i<n;i++){
  68.     if(v[i]==maxi) pos_max=i+1;
  69.     if(v[i]==mini and pos_max){
  70.         ans += (pos_max-pos_min) * (n-i);
  71.         break;
  72.     }
  73. }
  74. cout<<ans<<nl;
  75.  
  76.  
  77.  
  78. }
  79.  
  80. int main(){
  81.     Fast_IO();
  82. int t =1;
  83. cin>>t;
  84. while(t--){
  85. solve();
  86. }
  87. return 0;
  88. }  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement