tinycodinglm / cp2 /cp-0004.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 15)
b27bfb3 verified
|
Raw History Blame Contribute Delete
4.3 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-07T01:20:03.537Z

Paint Layers

Topic: Arrays and prefix sums | Difficulty: easy

Problem

A fence has n planks; plank i currently has thickness a[i]. Painters apply q operations in order. Operation (l, r, v) adds v to the thickness of every plank from l to r inclusive (v may be negative). Print the thickness of every plank after all operations.

Input

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

Output

Print n integers separated by single spaces: the final thicknesses.

Constraints

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

Examples

Example 1

Input:

markdown
5 2
1 1 1 1 1
1 3 2
2 5 -1

Output:

markdown
3 2 2 0 0

Example 2

Input:

markdown
1 1
5
1 1 1000000000

Output:

markdown
1000000005

Note: Values exceed 32-bit range in general; use 64-bit integers.

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

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

Approach. Record each update in a difference array (add v at l, subtract v at r+1) and take a prefix sum at the end to recover the added amounts.

Correctness. The prefix sum of the difference array at position i equals the sum of v over all updates covering i.

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

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 a: Vec<i64> = (0..n).map(|_| it.next().unwrap()).collect();
    let mut d = vec![0i64; n + 2];
    for _ in 0..q {
        let l = it.next().unwrap() as usize;
        let r = it.next().unwrap() as usize;
        let v = it.next().unwrap();
        d[l] += v;
        d[r + 1] -= v;
    }
    let mut cur = 0i64;
    let mut out = Vec::with_capacity(n);
    for i in 1..=n {
        cur += d[i];
        out.push((a[i - 1] + cur).to_string());
    }
    println!("{}", out.join(" "));
}

TypeScript

typescript
declare const require: any;
const t: number[] = require('fs').readFileSync(0, 'utf8').split(/\s+/).filter((x: string) => x.length).map(Number);
const n: number = t[0];
const q: number = t[1];
const d: number[] = new Array(n + 2).fill(0);
let p = 2 + n;
for (let k = 0; k < q; k++) {
  const l = t[p], r = t[p + 1], v = t[p + 2];
  p += 3;
  d[l] += v;
  d[r + 1] -= v;
}
let cur = 0;
const out: number[] = [];
for (let i = 1; i <= n; i++) {
  cur += d[i];
  out.push(t[1 + i] + cur);
}
console.log(out.join(' '));

C++17

cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, q;
    scanf("%d %d", &n, &q);
    vector<long long> a(n + 1), d(n + 2, 0);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    while (q--) {
        int l, r;
        long long v;
        scanf("%d %d %lld", &l, &r, &v);
        d[l] += v;
        d[r + 1] -= v;
    }
    long long cur = 0;
    for (int i = 1; i <= n; i++) {
        cur += d[i];
        printf("%lld%c", a[i] + cur, i == n ? '\n' : ' ');
    }
    return 0;
}