11765 | 2024-11-09 16:44:39 | balint | Sokszorozott maximumok | python3 | Time limit exceeded 11/100 | 2.101s | 70260 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()
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 19ms | 3384 KiB | ||||
subtask2 | 11/11 | ||||||
2 | Accepted | 29ms | 3640 KiB | ||||
3 | Accepted | 35ms | 3976 KiB | ||||
4 | Accepted | 180ms | 9016 KiB | ||||
5 | Accepted | 515ms | 14968 KiB | ||||
6 | Accepted | 19ms | 3384 KiB | ||||
7 | Accepted | 119ms | 7468 KiB | ||||
subtask3 | 0/13 | ||||||
8 | Accepted | 29ms | 3636 KiB | ||||
9 | Accepted | 34ms | 3892 KiB | ||||
10 | Accepted | 252ms | 9268 KiB | ||||
11 | Time limit exceeded | 2.091s | 14320 KiB | ||||
12 | Time limit exceeded | 2.088s | 26700 KiB | ||||
13 | Time limit exceeded | 2.101s | 26696 KiB | ||||
subtask4 | 0/19 | ||||||
14 | Time limit exceeded | 2.089s | 68656 KiB | ||||
15 | Time limit exceeded | 2.091s | 66188 KiB | ||||
16 | Time limit exceeded | 2.091s | 68164 KiB | ||||
17 | Time limit exceeded | 2.089s | 70080 KiB | ||||
18 | Time limit exceeded | 2.089s | 70260 KiB | ||||
subtask5 | 0/25 | ||||||
19 | Time limit exceeded | 2.081s | 14900 KiB | ||||
20 | Time limit exceeded | 2.079s | 13308 KiB | ||||
21 | Time limit exceeded | 2.081s | 14072 KiB | ||||
22 | Time limit exceeded | 2.081s | 14780 KiB | ||||
23 | Time limit exceeded | 2.079s | 17664 KiB | ||||
subtask6 | 0/32 | ||||||
24 | Time limit exceeded | 2.085s | 26700 KiB | ||||
25 | Time limit exceeded | 2.085s | 26700 KiB | ||||
26 | Time limit exceeded | 2.085s | 22100 KiB | ||||
27 | Time limit exceeded | 2.085s | 24264 KiB | ||||
28 | Time limit exceeded | 2.085s | 27112 KiB | ||||
29 | Time limit exceeded | 2.085s | 26700 KiB | ||||
30 | Time limit exceeded | 2.085s | 24560 KiB |