fork download
  1. #include <cstdio>
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <fstream>
  7. #include<stdlib.h>
  8.  
  9. #define rep( i, l, r ) for (int i = l; i <= r; i++)
  10. #define down( i, l, r ) for (int i = l; i >= r; i--)
  11.  
  12. using namespace std;
  13.  
  14. int n, s, num[100009], Left[2][200009], Right[2][200009];
  15.  
  16. int main()
  17. {
  18. scanf("%d%d", &n, &s);
  19. rep(i, 1, n) scanf("%d", &num[i]);
  20. rep(k, 1, n) if (num[k] == s)
  21. {
  22. int a = 0; int b = 0;
  23. Left[0][n] = Right[0][n] = 1;
  24. down(i, k-1, 1)
  25. {
  26. if (num[i] < num[k]) a++; else b++;
  27. Left[(k-i) % 2][a-b+n]++;
  28. }
  29. a = b = 0;
  30. rep(i, k+1, n)
  31. {
  32. if (num[i] < num[k]) a++; else b++;
  33. Right[(i-k) % 2][a-b+n]++;
  34. }
  35. int ans = 0;
  36. rep(i, 1, 2*n-1) ans += Left[0][i] * Right[0][2*n-i] + Left[1][i] * Right[1][2*n-i];
  37. printf("%d", ans);
  38. return 0;
  39. }
  40. return 0;
  41. }
  42.  
Success #stdin #stdout 0s 6860KB
stdin
7 4
5 7 2 4 3 1 6
stdout
4