結果

問題 No.404 部分門松列
ユーザー square1001square1001
提出日時 2020-03-05 15:07:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,478 bytes
コンパイル時間 949 ms
コンパイル使用メモリ 83,008 KB
実行使用メモリ 17,628 KB
最終ジャッジ日時 2024-04-22 02:33:19
合計ジャッジ時間 11,769 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 WA -
testcase_03 AC 2 ms
6,940 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	vector<int> SA = A;
	sort(SA.begin(), SA.end());
	SA.erase(unique(SA.begin(), SA.end()), SA.end());
	for (int i = 0; i < N; ++i) {
		A[i] = lower_bound(SA.begin(), SA.end(), A[i]) - SA.begin();
	}
	int S = SA.size();
	vector<vector<int> > G(N);
	for (int i = 0; i < N; ++i) {
		G[A[i]].push_back(i);
	}
	vector<int> lb(N), rb(N);
	for (int i = 0; i < S; ++i) {
		for (int j = 0; j < G[i].size(); ++j) {
			lb[G[i][j]] = j;
			rb[G[i][j]] = G[i].size() - j - 1;
		}
	}
	vector<int> bit(N + 1);
	vector<long long> ans(S + 1);
	int cnt = 0;
	for (int i = 0; i < S; ++i) {
		for (int j : G[i]) {
			int lx = 0;
			for (int k = j; k >= 1; k -= k & (-k)) {
				lx += bit[k];
			}
			int ly = j - lb[j] - lx;
			int rx = cnt - lx;
			int ry = (N - j - 1) - rb[j] - rx;
			ans[i + 1] = 1LL * lx * rx + 1LL * ly * ry;
		}
		cnt += G[i].size();
		for (int j : G[i]) {
			for (int k = j + 1; k <= N; k += k & (-k)) {
				++bit[k];
			}
		}
	}
	for (int i = 0; i < N; ++i) {
		ans[i + 1] += ans[i];
	}
	int Q;
	cin >> Q;
	for (int i = 0; i < Q; ++i) {
		int L, H;
		cin >> L >> H;
		int pl = lower_bound(SA.begin(), SA.end(), L) - SA.begin();
		int pr = lower_bound(SA.begin(), SA.end(), H + 1) - SA.begin();
		cout << ans[pr] - ans[pl] << '\n';
	}
	return 0;
}
0