|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <math.h> |
| 4 | + |
| 5 | +#define MAX_N 100000 |
| 6 | +#define MAX_Q 100000 |
| 7 | + |
| 8 | +// Structure for each query |
| 9 | +typedef struct { |
| 10 | + int L, R, idx; |
| 11 | +} Query; |
| 12 | + |
| 13 | +int n, q; |
| 14 | +int arr[MAX_N]; |
| 15 | +Query queries[MAX_Q]; |
| 16 | +int answer[MAX_Q]; |
| 17 | +int current_answer = 0; |
| 18 | +int freq[1000001]; |
| 19 | + |
| 20 | +// Comparator function to sort queries by Mo's ordering |
| 21 | +int compare(const void *a, const void *b) { |
| 22 | + Query *q1 = (Query *)a; |
| 23 | + Query *q2 = (Query *)b; |
| 24 | + int block1 = q1->L / (int)sqrt(n); |
| 25 | + int block2 = q2->L / (int)sqrt(n); |
| 26 | + if (block1 != block2) { |
| 27 | + return block1 - block2; |
| 28 | + } |
| 29 | + return (block1 & 1) ? (q1->R - q2->R) : (q2->R - q1->R); |
| 30 | +} |
| 31 | + |
| 32 | +// Function to add element at index pos |
| 33 | +void add(int pos) { |
| 34 | + current_answer += arr[pos]; |
| 35 | +} |
| 36 | + |
| 37 | +// Function to remove element at index pos |
| 38 | +void remove(int pos) { |
| 39 | + current_answer -= arr[pos]; |
| 40 | +} |
| 41 | + |
| 42 | +// Mo's Algorithm to answer all queries |
| 43 | +void mo_algorithm() { |
| 44 | + int currentL = 0, currentR = -1; |
| 45 | + for (int i = 0; i < q; i++) { |
| 46 | + int L = queries[i].L; |
| 47 | + int R = queries[i].R; |
| 48 | + |
| 49 | + // Adjust the left boundary |
| 50 | + while (currentL < L) { |
| 51 | + remove(currentL); |
| 52 | + currentL++; |
| 53 | + } |
| 54 | + while (currentL > L) { |
| 55 | + currentL--; |
| 56 | + add(currentL); |
| 57 | + } |
| 58 | + |
| 59 | + // Adjust the right boundary |
| 60 | + while (currentR < R) { |
| 61 | + currentR++; |
| 62 | + add(currentR); |
| 63 | + } |
| 64 | + while (currentR > R) { |
| 65 | + remove(currentR); |
| 66 | + currentR--; |
| 67 | + } |
| 68 | + |
| 69 | + // Store the answer for this query |
| 70 | + answer[queries[i].idx] = current_answer; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +int main() { |
| 75 | + // Input array size and elements |
| 76 | + printf("Enter the size of the array: "); |
| 77 | + scanf("%d", &n); |
| 78 | + for (int i = 0; i < n; i++) { |
| 79 | + scanf("%d", &arr[i]); |
| 80 | + } |
| 81 | + |
| 82 | + // Input number of queries and their ranges |
| 83 | + printf("Enter the number of queries: "); |
| 84 | + scanf("%d", &q); |
| 85 | + for (int i = 0; i < q; i++) { |
| 86 | + scanf("%d %d", &queries[i].L, &queries[i].R); |
| 87 | + queries[i].L--; // Convert to zero-based indexing |
| 88 | + queries[i].R--; |
| 89 | + queries[i].idx = i; |
| 90 | + } |
| 91 | + |
| 92 | + // Sort queries according to Mo's ordering |
| 93 | + qsort(queries, q, sizeof(Query), compare); |
| 94 | + |
| 95 | + // Execute Mo's Algorithm |
| 96 | + mo_algorithm(); |
| 97 | + |
| 98 | + // Output the answers for each query |
| 99 | + printf("Query Results:\n"); |
| 100 | + for (int i = 0; i < q; i++) { |
| 101 | + printf("Query %d: %d\n", i + 1, answer[i]); |
| 102 | + } |
| 103 | + |
| 104 | + return 0; |
| 105 | +} |
0 commit comments