#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; // セグメント木 class SegmentTree { private: typedef tuple T1; typedef int T2; // データの初期値、以下の条件を満たすこと // uniteData(v, INIT_DATA) == v static const T1 INIT_DATA; // 前回の値がprevである要素に対して、 // パラメータxを用いた更新処理を適用した後の計算結果を返す T1 updateData(T1 prev, T2 x){ get<0>(prev) -= x; get<1>(prev) += x; get<2>(prev) = get<0>(prev) * (long long) get<1>(prev); return prev; } // 2つの区間の計算結果v1,v2に対して、 // その2つの区間を統合した区間における計算結果を返す T1 uniteData(T1 v1, T1 v2){ get<0>(v1) += get<0>(v2); get<1>(v1) += get<1>(v2); get<2>(v1) += get<2>(v2); return v1; } int n; vector data; void updateTree(int a, int k, int l, int r, T2 x){ if(a == l && a == r){ data[k] = updateData(data[k], x); } else if(l <= a && a <= r){ updateTree(a, k*2+1, l, (l+r)/2, x); updateTree(a, k*2+2, (l+r+1)/2, r, x); data[k] = uniteData(data[k*2+1], data[k*2+2]); } } T1 getValue(int a, int b, int k, int l, int r){ if(a <= l && r <= b){ return data[k]; } else if(a <= r && l <= b){ T1 v1 = getValue(a, b, k*2+1, l, (l+r)/2); T1 v2 = getValue(a, b, k*2+2, (l+r+1)/2, r); return uniteData(v1, v2); } else{ return INIT_DATA; } } public: SegmentTree(int n0){ n = 1; while(n < n0) n *= 2; data.assign(2*n-1, INIT_DATA); } SegmentTree(const vector& v) : SegmentTree((int)v.size()){ for(unsigned i=0; i=0; --k) data[k] = uniteData(data[k*2+1], data[k*2+2]); } // a番目の要素にパラメータxによる更新処理を適用 void update(int a, T2 x){ updateTree(a, 0, 0, n-1, x); } // 区間[a,b]の計算結果を返す T1 getValue(int a, int b){ return getValue(a, b, 0, 0, n-1); } }; const tuple SegmentTree::INIT_DATA = make_tuple(0, 0, 0); int normalize(vector& a, vector& b, vector& c) { int na = a.size(); int nb = b.size(); int nc = c.size(); map conv; conv[INT_MIN]; conv[INT_MAX]; for(int i=0; isecond; } for(int i=0; isecond; } return cnt; } int main() { int n; cin >> n; vector a(n); for(int i=0; i> a[i]; int q; cin >> q; vector low(q), high(q); for(int i=0; i> low[i] >> high[i]; int m = normalize(a, low, high); vector > v(m); for(int i=0; i(v[a[i]]); SegmentTree st(v); vector cnt(m+1, 0); for(int i=0; i(t1) * (long long) get<1>(t1) - get<2>(t1); cnt[a[i]+1] += get<0>(t2) * (long long) get<1>(t2) - get<2>(t2); st.update(a[i], 1); } for(int i=0; i