/ SeriousOJ /

Record Detail

Compile Error

/w/foo.js:1
import sys
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at node:internal/main/check_syntax:84:41
    at loadESM (node:internal/process/esm_loader:34:13)
    at checkSyntax (node:internal/main/check_syntax:84:21)

Node.js v18.19.1

Code

import sys
input = sys.stdin.read
from collections import defaultdict
from math import sqrt

# Function to preprocess data for faster queries
def preprocess_array(A, N):
    # Stores occurrences of each element at each index
    positions = defaultdict(list)
    for i in range(N):
        positions[A[i]].append(i)
    return positions

# Binary search to count occurrences in a range [l, r]
def count_in_range(positions, x, l, r):
    from bisect import bisect_left, bisect_right
    pos_list = positions[x]
    return bisect_right(pos_list, r) - bisect_left(pos_list, l)

def process_queries(N, Q, A, queries):
    # Step 1: Preprocess the positions of each element in the array
    positions = preprocess_array(A, N)
    results = []
    
    # Step 2: Process each query
    for l, r in queries:
        l -= 1  # convert to 0-based index
        r -= 1
        
        threshold = (r - l + 1) // 3
        found = -1

        # Collect potential candidates (at most 2 elements)
        candidates = set(A[l:r+1][:2])  # take first two elements in range as starting candidates
        for candidate in candidates:
            # Count occurrences of candidate in range [l, r]
            count = count_in_range(positions, candidate, l, r)
            if count > threshold:
                if found == -1 or candidate < found:
                    found = candidate
        
        # Append the result for this query
        results.append(str(found))
    
    return "\n".join(results)

# Main function to handle input and output
def main():
    data = input().split()
    idx = 0
    N = int(data[idx])
    Q = int(data[idx + 1])
    idx += 2
    A = list(map(int, data[idx:idx + N]))
    idx += N
    queries = []
    for _ in range(Q):
        l = int(data[idx])
        r = int(data[idx + 1])
        queries.append((l, r))
        idx += 2
    results = process_queries(N, Q, A, queries)
    print(results)

Information

Submit By
Type
Submission
Problem
P1103 The Secret Garden of Numbers
Contest
Brain Booster #7
Language
JavaScript (Node.js v18.19.1)
Submit At
2024-11-05 15:12:51
Judged At
2024-11-05 15:12:51
Judged By
Score
0
Total Time
0ms
Peak Memory
0 Bytes