a014.
夾娃娃
| From: [36.226.23.66] |
發表日期
:
2024-08-11 12:38
#pragma GCC optimize("Ofast", "unroll-loops", "no-stack-protector", "fast-math", "prefetch-loop-arrays")
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_INPUT_SIZE 100001 // Example limit for input size
int main() {
// Allocate memory dynamically based on the maximum possible range
int* e = (int*)calloc(MAX_INPUT_SIZE + 1, sizeof(int));
if (e == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
while (1) {
int n, y;
if (scanf("%d %d", &n, &y) != 2) {
break;
}
int max_count = 0, count = 0;
int min_index = MAX_INPUT_SIZE, max_index = 0;
for (int i = 0; i < n; i++) {
int a, b;
scanf("%d %d", &a, &b);
int start = (b - y < 0) ? 0 : b - y;
int end = a;
// Track the active range to minimize the reset operation later
if (start < min_index) min_index = start;
if (end > max_index) max_index = end;
for (int j = start; j <= end; j++) {
int value = ++e[j];
if (value > max_count) {
max_count = value;
count = 1;
} else if (value == max_count) {
count++;
}
}
}
printf("%d %d\n", count, max_count);
// Reset only the used portion of the array
for (int i = min_index; i <= max_index; i++) {
e[i] = 0;
}
}
// Free allocated memory
free(e);
return 0;
}