Submission #58114067


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
struct node_s {
int exists;
struct node_s* next[2];
};
struct map_s {
int elem_count;
struct node_s* root;
};
/* 10 */
int map_add_i(struct node_s** node, int key) {
if (*node == NULL) {
*node = malloc(sizeof(**node));
if (*node == NULL) exit(2);
**node = (struct node_s){0, {NULL, NULL}};
}
if (key == 0) {
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>

struct node_s {
	int exists;
	struct node_s* next[2];
};

struct map_s {
	int elem_count;
	struct node_s* root;
};

/* 追加した場合は1、既にある場合は0を返す */
int map_add_i(struct node_s** node, int key) {
	if (*node == NULL) {
		*node = malloc(sizeof(**node));
		if (*node == NULL) exit(2);
		**node = (struct node_s){0, {NULL, NULL}};
	}
	if (key == 0) {
		int ret = !(*node)->exists;
		(*node)->exists = 1;
		return ret;
	} else {
		return map_add_i(&(*node)->next[key & 1], key >> 1);
	}
}

void map_add(struct map_s* map, int key) {
	if (map_add_i(&map->root, key)) map->elem_count++;
}

void map_iter_i(struct node_s* node, int key, int depth, void* data, void (*func)(void* data, int key)) {
	if (node != NULL) {
		if (node->exists) func(data, key);
		map_iter_i(node->next[0], key, depth + 1, data, func);
		map_iter_i(node->next[1], key | (1 << depth), depth + 1, data, func);
	}
}

void map_iter(struct map_s* map, void* data, void (*func)(void* data, int key)) {
	map_iter_i(map->root, 0, 0, data, func);
}

void map_clear_i(struct node_s* node) {
	if (node != NULL) {
		map_clear_i(node->next[0]);
		map_clear_i(node->next[1]);
		free(node);
	}
}

void map_clear(struct map_s* map) {
	map_clear_i(map->root);
	map->elem_count = 0;
	map->root = NULL;
}

int N, Q;
int C[212345];
int a[212345], b[212345];

struct map_s hako[212345];

void add_func(void* data, int key) {
	map_add(data, key);
}

int main(void) {
	int i;
	if (scanf("%d%d", &N, &Q) != 2) return 1;
	for (i = 1; i <= N; i++) {
		if (scanf("%d", &C[i]) != 1) return 1;
	}
	for (i = 0; i < Q; i++) {
		if (scanf("%d%d", &a[i], &b[i]) != 2) return 1;
	}

	for (i = 1; i <= N; i++) {
		map_add(&hako[i], C[i]);
	}

	for (i = 0; i < Q; i++) {
		if (hako[a[i]].elem_count <= hako[b[i]].elem_count) {
			map_iter(&hako[a[i]], &hako[b[i]], add_func);
			map_clear(&hako[a[i]]);
		} else {
			struct map_s temp;
			map_iter(&hako[b[i]], &hako[a[i]], add_func);
			map_clear(&hako[b[i]]);
			temp = hako[a[i]];
			hako[a[i]] = hako[b[i]];
			hako[b[i]] = temp;
		}
		printf("%d\n", hako[b[i]].elem_count);
	}
	return 0;
}

/*

各箱にはいっているボールの色の集合を管理する
要素数の少ない方を走査して大きい方に突っ込めば多分なんとかなる (予想)

*/

Submission Info

Submission Time
Task F - Colored Ball
User mikecat
Language C (gcc 12.2.0)
Score 500
Code Size 2432 Byte
Status AC
Exec Time 1489 ms
Memory 120340 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 44
Set Name Test Cases
Sample sample00.txt, sample01.txt
All sample00.txt, sample01.txt, testcase00.txt, testcase01.txt, testcase02.txt, testcase03.txt, testcase04.txt, testcase05.txt, testcase06.txt, testcase07.txt, testcase08.txt, testcase09.txt, testcase10.txt, testcase11.txt, testcase12.txt, testcase13.txt, testcase14.txt, testcase15.txt, testcase16.txt, testcase17.txt, testcase18.txt, testcase19.txt, testcase20.txt, testcase21.txt, testcase22.txt, testcase23.txt, testcase24.txt, testcase25.txt, testcase26.txt, testcase27.txt, testcase28.txt, testcase29.txt, testcase30.txt, testcase31.txt, testcase32.txt, testcase33.txt, testcase34.txt, testcase35.txt, testcase36.txt, testcase37.txt, testcase38.txt, testcase39.txt, testcase40.txt, testcase41.txt
Case Name Status Exec Time Memory
sample00.txt AC 1 ms 1620 KB
sample01.txt AC 0 ms 1584 KB
testcase00.txt AC 135 ms 117764 KB
testcase01.txt AC 134 ms 117768 KB
testcase02.txt AC 357 ms 117836 KB
testcase03.txt AC 360 ms 117800 KB
testcase04.txt AC 347 ms 117744 KB
testcase05.txt AC 342 ms 117760 KB
testcase06.txt AC 365 ms 117752 KB
testcase07.txt AC 361 ms 117820 KB
testcase08.txt AC 347 ms 117768 KB
testcase09.txt AC 345 ms 117772 KB
testcase10.txt AC 355 ms 117656 KB
testcase11.txt AC 347 ms 117768 KB
testcase12.txt AC 205 ms 95528 KB
testcase13.txt AC 220 ms 103940 KB
testcase14.txt AC 216 ms 109180 KB
testcase15.txt AC 239 ms 120340 KB
testcase16.txt AC 226 ms 113996 KB
testcase17.txt AC 237 ms 118452 KB
testcase18.txt AC 220 ms 109780 KB
testcase19.txt AC 236 ms 117228 KB
testcase20.txt AC 239 ms 117164 KB
testcase21.txt AC 245 ms 117860 KB
testcase22.txt AC 251 ms 117152 KB
testcase23.txt AC 248 ms 117968 KB
testcase24.txt AC 251 ms 116248 KB
testcase25.txt AC 257 ms 118040 KB
testcase26.txt AC 237 ms 111484 KB
testcase27.txt AC 256 ms 117644 KB
testcase28.txt AC 229 ms 106940 KB
testcase29.txt AC 257 ms 117680 KB
testcase30.txt AC 236 ms 107116 KB
testcase31.txt AC 257 ms 117744 KB
testcase32.txt AC 253 ms 115420 KB
testcase33.txt AC 260 ms 117652 KB
testcase34.txt AC 241 ms 109020 KB
testcase35.txt AC 257 ms 117772 KB
testcase36.txt AC 248 ms 114416 KB
testcase37.txt AC 259 ms 117732 KB
testcase38.txt AC 232 ms 106192 KB
testcase39.txt AC 258 ms 117764 KB
testcase40.txt AC 1476 ms 117772 KB
testcase41.txt AC 1489 ms 117596 KB


2024-12-04 (Wed)
07:23:45 +09:00