SystemYou 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.
User2026-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 131 -121
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 000 00
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).
Assistant2026-11-07T05:20:05.511Z
Writing the Kotlin solution and testing it on the examples.
Assistant2026-11-07T05:20:09.870Z
Saving the example inputs.
2 tool calls (write_file)
Assistant2026-11-07T05:20:16.536Z
Compiling the Kotlin solution.
Assistant2026-11-07T05:20:20.465Z
Running every example.
2 tool calls (run_shell)
Assistant2026-11-07T05:20:27.386Z
Cross-checking with the C++17 version.
Assistant2026-11-07T05:20:31.785Z
Compiling the C++17 solution.
Assistant2026-11-07T05:20:35.832Z
Running every example.
2 tool calls (run_shell)
Assistant2026-11-07T05:20:42.930Z
Cross-checking with the Go version.
Assistant2026-11-07T05:20:46.547Z
Compiling the Go solution.
Assistant2026-11-07T05:20:50.712Z
Running every example.
2 tool calls (run_shell)
Assistant2026-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.*
funmain() {
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 in1..n) p[i] = p[i - 1] + st.nextToken().toLong()
val s = p.copyOf()
s.sort()
val m = n + 1val bit = IntArray(m + 1)
funlowerBound(x: Long): Int {
var l = 0var r = m
while (l < r) {
val mid = (l + r) ushr 1if (s[mid] < x) l = mid + 1else r = mid
}
return l
}
funquery(k: Int): Int {
var i = k
var r = 0while (i > 0) { r += bit[i]; i -= i and -i }
return r
}
funadd(k: Int) {
var i = k + 1while (i <= m) { bit[i]++; i += i and -i }
}
var ans = 0Lfor (j in0..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>usingnamespace std;
intmain(){
int n;
longlong lo, hi;
scanf("%d %lld %lld", &n, &lo, &hi);
vector<longlong> p(n + 1, 0);
for (int i = 1; i <= n; i++) {
longlong x;
scanf("%lld", &x);
p[i] = p[i - 1] + x;
}
vector<longlong> s(p);
sort(s.begin(), s.end());
int m = n + 1;
vector<int> bit(m + 1, 0);
auto lb = [&](longlong 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; };
longlong 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"
)
funcmain() {
in := bufio.NewReaderSize(os.Stdin, 1<<20)
var n intvar 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 := 0for i := k; i > 0; i -= i & -i {
r += bit[i]
}
return r
}
var ans int64for 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)
}