To solve this problem efficiently, we'll use a Segment Tree, which allows us to perform efficient range queries and updates. Specifically, we'll use it to keep track of the minimum value and its first occurrence index in the array nums. Here's the full C code that implements this solution:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
int64_t min_value;
int min_index;
} SegmentTreeNode;
void buildSegmentTree(SegmentTreeNode *tree, int64_t *nums, int node, int start, int end) {
if (start == end) {
// Leaf node: store the value and its index
tree[node].min_value = nums[start];
tree[node].min_index = start;
} else {
int mid = (start + end) / 2;
// Build left and right subtrees
buildSegmentTree(tree, nums, 2 * node, start, mid);
buildSegmentTree(tree, nums, 2 * node + 1, mid + 1, end);
// Combine the results from left and right subtrees
if (tree[2 * node].min_value < tree[2 * node + 1].min_value) {
tree[node] = tree[2 * node];
} else if (tree[2 * node].min_value > tree[2 * node + 1].min_value) {
tree[node] = tree[2 * node + 1];
} else {
// If values are equal, choose the one with the smaller index
if (tree[2 * node].min_index <= tree[2 * node + 1].min_index) {
tree[node] = tree[2 * node];
} else {
tree[node] = tree[2 * node + 1];
}
}
}
}
void updateSegmentTree(SegmentTreeNode *tree, int node, int start, int end, int idx, int64_t value) {
if (start == end) {
// Update the value at the leaf node
tree[node].min_value = value;
tree[node].min_index = idx;
} else {
int mid = (start + end) / 2;
if (idx <= mid) {
// Update left subtree
updateSegmentTree(tree, 2 * node, start, mid, idx, value);
} else {
// Update right subtree
updateSegmentTree(tree, 2 * node + 1, mid + 1, end, idx, value);
}
// After updating, combine results from left and right subtrees
if (tree[2 * node].min_value < tree[2 * node + 1].min_value) {
tree[node] = tree[2 * node];
} else if (tree[2 * node].min_value > tree[2 * node + 1].min_value) {
tree[node] = tree[2 * node + 1];
} else {
// If values are equal, choose the one with the smaller index
if (tree[2 * node].min_index <= tree[2 * node + 1].min_index) {
tree[node] = tree[2 * node];
} else {
tree[node] = tree[2 * node + 1];
}
}
}
}
int* getFinalState(int* nums, int numsSize, int k, int multiplier, int* returnSize) {
// Use int64_t for computations to avoid overflow
int64_t *nums64 = (int64_t *)malloc(numsSize * sizeof(int64_t));
for (int i = 0; i < numsSize; i++) {
nums64[i] = nums[i];
}
// Initialize the segment tree
SegmentTreeNode *tree = (SegmentTreeNode *)malloc(4 * numsSize * sizeof(SegmentTreeNode));
buildSegmentTree(tree, nums64, 1, 0, numsSize - 1);
// Perform k operations
for (int i = 0; i < k; i++) {
// Get the minimum value and its index from the root of the segment tree
int64_t min_value = tree[1].min_value;
int min_index = tree[1].min_index;
// Update the value at the index
nums64[min_index] = min_value * multiplier;
// Update the segment tree with the new value
updateSegmentTree(tree, 1, 0, numsSize - 1, min_index, nums64[min_index]);
}
// Apply modulo 1e9 + 7 to each element and prepare the result
int64_t MOD = 1000000007;
int *result = (int *)malloc(numsSize * sizeof(int));
for (int i = 0; i < numsSize; i++) {
nums64[i] %= MOD;
result[i] = (int)nums64[i];
}
// Set the return size
*returnSize = numsSize;
// Free allocated memory
free(nums64);
free(tree);
return result;
}
Explanation:
- Segment Tree Structure: Each node in the segment tree stores the minimum value in a segment of the array and the index of its first occurrence.
- Building the Segment Tree: We recursively build the segment tree by dividing the array into halves until we reach the leaf nodes (individual elements), then we combine the results while respecting the condition of choosing the first occurrence in case of equal values.
- Updating the Segment Tree: When we update a value in the array, we also update the segment tree to reflect the new minimum values and indices.
- Performing Operations: For each of the
k operations, we retrieve the current minimum value and its index from the root of the segment tree, update the value by multiplying with the multiplier, and update the segment tree accordingly.
- Applying Modulo and Preparing the Result: After performing all operations, we apply the modulo (10^9 + 7) to each element and prepare the result array to return.
Example Usage:
int main() {
int nums[] = {2, 1, 3, 5, 6};
int numsSize = 5;
int k = 5;
int multiplier = 2;
int returnSize;
int *result = getFinalState(nums, numsSize, k, multiplier, &returnSize);
printf("[");
for (int i = 0; i < returnSize; i++) {
printf("%d", result[i]);
if (i < returnSize - 1) printf(", ");
}
printf("]\n");
free(result);
return 0;
}
Output:
[8, 4, 6, 5, 6]
This code efficiently solves the problem by ensuring that each operation is done in (O(\log n)) time, resulting in a total time complexity of (O(k \log n)), which is acceptable for the given constraints.