---
id: 949
lang: en
translation_of: index.md
source_sha256: 31d2aa38f2181ebf2314686c864158033101fb42a27d8f425691d379036edfd7
title: "P3924: Kanna's Segment Tree — Solution"
description: "Each leaf contributes its chain sum divided by a power of two set by its depth, so prefix sums answer every range-add query in O(1)."
tags: ["OI","Segment Tree","Prefix Sums"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 康娜的线段树 rendered as “Kanna's Segment Tree”."
  - "线段是 (sic) read as 线段树, “segment tree”."
  - "我们在讨论询问的问题 read as 我们再讨论询问的问题, “next we discuss the queries”."
  - "卡掉本题 rendered as “squeezing past this problem”."
---

## Approach

This problem is great, very interesting.

If you happen to be <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">a god</span>, you could consider <span class="slang" data-zh="卡掉" data-gloss="kǎdiào, “jam out”. OI slang; here: forcing a straightforward solution through a problem by squeezing it under the limits.">squeezing past</span> this problem directly with a segment tree (someone has tried it), but a <span class="slang" data-zh="菜鸡" data-gloss="càijī, “vegetable chicken”. Slang for a noob, someone who is bad at something; self-deprecating here.">scrub</span> like me can't, so I wrote up the $O(1)$-per-query solution.

First, the tree DP equation is:

$$
dp[u] = \frac{1}{2} * (dp[lson]+dp[rson])
$$

Looking at each leaf's contribution to the answer, we find it is the sum of the weights on its chain divided by $2^{dep-1}$, that is, it depends on the depth. So, to simplify the answer, we can write this as:

$$
\frac{1}{2^{dep-1}} = 2^{1-dep} = \frac { 2^{maxDep - dep} }{ 2^{maxDep-1} }
$$

and it becomes a multiplication. Next we discuss the queries. On the interval $[l,r]$, when we add $x$, a chain's contribution to the answer is:

$$
x · \sum_{i = 1}^{dep} \frac{1}{2^{i-1}}
$$

So maintain a prefix sum, and add to the answer the product of the prefix-sum range sum and this contribution to get the answer. (Remember to reduce the fraction with gcd.)

## Code

```cpp
// P3924.cpp
#include <bits/stdc++.h>
#define ll long long
#define mid ((l + r) >> 1)
#define lson (p << 1)
#define rson ((p << 1) | 1)
using namespace std;
const int MAX_N = 1e6 + 2000;
ll n, m, qwq, sum[MAX_N << 2], depth[MAX_N << 2], arr[MAX_N], s[MAX_N], max_dep;
inline ll read()
{
    ll s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9')
    {
        if (ch == '-')
            w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
        s = s * 10 + ch - '0', ch = getchar();
    return s * w;
}
void build(ll l, ll r, ll dp, ll p)
{
    if (l == r)
    {
        sum[p] = arr[l], depth[l] = dp, max_dep = max(max_dep, dp);
        return;
    }
    build(l, mid, dp + 1, lson), build(mid + 1, r, dp + 1, rson);
    sum[p] = sum[lson] + sum[rson];
}
ll query(ll l, ll r, ll p, ll dep, ll prefix)
{
    if (l == r)
        return (1 << dep) * (prefix + sum[p]);
    return query(l, mid, lson, dep - 1, prefix + sum[p]) + query(mid + 1, r, rson, dep - 1, prefix + sum[p]);
}
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
int main()
{
    n = read(), m = read(), qwq = read();
    for (int i = 1; i <= n; i++)
        arr[i] = read();
    build(1, n, 1, 1);
    ll ans = query(1, n, 1, max_dep - 1, 0), dominator = 1 << (max_dep - 1);
    ll fact = gcd(dominator, qwq);
    dominator /= fact, qwq /= fact;
    for (int i = 1; i <= n; i++)
        s[i] = s[i - 1] + (((1 << depth[i]) - 1) << (max_dep - depth[i]));
    while (m--)
    {
        ll l = read(), r = read(), w = read();
        ans += (s[r] - s[l - 1]) * w;
        printf("%lld\n", ans / dominator * qwq);
    }
    return 0;
}
```
