結果

問題 No.404 部分門松列
ユーザー antaanta
提出日時 2016-07-21 14:48:10
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 3,000 bytes
コンパイル時間 2,665 ms
コンパイル使用メモリ 176,500 KB
実行使用メモリ 7,932 KB
最終ジャッジ日時 2023-09-11 02:23:07
合計ジャッジ時間 7,809 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 84 ms
4,852 KB
testcase_14 AC 47 ms
4,376 KB
testcase_15 AC 65 ms
4,380 KB
testcase_16 AC 86 ms
6,124 KB
testcase_17 AC 128 ms
6,384 KB
testcase_18 AC 62 ms
4,376 KB
testcase_19 AC 43 ms
4,532 KB
testcase_20 AC 58 ms
4,636 KB
testcase_21 AC 114 ms
5,884 KB
testcase_22 AC 75 ms
4,376 KB
testcase_23 AC 68 ms
4,588 KB
testcase_24 AC 120 ms
4,460 KB
testcase_25 AC 194 ms
7,932 KB
testcase_26 AC 173 ms
6,672 KB
testcase_27 AC 30 ms
4,376 KB
testcase_28 AC 104 ms
4,616 KB
testcase_29 AC 134 ms
6,176 KB
testcase_30 AC 82 ms
4,380 KB
testcase_31 AC 17 ms
4,380 KB
testcase_32 AC 120 ms
6,636 KB
testcase_33 AC 125 ms
5,592 KB
testcase_34 AC 98 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

#ifdef NDEBUG
#error assert is disabled!
#endif

template<typename T>
T readNatural(T lo, T up) {
	assert(0 <= lo && lo <= up);
	T x = 0;
	while(1) {
		int d = getchar();
		if(!('0' <= d && d <= '9')) {
			ungetc(d, stdin);
			break;
		}
		d -= '0';
		assert(d <= up && x <= (up - d) / 10);
		x = x * 10 + d;
	}
	assert(lo <= x && x <= up);
	return x;
}

void readSpace() { int c = getchar(); assert(c == ' '); }
static bool read_eof = false;
void readEOL() {
	int c = getchar();
	if(c == EOF) {
		assert(!read_eof);
		read_eof = true;
	} else {
		assert(c == '\n');
	}
}
void readEOF() {
	assert(!read_eof);
	int c = getchar();
	assert(c == EOF);
	read_eof = true;
}


struct FenwickTree {
	typedef int T;
	vector<T> v;
	void init(int n) { v.assign(n, 0); }
	void add(int i, T x) {
		for(; i < (int)v.size(); i |= i + 1) v[i] += x;
	}
	T sum(int i) const {	//[0, i)
		T r = 0;
		for(-- i; i >= 0; i = (i & (i + 1)) - 1) r += v[i];
		return r;
	}
	T sum(int left, int right) const {	//[left, right)
		return sum(right) - sum(left);
	}
};

int main() {
	int N = readNatural(1, 200000);
	readEOL();
	vector<int> A(N);
	for(int i = 0; i < N; ++ i) {
		if(i != 0) readSpace();
		A[i] = readNatural(1, (int)1e9);
	}
	readEOL();

	vector<int> values = A;
	sort(values.begin(), values.end());
	values.erase(unique(values.begin(), values.end()), values.end());
	int X = (int)values.size();
	for(auto &x : A)
		x = (int)(lower_bound(values.begin(), values.end(), x) - values.begin());

	vector<long long> ansSum(X + 1);
	FenwickTree ftL, ftR;
	ftL.init(X);
	ftR.init(X);
	vector<int> numL(X), numR(X);
	ll sumProd = 0;
	for(int x : A) {
		ftR.add(x, 1);
		++ numR[x];
	}
	for(int x : A) {
		ftR.add(x, -1);
		-- numR[x];
		sumProd -= numL[x];

		ll cnt = 0;
		cnt += (ll)ftL.sum(x) * ftR.sum(x);
		cnt += (ll)ftL.sum(x + 1, X) * ftR.sum(x + 1, X);
		cnt -= sumProd - (ll)numL[x] * numR[x];
		ansSum[x + 1] += cnt;

		ftL.add(x, 1);
		++ numL[x];
		sumProd += numR[x];
	}
	rep(x, X)
		ansSum[x + 1] += ansSum[x];

	int Q = readNatural(1, 200000);
	readEOL();
	rep(ii, Q) {
		int L = readNatural(1, (int)1e9);
		readSpace();
		int R = readNatural(L, (int)1e9);
		readEOL();

		int l = (int)(lower_bound(values.begin(), values.end(), L) - values.begin());
		int r = (int)(upper_bound(values.begin(), values.end(), R) - values.begin());
		long long ans = ansSum[r] - ansSum[l];
		printf("%lld\n", ans);
	}
	readEOF();
	return 0;
}
0