結果

問題 No.404 部分門松列
ユーザー bal4ubal4u
提出日時 2019-08-23 17:37:49
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 189 ms / 2,000 ms
コード長 2,971 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 34,372 KB
実行使用メモリ 8,984 KB
最終ジャッジ日時 2024-04-21 15:39:30
合計ジャッジ時間 6,017 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 86 ms
5,376 KB
testcase_14 AC 38 ms
5,376 KB
testcase_15 AC 83 ms
5,376 KB
testcase_16 AC 73 ms
6,568 KB
testcase_17 AC 124 ms
6,488 KB
testcase_18 AC 57 ms
5,376 KB
testcase_19 AC 38 ms
5,376 KB
testcase_20 AC 60 ms
5,376 KB
testcase_21 AC 112 ms
6,260 KB
testcase_22 AC 70 ms
5,376 KB
testcase_23 AC 78 ms
5,528 KB
testcase_24 AC 143 ms
5,528 KB
testcase_25 AC 189 ms
8,984 KB
testcase_26 AC 181 ms
7,556 KB
testcase_27 AC 21 ms
5,376 KB
testcase_28 AC 99 ms
5,376 KB
testcase_29 AC 129 ms
7,004 KB
testcase_30 AC 97 ms
5,376 KB
testcase_31 AC 10 ms
5,376 KB
testcase_32 AC 113 ms
6,828 KB
testcase_33 AC 124 ms
5,376 KB
testcase_34 AC 118 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:10:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
   10 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:18:24: note: in expansion of macro 'gc'
   18 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'out':
main.c:11:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
   11 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:26:17: note: in expansion of macro 'pc'
   26 |         if (!n) pc('0');
      |                 ^~

ソースコード

diff #

// yukicoder: No.404 部分門松列
// 2019.8.23 bal4u

#include <stdio.h>
#include <stdlib.h>

typedef long long ll;

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() { // 整数の入力(負数対応)
	int n = 0, c = gc();
	do n = 10*n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(ll n) { // 非負整数の表示(出力)
	int i; char b[30];

	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

#define MAX 200005

//// bit木 1-indexedに注意
int bitf[MAX], bits[MAX], bitn;
void add(int *bit, int i) { while (i <= bitn) bit[i]++, i += i & -i; }
void sub(int *bit, int i) { while (i <= bitn) bit[i]--, i += i & -i; }
int sum(int *bit, int i) { int s = 0; while (i > 0) s += bit[i], i -= i & -i; return s; }

//// 本問題関連
#define MAX 200005
typedef struct { int a, id; } T;
T t[MAX]; int N;
int A[MAX];
int vf[MAX], vs[MAX];
ll  cnt[MAX], cum[MAX];

// a[i]>=key という条件を満たす最小のi
int lower_bound(int x) {
	int m, l = 1, r = N+1;
	while (l+1 < r) {
		m = (l+r) >> 1;
		if (t[m].a < x) l = m; else r = m;
	}
	return t[l].a < x? r: l;
}

// a[i]>key という条件を満たす最小のi
int upper_bound(int x) {
	int m, l = 1, r = N+1;
	while (l+1 < r) {
		m = (l+r) >> 1;
		if (t[m].a <= x) l = m; else r = m;
	}
	return t[l].a <= x? r: l;
}

int cmp(const void *u, const void *v) { return ((T *)u)->a - ((T *)v)->a; }

void compact(int *a, int n) { // データ圧縮。圧縮後のデータ順序は圧縮前と同様
	int i, v;
	for (i = 1; i <= n; i++) t[i].a = a[i], t[i].id = i;
	qsort(t+1, n, sizeof(T), cmp);
	v = 1; a[t[1].id] = v;
	for (i = 2; i <= n; i++) {
		if (t[i].a != t[i-1].a) v++;
		a[t[i].id] = v;
	}
}

int main()
{
	int i, x, Q; ll same;
	
	N = in(); for (i = 1; i <= N; i++) A[i] = in();
	compact(A, N);  // 1-indexed

	bitn = N+1;  // bit木の初期化
	
	// すべてのデータをとりあえずすべて後半のbit木、頻度表に登録
	for (i = 1; i <= N; i++) {
		add(bits, A[i]);  // 後半のデータをbit木に追加
		vs[A[i]]++;       // 後半のデータを頻度表にも追加
	}
	
	same = 0; // 前後半における同じ値のデータの累積和
	x = A[1], add(bitf, x), sub(bits, x);
	vf[x]++, vs[x]--, same += vs[x];
	for (i = 2; i < N; i++) {
		x = A[i];
		sub(bits, x), vs[x]--, same -= vf[x];

		int sf = sum(bitf, x-1), ss = sum(bits, x-1);

		cnt[x] += (ll)sf*ss + (ll)(i-1-sf-vf[x])*(N-i-ss-vs[x]) - (same-vf[x]*vs[x]);

		add(bitf, x), same += vs[x], vf[x]++;
	}
	cum[0] = 0; for (i = 1; i <= N; i++) cum[i] = cum[i-1] + cnt[i];

	Q = in(); while (Q--) {
		int L = in(), H = in();
		if ((L = lower_bound(L)) > N || L < 1 ||
			(H = upper_bound(H)) <= 1 || H == L) pc('0');
		else {
			ll r = cum[A[t[H-1].id]] - cum[A[t[L].id]-1];
			if (r <= 0) pc('0');
			else out(r);
		}
		pc('\n');
	}
	return 0;
}
0