tinycodinglm / cp2 /cp-0008.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
13.8 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. Give only a concise rationale: approach, correctness idea, and time/space com...

You are a competitive-programming assistant. Deliver correct, complete, runnable solutions that read standard input and write standard output. Give only a concise rationale: approach, correctness idea, and time/space complexity. 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-07T05:20:03.537Z

Budget Band Count

Topic: Arrays and prefix sums | Difficulty: hard

Problem

A ledger has n entries a[1..n] (possibly negative). Count the contiguous non-empty subarrays whose sum is at least lo and at most hi.

Input

The first line contains n, lo and hi. The second line contains n integers.

Output

Print the number of subarrays whose sum lies in [lo, hi].

Constraints

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

Examples

Example 1

Input:

markdown
4 1 3
1 -1 2 1

Output:

markdown
8

Note: Only the subarrays [1,2] (sum 0) and [2,2] (sum -1) fall outside [1,3] among the ten subarrays.

Example 2

Input:

markdown
3 0 0
0 0 0

Output:

markdown
6

Note: All six subarrays sum to zero.

Solve this task. Write solutions in Kotlin, C++17, Go, test what you can run on the examples, and send back the final programs. Include a concise rationale (approach, correctness idea, time/space complexity).

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

Writing the Kotlin solution and testing it on the examples.

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

Saving the example inputs.

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

Compiling the Kotlin solution.

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

Running every example.

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

Cross-checking with the C++17 version.

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

Compiling the C++17 solution.

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

Running every example.

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

Cross-checking with the Go version.

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

Compiling the Go solution.

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

Running every example.

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

Approach. Let P be the prefix sums P[0..n]. A subarray (i, j) with i < j is counted when lo <= P[j]-P[i] <= hi; sweep j and query a Fenwick tree, indexed by the sorted prefix values, for the number of earlier P[i] in [P[j]-hi, P[j]-lo].

Correctness. Every subarray corresponds to exactly one pair i < j of prefix indices, and the Fenwick tree holds precisely the earlier prefix values when j is processed, so each pair is tested once.

Complexity. Time: O(n log n). Space: O(n).

All solutions below passed every example.

Kotlin

kotlin
import java.io.*
import java.util.*

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val lo = st.nextToken().toLong()
    val hi = st.nextToken().toLong()
    val p = LongArray(n + 1)
    for (i in 1..n) p[i] = p[i - 1] + st.nextToken().toLong()
    val s = p.copyOf()
    s.sort()
    val m = n + 1
    val bit = IntArray(m + 1)
    fun lowerBound(x: Long): Int {
        var l = 0
        var r = m
        while (l < r) {
            val mid = (l + r) ushr 1
            if (s[mid] < x) l = mid + 1 else r = mid
        }
        return l
    }
    fun query(k: Int): Int {
        var i = k
        var r = 0
        while (i > 0) { r += bit[i]; i -= i and -i }
        return r
    }
    fun add(k: Int) {
        var i = k + 1
        while (i <= m) { bit[i]++; i += i and -i }
    }
    var ans = 0L
    for (j in 0..n) {
        val a = lowerBound(p[j] - hi)
        val b = lowerBound(p[j] - lo + 1)
        ans += query(b) - query(a)
        add(lowerBound(p[j]))
    }
    println(ans)
}

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    long long lo, hi;
    scanf("%d %lld %lld", &n, &lo, &hi);
    vector<long long> p(n + 1, 0);
    for (int i = 1; i <= n; i++) {
        long long x;
        scanf("%lld", &x);
        p[i] = p[i - 1] + x;
    }
    vector<long long> s(p);
    sort(s.begin(), s.end());
    int m = n + 1;
    vector<int> bit(m + 1, 0);
    auto lb = [&](long long x) { return (int)(lower_bound(s.begin(), s.end(), x) - s.begin()); };
    auto query = [&](int k) { int r = 0; for (int i = k; i > 0; i -= i & -i) r += bit[i]; return r; };
    long long ans = 0;
    for (int j = 0; j <= n; j++) {
        ans += query(lb(p[j] - lo + 1)) - query(lb(p[j] - hi));
        for (int i = lb(p[j]) + 1; i <= m; i += i & -i) bit[i]++;
    }
    printf("%lld\n", ans);
}

Go

go
package main

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

func main() {
    in := bufio.NewReaderSize(os.Stdin, 1<<20)
    var n int
    var lo, hi int64
    fmt.Fscan(in, &n, &lo, &hi)
    p := make([]int64, n+1)
    for i := 1; i <= n; i++ {
        var x int64
        fmt.Fscan(in, &x)
        p[i] = p[i-1] + x
    }
    s := make([]int64, n+1)
    copy(s, p)
    sort.Slice(s, func(a, b int) bool { return s[a] < s[b] })
    m := n + 1
    bit := make([]int, m+1)
    lb := func(x int64) int {
        return sort.Search(m, func(i int) bool { return s[i] >= x })
    }
    query := func(k int) int {
        r := 0
        for i := k; i > 0; i -= i & -i {
            r += bit[i]
        }
        return r
    }
    var ans int64
    for j := 0; j <= n; j++ {
        ans += int64(query(lb(p[j]-lo+1)) - query(lb(p[j]-hi)))
        for i := lb(p[j]) + 1; i <= m; i += i & -i {
            bit[i]++
        }
    }
    fmt.Println(ans)
}