結果

問題 No.2002 Range Swap Query
ユーザー 👑 ygussanyygussany
提出日時 2022-06-12 17:31:50
言語 C
(gcc 12.3.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,727 bytes
コンパイル時間 1,348 ms
コンパイル使用メモリ 31,540 KB
実行使用メモリ 50,348 KB
最終ジャッジ日時 2023-08-10 08:09:27
合計ジャッジ時間 14,860 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 0 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
5,924 KB
testcase_08 AC 6 ms
4,384 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 1,100 ms
50,236 KB
testcase_13 AC 1,115 ms
50,224 KB
testcase_14 AC 1,094 ms
50,180 KB
testcase_15 AC 1,007 ms
48,440 KB
testcase_16 TLE -
testcase_17 AC 852 ms
16,692 KB
testcase_18 AC 217 ms
6,284 KB
testcase_19 AC 1,212 ms
7,720 KB
testcase_20 AC 78 ms
5,588 KB
testcase_21 AC 71 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>

#define SIZE 400
#define NUM 500

void swap(int* a, int* b)
{
	if (a == b) return;
	*a ^= *b;
	*b ^= *a;
	*a ^= *b;
}

typedef struct List {
	struct List *next;
	int u, w;
} list;

#define HASH 10007
const int H_Mod = HASH;

int main()
{
	int i, N, K, Q, A[200001], B[200001];
	scanf("%d %d %d", &N, &K, &Q);
	for (i = 1; i <= K; i++) scanf("%d %d", &(A[K-i]), &(B[K-i]));
	
	int h, hn = 0, j, k = K / SIZE, tmp[400001], u, w;
	static list *hash[NUM][HASH] = {}, hd[SIZE * NUM * 2], *hp;
	for (j = 1; j <= N; j++) tmp[j] = j;
	for (i = 0; i <= k; i++) {
		for (j = 0; j < SIZE && j + i * SIZE < K; j++) swap(&(tmp[A[j+i*SIZE]]), &(tmp[B[j+i*SIZE]]));
		for (j = 0; j < SIZE && j + i * SIZE < K; j++) {
			w = A[j+i*SIZE];
			if (tmp[w] != w) {
				u = tmp[w];
				h = u % H_Mod;
				hd[hn].u = u;
				hd[hn].w = w;
				hd[hn].next = hash[i][h];
				hash[i][h] = &(hd[hn++]);
				tmp[w] = w;
			}
			
			w = B[j+i*SIZE];
			if (tmp[w] != w) {
				u = tmp[w];
				h = u % H_Mod;
				hd[hn].u = u;
				hd[hn].w = w;
				hd[hn].next = hash[i][h];
				hash[i][h] = &(hd[hn++]);
				tmp[w] = w;
			}
		}
	}
	
	int q, L, R, X;
	for (q = 1; q <= Q; q++) {
		scanf("%d %d %d", &L, &R, &X);
		L ^= R;
		R ^= L;
		L ^= R;
		L = K - L;
		R = K - R;
		for (j = L; j <= R && j % SIZE != 0; j++) {
			if (X == A[j]) X = B[j];
			else if (X == B[j]) X = A[j];
		}
		if (j <= R) {
			for (i = j / SIZE; (i + 1) * SIZE <= R; i++) {
				h = X % H_Mod;
				for (hp = hash[i][h]; hp != NULL; hp = hp->next) if (hp->u == X) break;
				if (hp != NULL) X = hp->w;
			}
			for (j = i * SIZE; j <= R; j++) {
				if (X == A[j]) X = B[j];
				else if (X == B[j]) X = A[j];
			}
		}
		printf("%d\n", X);
	}
	fflush(stdout);
	return 0;
}
0