117652024-11-09 16:44:39balintSokszorozott maximumokpython3Time limit exceeded 11/1002.101s70260 KiB
from math import prod
from collections import defaultdict


def main():
    MOD = 10**9 + 7
    N, Q = map(int, input().split())
    nums = list(map(int, input().split()))

    # Memoization caches
    sorted_cache = defaultdict(list)  # Cache for sorted subarrays
    product_cache = {}  # Cache for product of top k elements

    # Function to compute the product of top k elements
    def get_top_k_product(L, R, k):
        # If we already have the result in the cache, return it
        if (L, R, k) in product_cache:
            return product_cache[(L, R, k)]

        # Otherwise, compute it from scratch
        sub = nums[L : R + 1]
        if (L, R) not in sorted_cache:
            sorted_cache[(L, R)] = sorted(
                sub, reverse=True
            )  # Sort only once for each range
        top_k = sorted_cache[(L, R)][:k]

        # Calculate the product of the top k elements
        result = prod(top_k) % MOD
        product_cache[(L, R, k)] = result  # Cache the result

        return result

    # Process queries
    for _ in range(Q):
        L, R, k = map(int, input().split())
        print(get_top_k_product(L, R, k))


main()
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted19ms3384 KiB
subtask211/11
2Accepted29ms3640 KiB
3Accepted35ms3976 KiB
4Accepted180ms9016 KiB
5Accepted515ms14968 KiB
6Accepted19ms3384 KiB
7Accepted119ms7468 KiB
subtask30/13
8Accepted29ms3636 KiB
9Accepted34ms3892 KiB
10Accepted252ms9268 KiB
11Time limit exceeded2.091s14320 KiB
12Time limit exceeded2.088s26700 KiB
13Time limit exceeded2.101s26696 KiB
subtask40/19
14Time limit exceeded2.089s68656 KiB
15Time limit exceeded2.091s66188 KiB
16Time limit exceeded2.091s68164 KiB
17Time limit exceeded2.089s70080 KiB
18Time limit exceeded2.089s70260 KiB
subtask50/25
19Time limit exceeded2.081s14900 KiB
20Time limit exceeded2.079s13308 KiB
21Time limit exceeded2.081s14072 KiB
22Time limit exceeded2.081s14780 KiB
23Time limit exceeded2.079s17664 KiB
subtask60/32
24Time limit exceeded2.085s26700 KiB
25Time limit exceeded2.085s26700 KiB
26Time limit exceeded2.085s22100 KiB
27Time limit exceeded2.085s24264 KiB
28Time limit exceeded2.085s27112 KiB
29Time limit exceeded2.085s26700 KiB
30Time limit exceeded2.085s24560 KiB