---
id: 1943
lang: en
translation_of: index.md
source_sha256: 8accdc55b5da39750a834292b1fe5815dcb481ac9bb903792120ed903c71c656
title: "Range Division and Square Root with a Segment Tree"
description: "If the spread of values in an interval is small, division or square root acts the same on all of it, so it can be turned into a subtraction."
tags: ["OI","Data Structures"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "极差 (max minus min of the values) rendered as “the spread of values”, to avoid clashing with “range” meaning the interval."
---

Range division and square root are about handling the following two operations:

1. $\forall i \in [l, r], \lfloor \frac{a_i}{d} \rfloor \to a_i$
2. $\forall i \in [l, r], \lfloor \sqrt{a_i} \rfloor \to a_i$

These operations are very hard to implement with ordinary lazy tags, but we can transform them a bit, because within a certain range of values they can be processed in bulk. In other words, if the spread of values in the interval is small, then division or square root has the same effect on the whole interval, so it can be turned into a subtraction.

Take division as an example: in the interval $(l, r)$, if $\max(S) - \lfloor \frac{\max(S)}{d} \rfloor = \min(S) - \lfloor \frac{\min(S)}{d} \rfloor$, then we can use this difference as the subtrahend and tag it in the usual add/subtract way.

<!--more-->

## Example Problems

**LOJ6029  Implementing Division**

```cpp
// LOJ6029.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200;
typedef long long ll;
const ll INF = 0x7fffffffffffffff;

ll nodes[MAX_N << 2], max_val[MAX_N << 2], min_val[MAX_N << 2], tag[MAX_N << 2];
int ai[MAX_N], n, q;

ll divide(ll x, ll y) { return x > 0 ? x / y : (x - y + 1) / y; }

#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)

void pushup(int p)
{
    nodes[p] = nodes[lson] + nodes[rson];
    max_val[p] = max(max_val[lson], max_val[rson]);
    min_val[p] = min(min_val[lson], min_val[rson]);
}

void build(int l, int r, int p)
{
    if (l == r)
        return (void)(nodes[p] = max_val[p] = min_val[p] = ai[l]);
    build(l, mid, lson), build(mid + 1, r, rson);
    pushup(p);
}

void pushdown(int p, int l, int r)
{
    if (tag[p] != 0)
    {
        nodes[lson] += tag[p] * (mid - l + 1), nodes[rson] += tag[p] * (r - mid);
        max_val[lson] += tag[p], max_val[rson] += tag[p];
        min_val[lson] += tag[p], min_val[rson] += tag[p];
        tag[lson] += tag[p], tag[rson] += tag[p];
        tag[p] = 0;
    }
}

void update(int ql, int qr, int l, int r, int p, int val)
{
    if (ql <= l && r <= qr)
        return (void)(nodes[p] += (r - l + 1) * val, tag[p] += val, max_val[p] += val, min_val[p] += val);
    pushdown(p, l, r);
    if (ql <= mid)
        update(ql, qr, l, mid, lson, val);
    if (mid < qr)
        update(ql, qr, mid + 1, r, rson, val);
    pushup(p);
}

void updateDiv(int ql, int qr, int l, int r, int p, ll val)
{
    if (ql <= l && r <= qr && max_val[p] - divide(max_val[p], val) == min_val[p] - divide(min_val[p], val))
    {
        ll delta = (max_val[p] - divide(max_val[p], val));
        nodes[p] -= (r - l + 1) * delta, tag[p] -= delta, max_val[p] -= delta, min_val[p] -= delta;
        return;
    }
    pushdown(p, l, r);
    if (ql <= mid)
        updateDiv(ql, qr, l, mid, lson, val);
    if (mid < qr)
        updateDiv(ql, qr, mid + 1, r, rson, val);
    pushup(p);
}

ll querySum(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr)
        return nodes[p];
    ll ret = 0;
    pushdown(p, l, r);
    if (ql <= mid)
        ret += querySum(ql, qr, l, mid, lson);
    if (mid < qr)
        ret += querySum(ql, qr, mid + 1, r, rson);
    return ret;
}

ll queryMin(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr)
        return min_val[p];
    ll ret = INF;
    pushdown(p, l, r);
    if (ql <= mid)
        ret = min(ret, queryMin(ql, qr, l, mid, lson));
    if (mid < qr)
        ret = min(ret, queryMin(ql, qr, mid + 1, r, rson));
    return ret;
}

int main()
{
    scanf("%d%d", &n, &q);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]);
    build(1, n, 1);
    while (q--)
    {
        int opt, l, r;
        ll x;
        scanf("%d%d%d", &opt, &l, &r), l++, r++;
        if (opt == 1)
            scanf("%lld", &x), update(l, r, 1, n, 1, x);
        else if (opt == 2)
            scanf("%lld", &x), updateDiv(l, r, 1, n, 1, x);
        else if (opt == 3)
            printf("%lld\n", queryMin(l, r, 1, n, 1));
        else
            printf("%lld\n", querySum(l, r, 1, n, 1));
    }
    return 0;
}
```

**UOJ228  Implementing Square Root**

```cpp
// UOJ228.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100100;
typedef long long ll;

int n, m;
ll nodes[MAX_N << 2], max_val[MAX_N << 2], min_val[MAX_N << 2], tag[MAX_N << 2];

#define lson (p << 1)
#define rson ((p << 1) | 1)
#define mid ((l + r) >> 1)

void pushup(int p)
{
    nodes[p] = nodes[lson] + nodes[rson];
    max_val[p] = max(max_val[lson], max_val[rson]);
    min_val[p] = min(min_val[lson], min_val[rson]);
}

void build(int l, int r, int p)
{
    if (l == r)
        return (void)(scanf("%lld", &nodes[p]), max_val[p] = min_val[p] = nodes[p]);
    build(l, mid, lson), build(mid + 1, r, rson);
    pushup(p);
}

void pushdown(int p, int l, int r)
{
    if (tag[p])
    {
        nodes[lson] += (mid - l + 1) * tag[p], nodes[rson] += (r - mid) * tag[p];
        tag[lson] += tag[p], tag[rson] += tag[p];
        max_val[lson] += tag[p], max_val[rson] += tag[p];
        min_val[lson] += tag[p], min_val[rson] += tag[p];
        tag[p] = 0;
    }
}

void update(int ql, int qr, int l, int r, int p, ll val)
{
    if (ql <= l && r <= qr)
        return (void)(nodes[p] += (r - l + 1) * val, max_val[p] += val, min_val[p] += val, tag[p] += val);
    pushdown(p, l, r);
    if (ql <= mid)
        update(ql, qr, l, mid, lson, val);
    if (mid < qr)
        update(ql, qr, mid + 1, r, rson, val);
    pushup(p);
}

void updateSqrt(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr && max_val[p] - (ll)sqrt(max_val[p]) == min_val[p] - (ll)sqrt(min_val[p]))
    {
        ll delta = ((ll)sqrt(max_val[p]) - max_val[p]);
        nodes[p] += (r - l + 1) * delta, max_val[p] += delta, min_val[p] += delta, tag[p] += delta;
        return;
    }
    pushdown(p, l, r);
    if (ql <= mid)
        updateSqrt(ql, qr, l, mid, lson);
    if (mid < qr)
        updateSqrt(ql, qr, mid + 1, r, rson);
    pushup(p);
}

ll query(int ql, int qr, int l, int r, int p)
{
    if (ql <= l && r <= qr)
        return nodes[p];
    pushdown(p, l, r);
    ll ret = 0;
    if (ql <= mid)
        ret += query(ql, qr, l, mid, lson);
    if (mid < qr)
        ret += query(ql, qr, mid + 1, r, rson);
    return ret;
}

int main()
{
    scanf("%d%d", &n, &m);
    build(1, n, 1);
    while (m--)
    {
        int opt, l, r, x;
        scanf("%d%d%d", &opt, &l, &r);
        if (opt == 1)
            scanf("%d", &x), update(l, r, 1, n, 1, x);
        else if (opt == 2)
            updateSqrt(l, r, 1, n, 1);
        else
            printf("%lld\n", query(l, r, 1, n, 1));
    }
    return 0;
}
```
