結果

問題 No.649 ここでちょっとQK!
ユーザー asugen0402asugen0402
提出日時 2019-04-24 11:49:59
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 147 ms / 3,000 ms
コード長 5,939 bytes
コンパイル時間 431 ms
コンパイル使用メモリ 33,792 KB
実行使用メモリ 9,728 KB
最終ジャッジ日時 2024-04-25 05:47:22
合計ジャッジ時間 5,482 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 122 ms
6,400 KB
testcase_04 AC 118 ms
9,728 KB
testcase_05 AC 141 ms
8,832 KB
testcase_06 AC 126 ms
8,832 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 60 ms
6,272 KB
testcase_13 AC 60 ms
6,272 KB
testcase_14 AC 59 ms
6,272 KB
testcase_15 AC 61 ms
6,400 KB
testcase_16 AC 64 ms
6,272 KB
testcase_17 AC 72 ms
6,528 KB
testcase_18 AC 82 ms
7,040 KB
testcase_19 AC 85 ms
7,296 KB
testcase_20 AC 93 ms
7,424 KB
testcase_21 AC 104 ms
7,936 KB
testcase_22 AC 111 ms
8,192 KB
testcase_23 AC 119 ms
8,320 KB
testcase_24 AC 131 ms
8,704 KB
testcase_25 AC 141 ms
8,960 KB
testcase_26 AC 147 ms
9,216 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 54 ms
6,272 KB
testcase_31 AC 59 ms
6,272 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_QUERY_MAX		200000									// 最大クエリ数
#define D_QUERY_ADD		1										// クエリ - 追加
#define D_SQR_SIZE		450										// 平方分割サイズ

// 内部構造体 - クエリ情報
typedef struct Query {
	int miType;													// タイプ
	long long mlVal;											// 値
} Query;

// 内部構造体 - 平方分割情報
typedef struct Sqr {
	long long mlRngS, mlRngE;									// 値範囲
	long long ml1Val[D_SQR_SIZE];								// 値
	int miVCnt;													// 値数
} Sqr;

