結果

問題 No.404 部分門松列
ユーザー pekempeypekempey
提出日時 2016-07-22 23:32:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,064 ms / 2,000 ms
コード長 2,924 bytes
コンパイル時間 2,736 ms
コンパイル使用メモリ 157,492 KB
実行使用メモリ 65,868 KB
最終ジャッジ日時 2023-09-11 02:27:55
合計ジャッジ時間 17,936 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
15,852 KB
testcase_01 AC 9 ms
15,712 KB
testcase_02 AC 9 ms
15,916 KB
testcase_03 AC 8 ms
15,776 KB
testcase_04 AC 9 ms
16,028 KB
testcase_05 AC 9 ms
15,756 KB
testcase_06 AC 9 ms
15,820 KB
testcase_07 AC 9 ms
15,760 KB
testcase_08 AC 9 ms
15,752 KB
testcase_09 AC 9 ms
15,888 KB
testcase_10 AC 9 ms
15,888 KB
testcase_11 AC 9 ms
15,900 KB
testcase_12 AC 9 ms
15,772 KB
testcase_13 AC 411 ms
38,516 KB
testcase_14 AC 352 ms
49,224 KB
testcase_15 AC 421 ms
37,684 KB
testcase_16 AC 684 ms
43,004 KB
testcase_17 AC 665 ms
49,928 KB
testcase_18 AC 193 ms
30,704 KB
testcase_19 AC 246 ms
28,632 KB
testcase_20 AC 471 ms
56,148 KB
testcase_21 AC 712 ms
48,300 KB
testcase_22 AC 235 ms
34,144 KB
testcase_23 AC 541 ms
59,924 KB
testcase_24 AC 1,018 ms
59,888 KB
testcase_25 AC 1,064 ms
65,868 KB
testcase_26 AC 1,042 ms
62,932 KB
testcase_27 AC 151 ms
29,948 KB
testcase_28 AC 397 ms
41,820 KB
testcase_29 AC 859 ms
53,804 KB
testcase_30 AC 494 ms
42,700 KB
testcase_31 AC 77 ms
23,020 KB
testcase_32 AC 681 ms
48,744 KB
testcase_33 AC 544 ms
47,240 KB
testcase_34 AC 651 ms
49,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T> ostream &operator <<(ostream &os, const vector<T> &v) { for (T x : v) os << x << " "; return os; }

struct BIT {
	vector<long long> bit;

	BIT(int n) : bit(n) {}

	void update(int k, long long v) {
		for (k++; k < bit.size(); k += k & -k) {
			bit[k] += v;
		}
	}

	long long query(int k) {
		long long res = 0;
		for (k++; k > 0; k -= k & -k) {
			res += bit[k];
		}
		return res;
	}
};

const int N = 1 << 18;
vector<int> seg[N * 2];

void build() {
	for (int i = N - 2; i >= 0; i--) {
		seg[i].resize(seg[i * 2 + 1].size() + seg[i * 2 + 2].size());
		merge(seg[i * 2 + 1].begin(), seg[i * 2 + 1].end(), seg[i * 2 + 2].begin(), seg[i * 2 + 2].end(), seg[i].begin());
	}
}

// [:v]
long long query(int a, int b, int v, int k = 0, int l = 0, int r = N) {
	if (r <= a || b <= l) return 0;
	if (a <= l && r <= b) return upper_bound(seg[k].begin(), seg[k].end(), v) - seg[k].begin();
	long long L = query(a, b, v, k * 2 + 1, l, (l + r) / 2);
	long long R = query(a, b, v, k * 2 + 2, (l + r) / 2, r);
	return L + R;
}

// [v:]
long long query2(int a, int b, int v, int k = 0, int l = 0, int r = N) {
	if (r <= a || b <= l) return 0;
	if (a <= l && r <= b) return seg[k].end() - lower_bound(seg[k].begin(), seg[k].end(), v);
	long long L = query2(a, b, v, k * 2 + 1, l, (l + r) / 2);
	long long R = query2(a, b, v, k * 2 + 2, (l + r) / 2, r);
	return L + R;
}

int main() {
	int n;
	cin >> n;

	vector<int> a(n);
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);

	vector<int> A(a);
	int Q;
	cin >> Q;
	vector<int> ll(Q), rr(Q);
	for (int i = 0; i < Q; i++) {
		scanf("%d %d", &ll[i], &rr[i]);
		A.push_back(ll[i]);
		A.push_back(rr[i]);
	}

	sort(A.begin(), A.end());
	A.erase(unique(A.begin(), A.end()), A.end());
	for (int i = 0; i < n; i++) a[i] = lower_bound(A.begin(), A.end(), a[i]) - A.begin();
	for (int i = 0; i < Q; i++) {
		ll[i] = lower_bound(A.begin(), A.end(), ll[i]) - A.begin();
		rr[i] = lower_bound(A.begin(), A.end(), rr[i]) - A.begin();
	}
	for (int i = 0; i < n; i++) {
		seg[i + N - 1].push_back(a[i]);
	}
	build();

	vector<long long> L(A.size() + 2), R(A.size() + 2);
	for (int i = 0; i < n; i++) R[a[i]]++;
	BIT bit(A.size() + 10);

	vector<long long> cnt(A.size() + 2);

	for (int i = 0; i < n; i++) {
		bit.update(a[i], -R[a[i]] * L[a[i]]);
		R[a[i]]--;
		bit.update(a[i], R[a[i]] * L[a[i]]);

		long long s = bit.query(a[i] - 1);
		long long S = bit.query(bit.bit.size() - 2) - bit.query(a[i]);
		cnt[a[i]] += -s + query(0, i, a[i] - 1) * query(i + 1, N, a[i] - 1);
		cnt[a[i]] += -S + query2(0, i, a[i] + 1) * query2(i + 1, N, a[i] + 1);

		bit.update(a[i], -R[a[i]] * L[a[i]]);
		L[a[i]]++;
		bit.update(a[i], R[a[i]] * L[a[i]]);
	}

	vector<long long> sum(A.size() + 2);
	for (int i = 0; i < A.size() + 1; i++) {
		sum[i + 1] = sum[i] + cnt[i];
	}

	for (int i = 0; i < Q; i++) {
		printf("%lld\n", sum[rr[i] + 1] - sum[ll[i]]);
	}
}
0