結果

問題 No.2002 Range Swap Query
ユーザー 👑 ygussanyygussany
提出日時 2022-06-12 17:32:24
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 1,573 ms / 2,000 ms
コード長 1,726 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 104,192 KB
実行使用メモリ 24,192 KB
最終ジャッジ日時 2024-04-28 03:21:44
合計ジャッジ時間 9,824 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 9 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 AC 794 ms
24,064 KB
testcase_13 AC 755 ms
24,064 KB
testcase_14 AC 747 ms
24,064 KB
testcase_15 AC 686 ms
23,168 KB
testcase_16 AC 1,573 ms
24,192 KB
testcase_17 AC 617 ms
18,048 KB
testcase_18 AC 210 ms
5,376 KB
testcase_19 AC 1,060 ms
7,424 KB
testcase_20 AC 71 ms
5,376 KB
testcase_21 AC 69 ms
5,376 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 3001
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