-
Notifications
You must be signed in to change notification settings - Fork 0
/
Organizing_A_Lottery.c
75 lines (69 loc) · 1.95 KB
/
Organizing_A_Lottery.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void swap(int*, int*);
int* partition(int*, int*, int, int);
void three_way_quicksort(int*, int*, int, int);
void scan(int*, int*, int, int, int, int*);
void swap(int* p, int* q)
{
int temp = *p;
*p = *q;
*q = temp;
}
int* partition(int* seg_starts, int* seg_ends, int low, int high)
{
int* partitions = (int*)malloc(2 * sizeof(int)), i = low;
partitions[0] = low;
partitions[1] = high;
while(i <= partitions[1])
{
if(seg_ends[i] < seg_ends[partitions[0]])
{
swap(&seg_ends[i], &seg_ends[partitions[0]]);
swap(&seg_starts[i++], &seg_starts[(partitions[0])++]);
}
else if(seg_ends[i] > seg_ends[partitions[0]])
{
swap(&seg_ends[i], &seg_ends[partitions[1]]);
swap(&seg_starts[i], &seg_starts[(partitions[1])--]);
}
else i++;
}
return partitions;
}
void three_way_quicksort(int* seg_starts, int* seg_ends, int low, int high)
{
if(low < high)
{
int temp = low + rand() % (high - low + 1);
swap(&seg_starts[temp], &seg_starts[low]);
swap(&seg_ends[temp], &seg_ends[low]);
int* partitions = partition(seg_starts, seg_ends, low, high);
three_way_quicksort(seg_starts, seg_ends, low, partitions[0] - 1);
three_way_quicksort(seg_starts, seg_ends, partitions[1] + 1, high);
}
}
void scan(int* seg_starts, int* seg_ends, int low, int high, int point, int* count)
{
for(int i = high; i >= low; i--)
{
if(point > seg_ends[i]) break;
if(point <= seg_ends[i] && point >= seg_starts[i]) (*count)++;
}
}
void main()
{
int ns, np, i, count;
scanf("%d%d", &ns, &np);
int seg_starts[ns], seg_ends[ns], points[np];
for(i = 0; i < ns; i++) scanf("%d%d", &seg_starts[i], &seg_ends[i]);
for(i = 0; i < np; i++) scanf("%d", &points[i]);
three_way_quicksort(seg_starts, seg_ends, 0, ns - 1);
for(i = 0; i < np; i++)
{
count = 0;
scan(seg_starts, seg_ends, 0, ns - 1, points[i], &count);
printf("%d ", count);
}
}