---
id: 2454
lang: en
translation_of: index.md
source_sha256: 2410871c1c6ee6538576ccc5939cf36e092b85f325c1b299deced994c4146df5
title: "P4559: [JSOI2018] Lining Up — Solution"
description: "Greedily, the i-th smallest goes to position K + i - 1; a persistent segment tree splits the absolute-value contribution into two contiguous parts."
tags: ["OI","Segment Tree","Persistent Segment Tree"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 列队 translated literally as “Lining Up”, matching an earlier translated post, rather than taken from any official English statement."
  - "想了一个假的 rendered as “came up with a bogus one”; 假 (fake) is the usual OI word for a wrong solution, translated without a chip."
---

## Main Idea

At first I came up with a bogus one, and only after I finished writing it did I realize where it went wrong, which was pretty <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span>.

First, greedily, we know that the $i$-th smallest number must end up at position $K + i - 1$. When computing the answer, we find it is nothing more than splitting the contribution according to how the absolute value breaks apart and handling the parts separately: one part is $(a_i - rk_i) - (k - 1)$, and the other is $(k - 1) - (a_i - rk_i)$. So we need a data structure to split these two parts.

First, we know these two parts must be two contiguous segments, because the Delta from one element to the next is always at most $1$. Knowing this property, we can actually try using a persistent segment tree to handle the segments within the range. Borrowing the routine of the potential-based segment tree, we can keep pushing a segment down until it is complete.

For a complete segment, it turns out we just wrap the contribution formula above in a $\sum$, and when wrapping it in the $\sum$ we need to record the left endpoint of this segment's sum, so that we can handle it directly with an arithmetic series.

<!--more-->

## Code

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

using namespace std;

const int MAX_N = 1e6 + 200, RANGE = 1e6;

typedef long long ll;

int n, m, ai[MAX_N], roots[MAX_N], ptot;

struct node
{
    int val, lson, rson;
    ll sum;
} nodes[MAX_N * 30];

#define mid ((l + r) >> 1)

int update(int qx, int l, int r, int pre)
{
    int p = ++ptot;
    nodes[p] = nodes[pre], nodes[p].val++, nodes[p].sum += qx;
    if (l == r)
        return p;
    if (qx <= mid)
        nodes[p].lson = update(qx, l, mid, nodes[pre].lson);
    else
        nodes[p].rson = update(qx, mid + 1, r, nodes[pre].rson);
    return p;
}

ll query(int l, int r, int p, int pre, int cur, int k)
{
    if (nodes[p].val - nodes[pre].val == 0)
        return 0;
    int siz = nodes[p].val - nodes[pre].val;
    ll sum = nodes[p].sum - nodes[pre].sum;
    if (k + cur <= l)
        return sum - 1LL * ((k + cur) + (k + cur + siz - 1)) * siz / 2;
    else if (r <= k + cur + siz - 1)
        return 1LL * ((k + cur) + (k + cur + siz - 1)) * siz / 2 - sum;
    int lsiz = nodes[nodes[p].lson].val - nodes[nodes[pre].lson].val;
    return query(l, mid, nodes[p].lson, nodes[pre].lson, cur, k) +
           query(mid + 1, r, nodes[p].rson, nodes[pre].rson, cur + lsiz, k);
}

#undef mid

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), roots[i] = update(ai[i], 1, RANGE, roots[i - 1]);
    while (m--)
    {
        int L, R, k;
        scanf("%d%d%d", &L, &R, &k);
        printf("%lld\n", query(1, RANGE, roots[R], roots[L - 1], 0, k));
    }
    return 0;
}
```
