#include #include #include #include #include #include #include #include #include using namespace std; class BinaryIndexedTree_1_indexed{ void init(const vector &A){ for(int i=0; i T; int N; BinaryIndexedTree_1_indexed(const int n) : T(n+1,0), N(n){ } BinaryIndexedTree_1_indexed(const vector &A) : T(A.size()+1,0), N(A.size()){ init(A); } //caution : position "i" must be 1-indexed void add(int i, const int x){ while(i <= N){ T[i] += x; i += i & -i; } } //get sums [0,i] int get_sum(int i){ int ret=0; while(i>0){ ret += T[i]; i -= i & -i; } return ret; } }; int main(){ int N; cin >> N; vector L(N); for(int i=0; i> L[i]; vector > v(N); for(int i=0; i pool; int last = -1; for(int i=0; i pool; int last = -1; for(int i=N-1; i>=0; i--){ if(last == v[i].first){ pool.push(v[i].second); }else{ last = v[i].first; while(!pool.empty()){ BIT.add(pool.front(), 1); pool.pop(); } pool.push(v[i].second); } long long left = BIT.get_sum(v[i].second); long long right = BIT.get_sum(N) - left; ans += left*right; } } v.push_back( {1000000000+10, 1000000+10}); vector same; int last = -1; for(int i=0; i<=N; i++){ if(last == v[i].first){ same.push_back(v[i].second); }else{ int cnt = same.size(); for(int j=1; j(); same.push_back(v[i].second); last = v[i].first; } } cout << ans << endl; return 0; }