fork download
  1. /* Author : Nguyen Thanh Tung - Tran Hung Dao High School for Gifted Student */
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. #define endl '\n'
  8. #define ft first
  9. #define sc second
  10. #define ll long long
  11. #define vll vector<ll>
  12. #define vp vector<pll>
  13. #define pll pair<ll, ll>
  14. #define ull unsigned long long
  15. #define FOR(i, l, r) for (ll i = (l); i <= (r); ++i)
  16. #define FORD(i, r, l) for (ll i = (r); i >= (l); --i)
  17.  
  18. const long long maxn = 1e6 + 9;
  19. const long long N = 3e4 + 9;
  20. const long long oo = 1e18 + 9;
  21. const long long INF = 0x3f;
  22. const long long MOD = 1e9 + 7;
  23.  
  24. #define TASK "code"
  25.  
  26. void solve()
  27. {
  28. ll n, m;
  29. cin >> n >> m;
  30. ll a[n + 1];
  31. ll pos = 0;
  32. FOR(i, 1, n)
  33. {
  34. ll x;
  35. cin >> x;
  36. if (x == m)
  37. {
  38. pos = i;
  39. }
  40. else if (x < m)
  41. {
  42. a[i] = -1;
  43. }
  44. else if (x > m)
  45. {
  46. a[i] = 1;
  47. }
  48. }
  49. ll s = 0;
  50. ll ans = 1;
  51. unordered_map<ll, ll> mp;
  52. FORD(i, pos - 1, 1)
  53. {
  54. s += a[i];
  55. if (s == 0)
  56. {
  57. ans++;
  58. }
  59. mp[s]++;
  60. }
  61. s = 0;
  62. FOR(i, pos + 1, n)
  63. {
  64. s += a[i];
  65. if (s == 0)
  66. {
  67. ans++;
  68. }
  69. ans += mp[-s];
  70. }
  71. cout << ans;
  72. }
  73.  
  74. int main()
  75. {
  76. ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  77. // freopen(TASK ".inp", "r", stdin);
  78. // freopen(TASK ".out", "w", stdout);
  79. solve();
  80. return 0;
  81. }
  82. /// Stay calm, read the question slowly and understand the question, it is often simpler than you imagine
  83.  
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
stdout
1