fork download
  1. // ROOT : DRAGON3012009
  2. #include <bits/stdc++.h>
  3. #define ll long long
  4. #define ld long double
  5. #define el "\n"
  6. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7. #define __ROOT__ int main()
  8. #pragma GCC optimize("O2")
  9. //#pragma GCC optimize("unroll-loops")
  10. //#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
  11. #define FOR(i,l,r) for(int i = l ; i <= r ; i ++)
  12. #define FORD(i,r,l) for(int i = r ; i >= l ; i --)
  13. #define REP(i, a ) for(int i = 0 ; i < a ; i ++ )
  14. #define fi first
  15. #define se second
  16. #define M 1000000007
  17. #define MAXN 1000001
  18. #define INF (1ll<<30)
  19. #define BLOCK_SIZE 425
  20. #define MAX_NODE 1001001
  21. #define LOG 19
  22. #define ALPHA_SIZE 26
  23. #define BASE 256
  24. #define NAME "file"
  25. #define compare(v) sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end());
  26. using namespace std;
  27. using namespace chrono ;
  28. const ll MOD[] = {(ll)1e9 + 2277, (ll)1e9 + 5277, (ll)1e9 + 8277, (ll)1e9 + 9277, (ll) 1e9 + 7 };
  29. const ll NMOD = 1;
  30. const int dx[] = {-1, 0, 1,0};
  31. const int dy[] = {0, 1, 0, -1};
  32. //**Variable**//
  33. ll n ;
  34. ll pref[MAXN] ;
  35. ll arr[MAXN];
  36. ll l[MAXN] ;
  37. ll r[MAXN] ;
  38. //**Struct**//
  39.  
  40. //**Function**//
  41. template<class X, class Y >
  42. bool minimize(X & x, const Y &y ) {
  43. return x > y ? x = y, 1:0 ;
  44. }
  45. template<class X, class Y >
  46. bool maximize(X &x, const Y &y ) {
  47. return x < y ? x = y, 1:0 ;
  48. }
  49.  
  50. void init() {
  51. cin>>n;
  52. FOR(i,1, n) cin >> arr[i] , pref[i] = pref[i-1] + arr[i] ;
  53. }
  54. stack<ll> st ;
  55. void solve() {
  56. FOR(i, 1, n ) {
  57. while(!st.empty() && arr[i] >= arr[st.top() ] ) st.pop() ;
  58. l[i] = st.empty() ? 1 : st.top() + 1 ;
  59. st.push(i) ;
  60. }
  61. while(!st.empty()) st.pop() ;
  62. FORD(i, n, 1 ) {
  63. while(!st.empty() && arr[i] > arr[st.top() ] )st.pop() ;
  64. r[i] = st.empty() ? n : st.top() - 1 ;
  65. st.push(i) ;
  66. }
  67. ll ans = 0 ;
  68. FOR(i, 1, n ) {
  69. ll len_L = i - l[i] + 1 ;
  70. ll len_R = r[i] - i + 1;
  71. if(len_L <= len_R) {
  72. FOR(k , l[i] , i ) {
  73. ll target = 2 * arr[i] + pref[k - 1] ;
  74. ll pos = upper_bound(pref + i , pref + r[i] + 1 , target ) - pref ;
  75. if(pos <= r[i] ) ans+= r[i] - pos + 1 ;
  76. }
  77. }else {
  78. FORD(k , r[i] , i ) {
  79. ll target = pref[k] - 2 * arr[i] ;
  80. ll pos = lower_bound(pref + l[i] - 1 , pref + i , target ) - pref ;
  81. ans += pos - l[i] + 1 ;
  82. }
  83. }
  84. }
  85. cout << ans << el ;
  86. }
  87.  
  88. __ROOT__ {
  89. // freopen(NAME".inp" , "r" , stdin);
  90. // freopen(NAME".out" , "w", stdout) ;
  91. fast;
  92. srand(time(nullptr)) ;
  93. int t = 1; // cin >> t ;
  94. while(t--) {
  95. init();
  96. solve();
  97. }
  98. return (0&0);
  99. }
  100.  
  101.  
  102.  
  103.  
Success #stdin #stdout 0.01s 5336KB
stdin
Standard input is empty
stdout
0