提交时间:2022-07-20 12:05:32

运行 ID: 52873

#include<iostream> #include<cstdio> using namespace std; inline int read(){ int x = 0, f = 1; char ch = getchar(); while(ch < '0' && ch > '9'){ if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - 48; ch = getchar(); } return x * f; } const int N = 1e6 + 10; long long n, maxn[N], ans; int main(){ n = read(), maxn[1] = read(); for(long long i = 2, t; i <= n; i++){ t = read(); long long l = 1, r = i; maxn[i] = max(t, maxn[i - 1]); while(r - l > 1){ int mid = (l + r) / 2; if(maxn[mid] > t) r = mid; else l = mid; } // cout << l << " " << r << endl; if(maxn[l] > t) ans += i - l; else ans += i - r; } printf("%lld", ans); return 0; }