tinycodinglm / cp2 /cp-0015.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
12.7 kB
cp-practice-sim trace
System You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation. Tools: write_file(path, content) writes...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Reply with code only; no explanation. Tools: write_file(path, content) writes a UTF-8 file (creating directories); run_shell(command) runs a bash command and returns its output and exit status. Test solutions on the provided examples before answering.

User 2026-11-07T12:20:03.537Z

Variety Band Subarrays

Topic: Two pointers and sliding window | Difficulty: medium

Problem

A playlist has n tracks, track i belonging to genre a[i]. Count the contiguous non-empty blocks of tracks whose number of distinct genres is at least lo and at most hi.

Input

The first line contains n, lo and hi. The second line contains a[1..n].

Output

Print the number of such blocks.

Constraints

  • 1 <= n <= 2*10^5
  • 1 <= lo <= hi <= n
  • 1 <= a[i] <= n

Examples

Example 1

Input:

markdown
5 2 2
1 2 1 2 3

Output:

markdown
7

Note: Blocks with exactly two genres: [1,2], [2,1], [1,2] (positions 3-4), [1,2,1], [2,1,2], [1,2,1,2], [2,3].

Example 2

Input:

markdown
5 1 2
1 2 1 2 3

Output:

markdown
12

Note: Of the 15 blocks only the three containing genre 3 and both 1 and 2 have three genres.

Solve this task. Write solutions in Go, Kotlin, Java 17, test what you can run on the examples, and send back the final programs. Final answer: code only, no explanation.

Assistant 2026-11-07T12:20:05.511Z

Writing the Go solution and testing it on the examples.

Assistant 2026-11-07T12:20:09.870Z

Saving the example inputs.

2 tool calls (write_file)
Assistant 2026-11-07T12:20:16.536Z

Compiling the Go solution.

Assistant 2026-11-07T12:20:20.465Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T12:20:27.386Z

Cross-checking with the Kotlin version.

Assistant 2026-11-07T12:20:31.785Z

Compiling the Kotlin solution.

Assistant 2026-11-07T12:20:35.832Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T12:20:42.930Z

Cross-checking with the Java 17 version.

Assistant 2026-11-07T12:20:46.547Z

Compiling the Java 17 solution.

Assistant 2026-11-07T12:20:50.712Z

Running every example.

2 tool calls (run_shell)
Assistant 2026-11-07T12:20:57.987Z

All solutions below passed every example.

Go

go
package main

import (
    "bufio"
    "fmt"
    "os"
)

var n int
var a []int

func atMost(k int) int64 {
    if k <= 0 {
        return 0
    }
    cnt := make([]int, n+1)
    distinct, l := 0, 0
    var total int64
    for r := 0; r < n; r++ {
        if cnt[a[r]] == 0 {
            distinct++
        }
        cnt[a[r]]++
        for distinct > k {
            cnt[a[l]]--
            if cnt[a[l]] == 0 {
                distinct--
            }
            l++
        }
        total += int64(r - l + 1)
    }
    return total
}

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var lo, hi int
    fmt.Fscan(in, &n, &lo, &hi)
    a = make([]int, n)
    for i := range a {
        fmt.Fscan(in, &a[i])
    }
    fmt.Println(atMost(hi) - atMost(lo-1))
}

Kotlin

kotlin
import java.util.*

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val lo = st.nextToken().toInt()
    val hi = st.nextToken().toInt()
    val a = IntArray(n) { st.nextToken().toInt() }
    fun atMost(k: Int): Long {
        if (k <= 0) return 0L
        val cnt = IntArray(n + 1)
        var distinct = 0
        var l = 0
        var total = 0L
        for (r in 0 until n) {
            if (cnt[a[r]] == 0) distinct++
            cnt[a[r]]++
            while (distinct > k) {
                cnt[a[l]]--
                if (cnt[a[l]] == 0) distinct--
                l++
            }
            total += (r - l + 1).toLong()
        }
        return total
    }
    println(atMost(hi) - atMost(lo - 1))
}

Java 17

java
import java.util.*;

public class Main {
    static int n;
    static int[] a;

    static long atMost(int k) {
        if (k <= 0) return 0;
        int[] cnt = new int[n + 1];
        int distinct = 0, l = 0;
        long total = 0;
        for (int r = 0; r < n; r++) {
            if (cnt[a[r]]++ == 0) distinct++;
            while (distinct > k) {
                if (--cnt[a[l]] == 0) distinct--;
                l++;
            }
            total += r - l + 1;
        }
        return total;
    }

    public static void main(String[] args) throws Exception {
        StringTokenizer st = new StringTokenizer(new String(System.in.readAllBytes()));
        n = Integer.parseInt(st.nextToken());
        int lo = Integer.parseInt(st.nextToken());
        int hi = Integer.parseInt(st.nextToken());
        a = new int[n];
        for (int i = 0; i < n; i++) a[i] = Integer.parseInt(st.nextToken());
        System.out.println(atMost(hi) - atMost(lo - 1));
    }
}