Saleh127

UVA 12778 / Searching

May 29th, 2022
1,005
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. /***
  2.  created: 2022-05-28-16.51.29
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. int main()
  13. {
  14.     ios_base::sync_with_stdio(0);
  15.     cin.tie(0);
  16.     cout.tie(0);
  17.  
  18.  
  19.     test
  20.     {
  21.         ll n,m=0,i,j,k,l=0;
  22.  
  23.  
  24.         cin>>n;
  25.  
  26.         ll a[n+4];
  27.  
  28.         for(i=0;i<n;i++)
  29.         {
  30.             cin>>a[i];
  31.             m+=(i+1)*(n-i)*a[i];
  32.         }
  33.  
  34.  
  35.         for(i=0;i<n;i++)
  36.         {
  37.             ll lo=i-1,hi=i+1;
  38.  
  39.             while(lo>=0 && a[lo]>=a[i]) lo--;
  40.             while(hi<n && a[hi]>a[i]) hi++;
  41.  
  42.             l+=((i-lo)*(hi-i)*(hi-lo)/2)*a[i];
  43.  
  44.         }
  45.  
  46.         cout<<"Case "<<cs<<": "<<m-l<<nl;
  47.  
  48.     }
  49.  
  50.  
  51.     get_lost_idiot;
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment