Submission #69443186


Source Code Expand

Copy
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
void* malloc_check(size_t size) {
void* res = malloc(size);
if (res == NULL) exit(2);
return res;
}
int Q;
int query[512345][3];
/* 1. */
struct node_s {
int value;
struct node_s* next;
};
struct node_s* num_to_node[512345];
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

void* malloc_check(size_t size) {
	void* res = malloc(size);
	if (res == NULL) exit(2);
	return res;
}

int Q;
int query[512345][3];

/* 1. 一旦削除クエリを無視し、挿入クエリのみを処理して順番の情報を得る */

struct node_s {
	int value;
	struct node_s* next;
};

struct node_s* num_to_node[512345];
int num_to_pos[512345];

/* 2. 区間更新・区間和のセグメント木でクエリを処理する */

#define KI_MAX (1 << 19) /* 524288 */

int64_t ki[KI_MAX * 2 - 1], ki_all[KI_MAX * 2 - 1];
char ki_all_valid[KI_MAX * 2 - 1];

int64_t ki_set_i(int idx, int ss, int se, int qs, int qe, int64_t value) {
	if (qs <= ss && se <= qe) { /* セグメントがクエリに完全に含まれる */
		ki_all[idx] = value;
		ki_all_valid[idx] = 1;
	} else if (se <= qs || qe <= ss) { /* 完全に外れている */
		/* 何もしない */
	} else {
		int sm = ss + (se - ss) / 2;
		int64_t l, r;
		if (ki_all_valid[idx]) {
			ki_all[idx * 2 + 1] = ki_all[idx];
			ki_all[idx * 2 + 2] = ki_all[idx];
			ki_all_valid[idx * 2 + 1] = 1;
			ki_all_valid[idx * 2 + 2] = 1;
			ki_all_valid[idx] = 0;
		}
		l = ki_set_i(idx * 2 + 1, ss, sm, qs, qe, value);
		r = ki_set_i(idx * 2 + 2, sm, se, qs, qe, value);
		ki[idx] = l + r;
	}
	return ki_all_valid[idx] ? ki_all[idx] * (se - ss) : ki[idx];
}

void ki_set(int qs, int qe, int64_t value) {
	ki_set_i(0, 0, KI_MAX, qs, qe, value);
}

int64_t ki_get_i(int idx, int ss, int se, int qs, int qe) {
	if (qs <= ss && se <= qe) { /* セグメントがクエリに完全に含まれる */
		return ki_all_valid[idx] ? ki_all[idx] * (se - ss) : ki[idx];
	} else if (se <= qs || qe <= ss) { /* 完全に外れている */
		return 0;
	} else if (ki_all_valid[idx]) { /* 全体セット済、一部のみ反映 */
		int s = ss < qs ? qs : ss;
		int e = se < qe ? se : qe;
		return ki_all[idx] * (e - s);
	} else {
		int sm = ss + (se - ss) / 2;
		int64_t l, r;
		l = ki_get_i(idx * 2 + 1, ss, sm, qs, qe);
		r = ki_get_i(idx * 2 + 2, sm, se, qs, qe);
		return l + r;
	}
}

int64_t ki_get(int qs, int qe) {
	return ki_get_i(0, 0, KI_MAX, qs, qe);
}

int main(void) {
	int i;
	struct node_s* np;
	if (scanf("%d", &Q) != 1) return 1;
	for (i = 1; i <= Q; i++) {
		if (scanf("%d%d", &query[i][0], &query[i][1]) != 2) return 1;
		if (query[i][0] == 2 && scanf("%d", &query[i][2]) != 1) return 1;
	}

	/* 1 */
	num_to_node[0] = malloc_check(sizeof(struct node_s));
	num_to_node[0]->value = 0;
	num_to_node[0]->next = NULL;
	for (i = 1; i <= Q; i++) {
		if (query[i][0] == 1) {
			struct node_s* new_node = malloc_check(sizeof(struct node_s));
			new_node->value = i;
			new_node->next = num_to_node[query[i][1]]->next;
			num_to_node[query[i][1]]->next = new_node;
			num_to_node[i] = new_node;
		}
	}
	for (i = 0, np = num_to_node[0]; np != NULL; i++, np = np->next) {
		num_to_pos[np->value] = i;
	}

	/* 2 */
	for (i = 1; i <= Q; i++) {
		if (query[i][0] == 1) {
			int pos = num_to_pos[i];
			ki_set(pos, pos + 1, i);
		} else if (query[i][0] == 2) {
			int pos1 = num_to_pos[query[i][1]];
			int pos2 = num_to_pos[query[i][2]];
			if (pos1 > pos2) {
				int t = pos1;
				pos1 = pos2;
				pos2 = t;
			}
			printf("%" PRId64 "\n", ki_get(pos1 + 1, pos2));
			ki_set(pos1 + 1, pos2, 0);
		}
	}

	return 0;
}

Submission Info

Submission Time
Task F - Erase between X and Y
User mikecat
Language C (gcc 12.2.0)
Score 525
Code Size 3453 Byte
Status AC
Exec Time 322 ms
Memory 40300 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 3
AC × 36
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 02_random2_00.txt, 02_random2_01.txt, 02_random2_02.txt, 02_random2_03.txt, 02_random2_04.txt, 03_handmade_00.txt, 03_handmade_01.txt, 03_handmade_02.txt, 03_handmade_03.txt, 03_handmade_04.txt, 03_handmade_05.txt, 03_handmade_06.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 1780 KiB
00_sample_01.txt AC 1 ms 1688 KiB
00_sample_02.txt AC 1 ms 1684 KiB
01_random_00.txt AC 208 ms 7656 KiB
01_random_01.txt AC 307 ms 14952 KiB
01_random_02.txt AC 306 ms 16364 KiB
01_random_03.txt AC 297 ms 17688 KiB
01_random_04.txt AC 293 ms 19176 KiB
01_random_05.txt AC 288 ms 20604 KiB
01_random_06.txt AC 282 ms 21932 KiB
01_random_07.txt AC 280 ms 23336 KiB
01_random_08.txt AC 271 ms 24848 KiB
01_random_09.txt AC 262 ms 26184 KiB
01_random_10.txt AC 258 ms 27548 KiB
01_random_11.txt AC 249 ms 28944 KiB
01_random_12.txt AC 243 ms 30548 KiB
01_random_13.txt AC 236 ms 31900 KiB
01_random_14.txt AC 228 ms 33236 KiB
01_random_15.txt AC 220 ms 34728 KiB
01_random_16.txt AC 213 ms 36168 KiB
01_random_17.txt AC 205 ms 37456 KiB
01_random_18.txt AC 198 ms 38964 KiB
01_random_19.txt AC 191 ms 40300 KiB
01_random_20.txt AC 322 ms 37264 KiB
02_random2_00.txt AC 198 ms 34636 KiB
02_random2_01.txt AC 181 ms 36808 KiB
02_random2_02.txt AC 172 ms 37032 KiB
02_random2_03.txt AC 164 ms 37248 KiB
02_random2_04.txt AC 159 ms 37388 KiB
03_handmade_00.txt AC 1 ms 1504 KiB
03_handmade_01.txt AC 153 ms 37120 KiB
03_handmade_02.txt AC 147 ms 37216 KiB
03_handmade_03.txt AC 156 ms 37424 KiB
03_handmade_04.txt AC 178 ms 22432 KiB
03_handmade_05.txt AC 176 ms 22520 KiB
03_handmade_06.txt AC 165 ms 22432 KiB


2025-09-20 (Sat)
05:12:02 +09:00