// 内部変数
static FILE *szpFpI;											// 入力
static Query sz1Query[D_QUERY_MAX];								// クエリ
static int siQCnt;												// クエリ数
static int siDNo;												// 表示配列番号 0~
static long long sl1Sqr[D_SQR_SIZE * D_SQR_SIZE];				// 平方分割値
static int siSqrCnt;											// 平方分割値数
static Sqr sz1Sqr[D_SQR_SIZE];									// 平方分割情報

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// 出力
int
fOut(
	char *pcpLine				// <I> 1行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// ソート関数 - long long昇順
int
fSortFnc(
	const void *pzpVal1			// <I> 値1
	, const void *pzpVal2		// <I> 値2
)
{
	long long *llpVal1 = (long long *)pzpVal1;
	long long *llpVal2 = (long long *)pzpVal2;

	// long long昇順
	if (*llpVal1 > *llpVal2) {
		return 1;
	}
	else if (*llpVal1 < *llpVal2) {
		return -1;
	}

	return 0;
}

// 平方分割 - 初期処理 - 値追加
int
fSqrIniAdd(
	long long plVal				// <I> 値
)
{
	sl1Sqr[siSqrCnt] = plVal;
	siSqrCnt++;

	return 0;
}

// 平方分割 - 初期処理 - 作成
int
fSqrIniMake(
)
{
	int i;

	// 平方分割値 - ソート
	qsort(sl1Sqr, siSqrCnt, sizeof(long long), fSortFnc);

	// 値範囲 - セット
	for (i = 0; ; i++) {
		sz1Sqr[i].mlRngS = sl1Sqr[D_SQR_SIZE * i];

		int liNo = D_SQR_SIZE * (i + 1) - 1;
		if (liNo >= siSqrCnt) {
			liNo = siSqrCnt - 1;
		}
		sz1Sqr[i].mlRngE = sl1Sqr[liNo];
		if (liNo >= siSqrCnt - 1) {
			break;
		}
	}

	return 0;
}

// 平方分割 - 対象位置検索 - 値
int
fSqrSrhPosNo(
	long long plVal				// <I> 値
)
{
	int i;

	for (i = 0; i < D_SQR_SIZE; i++) {
		if (sz1Sqr[i].mlRngS <= plVal && plVal <= sz1Sqr[i].mlRngE) {
			return i;
		}
	}

	return -1;
}

// 平方分割 - 値追加
int
fSqrAdd(
	long long plVal				// <I> 値
)
{
	int i;

	// 対象データ
	int liNo = fSqrSrhPosNo(plVal);
	while (1) {
		if (sz1Sqr[liNo].miVCnt < D_SQR_SIZE) {
			break;
		}
		else {
			liNo++;
		}
	}
	Sqr *lzpSqr = &sz1Sqr[liNo];

	// 追加位置
	for (i = 0; i < lzpSqr->miVCnt; i++) {
		if (lzpSqr->ml1Val[i] > plVal) {
			break;
		}
	}
	liNo = i;

	// 値を後ろへずらす
	for (i = lzpSqr->miVCnt - 1; i >= liNo; i--) {
		lzpSqr->ml1Val[i + 1] = lzpSqr->ml1Val[i];
	}

	// 値追加
	lzpSqr->ml1Val[liNo] = plVal;
	lzpSqr->miVCnt++;

	return 0;
}

// 平方分割 - 値取得
long long
fSqrGet(
	int piNo					// <I> 配列番号 0~
)
{
	int i;

	for (i = 0; i < D_SQR_SIZE; i++) {
		if (sz1Sqr[i].miVCnt > piNo) {
			return sz1Sqr[i].ml1Val[piNo];
		}
		else {
			piNo -= sz1Sqr[i].miVCnt;
		}
	}

	return -1;
}

// 平方分割 - 値削除
int
fSqrDel(
	long long plVal				// <I> 値
)
{
	int i;

	// 対象データ
	int liNo = fSqrSrhPosNo(plVal);
	Sqr *lzpSqr = &sz1Sqr[liNo];

	// 削除位置
	liNo = -1;
	for (i = 0; i < lzpSqr->miVCnt; i++) {
		if (lzpSqr->ml1Val[i] == plVal) {
			liNo = i;
			break;
		}
	}
	if (liNo < 0) {
		return -1;
	}

	// 値を前へずらす
	for (i = liNo; i < lzpSqr->miVCnt - 1; i++) {
		lzpSqr->ml1Val[i] = lzpSqr->ml1Val[i + 1];
	}

	// 値削除
	lzpSqr->miVCnt--;

	return 0;
}

// 実行メイン
int
fMain(
)
{
	int i;
	char lc1Buf[1024];

	// クエリ数・表示配列番号 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d%d", &siQCnt, &siDNo);
	siDNo--;

	// クエリ - 取得
	for (i = 0; i < siQCnt; i++) {
		fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
		sscanf(lc1Buf, "%d%lld", &sz1Query[i].miType, &sz1Query[i].mlVal);

		// 平方分割 - 初期処理 - 値追加
		if (sz1Query[i].miType == D_QUERY_ADD) {
			fSqrIniAdd(sz1Query[i].mlVal);
		}
	}

	// 平方分割 - 初期処理 - 作成
	fSqrIniMake();

	// クエリ - 実行
	for (i = 0; i < siQCnt; i++) {

		// 平方分割 - 値追加
		if (sz1Query[i].miType == D_QUERY_ADD) {
			fSqrAdd(sz1Query[i].mlVal);
			continue;
		}

		// 平方分割 - 値取得
		long long llVal = fSqrGet(siDNo);

		// 出力
		sprintf(lc1Buf, "%lld\n", llVal);
		fOut(lc1Buf);

		// 平方分割 - 値削除
		if (llVal >= 0) {
			fSqrDel(llVal);
		}
	}

	return 0;
}

// 1回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// データ - 初期化
	siSqrCnt = 0;												// 平方分割値数
	memset(sz1Sqr, 0, sizeof(sz1Sqr));							// 平方分割情報

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}

0