fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <stack>
  5. #include <list>
  6. typedef long long int ll;
  7. using namespace std;
  8. ll a[1000000+100],b[1000000+100]={0};
  9. int main() {
  10. // your code goes here
  11. ll n;
  12. cin>>n;
  13. for(ll i=0;i<n;i++)cin>>a[i];
  14. list<ll> k1,k2;
  15. for(ll i=0,j=n-1;i<n;i++,j--)
  16. {
  17. ll k11,k22;
  18. while(!k1.empty()&&a[k1.back()]>a[i])
  19. k1.pop_back();
  20. while(!k2.empty()&&a[k2.back()]>a[j])
  21. k2.pop_back();
  22. if(k1.empty())k11=0;
  23. else k11 = 1+k1.back();
  24. if(k2.empty())k22=n-1;
  25. else k22 = k2.back()+1;
  26. b[a[i]] += (i-k11+1)*(k22-i+1);
  27. k1.push_back(i);
  28. k2.push_back(i);
  29.  
  30. }
  31. ll q;
  32. cin>>q;
  33. while(q--)
  34. {
  35. ll temp;
  36. cin>>temp;
  37. cout<<b[temp]<<endl;
  38. }
  39. return 0;
  40. }
Success #stdin #stdout 0s 18864KB
stdin
5
4 1 2 3 4
4
3
4
6
1
stdout
0
6
0
8