tinycodinglm / cp2 /cp-0001.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
3.88 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.

User 2026-11-06T22:20:03.537Z

Warehouse Shelf Totals

Topic: Arrays and prefix sums | Difficulty: beginner

Problem

A warehouse has n shelves in a row. Shelf i holds a[i] boxes (a value may be negative when boxes are owed to a supplier). You will receive q questions; each asks for the total number of boxes on shelves l, l+1, ..., r.

Input

The first line contains n and q. The second line contains n integers a[1..n]. Each of the next q lines contains two integers l and r.

Output

For each question print the total on its own line.

Constraints

  • 1 <= n, q <= 2*10^5
  • |a[i]| <= 10^9
  • 1 <= l <= r <= n

Examples

Example 1

Input:

markdown
5 3
1 2 3 4 5
1 5
2 3
4 4

Output:

markdown
15
5
4

Example 2

Input:

markdown
3 2
-5 10 -7
1 3
1 1

Output:

markdown
-2
-5

Note: Negative values are allowed, and a query may cover a single shelf.

Solve this in C++17, Rust, Kotlin, one complete program per language. Include a concise rationale (approach, correctness idea, time/space complexity).

Assistant 2026-11-06T22:20:05.511Z

Approach. Build prefix sums P where P[i] is the sum of the first i values. Each query is P[r] - P[l-1].

Correctness. P[r] contains a[1..r] and P[l-1] contains a[1..l-1], so the difference is exactly a[l..r].

Complexity. Time: O(n + q). Space: O(n).

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    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;
    }
    while (q--) {
        int l, r;
        scanf("%d %d", &l, &r);
        printf("%lld\n", p[r] - p[l - 1]);
    }
    return 0;
}

Rust

rust
use std::io::*;
fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    let mut it = s.split_ascii_whitespace().map(|x| x.parse::<i64>().unwrap());
    let n = it.next().unwrap() as usize;
    let q = it.next().unwrap() as usize;
    let mut p = vec![0i64; n + 1];
    for i in 0..n {
        p[i + 1] = p[i] + it.next().unwrap();
    }
    let mut o = String::new();
    for _ in 0..q {
        let l = it.next().unwrap() as usize;
        let r = it.next().unwrap() as usize;
        o.push_str(&format!("{}\n", p[r] - p[l - 1]));
    }
    print!("{}", o);
}

Kotlin

kotlin
import java.util.StringTokenizer

fun main() {
    val st = StringTokenizer(System.`in`.bufferedReader().readText())
    val n = st.nextToken().toInt()
    val q = st.nextToken().toInt()
    val p = LongArray(n + 1)
    for (i in 1..n) p[i] = p[i - 1] + st.nextToken().toLong()
    val sb = StringBuilder()
    repeat(q) {
        val l = st.nextToken().toInt()
        val r = st.nextToken().toInt()
        sb.append(p[r] - p[l - 1]).append('\n')
    }
    print(sb)
}