2026 杭电春季联赛 4——1007-喂马(ODT 配合) · znzryb
## 题目大意
给定多组数据。每组有 `n` 匹马按编号 `1..n` 排列,`m` 个饲料槽编号 `1..m`。
初始时第 `i` 匹马分配到槽 `a_i`,其中 `a_i=0` 表示未分配。每个槽的投喂总量 `b_i` 初始都是 `0`。
需要按顺序执行 `q` 次操作,每次输入 `opt, l, r, x`:
- `opt=1`:把区间 `[l,r]` 内所有马的分配槽改为 `x`。
- `opt=2`:对区间 `[l,r]` 内每匹马,如果它当前已分配槽(`a_i≠0`),就给它所在槽增加 `x` 的投喂量(累加到对应 `b_{a_i}`)。
所有操作结束后,输出 `m` 个数,表示每个槽最终投喂总量 `b_1..b_m`。
```
Sample Input
1
6 4 5
1 0 3 2 4 1
2 1 4 3
1 2 5 1
2 1 6 2
1 3 4 4
2 2 5 4
Sample Output
23 3 3 8
```
样例过程对应含义:
- 初始分配:`[1,0,3,2,4,1]`,所有 `b_i=0`。
- 操作 `2 1 4 3`:马 `1..4` 中已分配的有 `1,3,4` 号马,分别给槽 `1,3,2` 各加 `3`。
- 操作 `1 2 5 1`:马 `2..5` 全部分配到槽 `1`。
- 操作 `2 1 6 2`:马 `1..6` 都已分配,按当前分配给对应槽加 `2`。
- 操作 `1 3 4 4`:马 `3..4` 改分配到槽 `4`。
- 操作 `2 2 5 4`:马 `2..5` 都已分配,按当前分配给对应槽加 `4`。
最终各槽投喂总量为:`23 3 3 8`。
## 思路讲解
[2025 河南省赛——Problem C. Toxel 与宝可梦图鉴(珂朵莉树+值域线段树维护全局信息)(因为这道题目,可以设计珂朵莉树无这个查询操作,只有 assign 操作,因此严格 O(nlogn))(区间等差数列赋值,全局查询最小众数)](/2026/03/23/20260323-99c49c97)
::: AI 生成的 latex 题解代码
```latex
\documentclass{article}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{xeCJK}
\usepackage{indentfirst}
\setlength{\parindent}{2em}
\usepackage{multicol}
\usepackage{tocloft}
\usepackage{xcolor}
\usepackage[cachedir=_minted-main]{minted}
\usepackage{pifont}
\newcommand{\opone}{\ding{172}}
\newcommand{\optwo}{\ding{173}}
\usepackage[a4paper, left=2cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, decorations.pathreplacing}
\definecolor{colorA}{RGB}{220,80,60}
\definecolor{colorB}{RGB}{50,120,200}
\definecolor{colorC}{RGB}{50,160,80}
\definecolor{colorZero}{RGB}{150,150,150}
\setminted{
breaklines=true,
breakanywhere=true,
fontsize=\small,
frame=single,
bgcolor=gray!5,
tabsize=2,
}
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}}
\usepackage[colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, filecolor=black, menucolor=black, runcolor=black, urlcolor=blue]{hyperref}
\title{1007 喂马 —— 题解(AI 辅助版)}
\author{znzryb}
\date{\today}
\begin{document}
\maketitle
\section*{1007 喂马}
\textbf{Time Limit:} 20000 / 10000 MS (Java / Others)\par
\textbf{Memory Limit:} 524288 / 524288 K (Java / Others)
\subsection*{Problem Description}
2026 丙午马年,某马术俱乐部有 $n$ 匹马排成一排,编号 $1, 2, \ldots, n$。俱乐部设有 $m$ 个饲料槽,编号 $1, 2, \ldots, m$。初始时,第 $i$ 匹马被分配到第 $a_i$ 号饲料槽($a_i=0$ 表示该马暂未分配)。每个饲料槽有一个投喂量计数 $b_i$,初始均为 $0$。
你需要依次执行 $q$ 次操作,操作有两种类型:
\begin{enumerate}
\item[\opone{}] 给定 $l, r, x$,将编号在 $[l, r]$ 内的所有马重新分配到第 $x$ 号饲料槽。
\item[\optwo{}] 给定 $l, r, x$,对编号在 $[l, r]$ 内的每匹马,若该马已分配饲料槽(即 $a_i \neq 0$),则向其饲料槽投喂 $x$ 单位饲料(即 $b_{a_i}$ 加上 $x$)。
\end{enumerate}
请输出所有操作执行完毕后,每个饲料槽的总投喂量。
\subsection*{数据范围}
$1 \le T \le 5$,$1 \le n, m, q \le 2 \times 10^6$,$0 \le a_i \le m$,$opt \in \{1, 2\}$,$1 \le l \le r \le n$,$1 \le x \le m$。所有测试数据的 $n$ 与 $q$ 之和均不超过 $2 \times 10^6$。
\section{核心思路:把投喂量先记在马身上}
注意到操作 \optwo{} 里,真正影响答案的,是"这匹马当前属于哪个槽"。如果一匹马在处理操作 \optwo{} 时属于槽 $c$,那这次投喂的 $x$ 就加到 \texttt{b[c]} 上。
一个关键的观察:\textbf{操作 \optwo{} 本身不需要知道颜色},它只是在 $[l, r]$ 上加一个值 $x$——不管这段里的马属于哪个槽。我们可以先把这些"投喂"全部记在马的编号上,开一个 BIT(支持区间加、区间查询),操作 \optwo{} 直接对 $[l,r]$ 区间加 $x$,BIT 里 \texttt{S[i]} 的含义就是"马 $i$ 被操作过的所有投喂量之和"。
\begin{minted}{cpp}
// 操作\optwo{} 直接变成一次 BIT 区间加,O(log n),完全不碰 ODT
if (opt == 2) bit.add(l, r, x);
\end{minted}
剩下的问题是:这些积在马身上的投喂量,怎么最终归到正确的槽?
\section{在换槽的瞬间结账}
马的槽会被操作 \opone{} 改变。想清楚下面这件事:一匹马 $i$ 一开始属于槽 $c_{old}$,然后某次操作 \opone{} 把它改成槽 $c_{new}$。在这次改变发生之前,所有打到马 $i$ 上的投喂量,都应该归给 $c_{old}$。
所以在执行操作 \opone{} 的那一刻,对于每一段被覆盖的旧区间 $[L, R]$(旧颜色 $c$),把 BIT 里 $[L,R]$ 的区间和查出来,直接加到 \texttt{b[c]} 上就行了:
\begin{minted}{cpp}
// 在 assign 里遍历每一个将被推平的旧段 [L, R, c]
ull V = bit.query(L, R); // 这段马身上目前积累的总投喂量
if (c != 0) b[c] += V; // 结账给旧槽
\end{minted}
但光加还不够——这段马接下来要属于新槽 $c_{new}$。假设之后又有操作 \optwo{} 打到它们身上,那些投喂量不应该再算到旧槽里去了。所以还要把"当前已积累的基准量"从新槽里提前减掉:
\begin{minted}{cpp}
if (x != 0) b[x] -= V; // 让新槽从"零基准"开始计,而不是从当前 V 开始
\end{minted}
这样做之后,将来再查 $[L,R]$ 的区间和时,那个值只包含"进入新槽 $x$ 之后新增的投喂量",正好是应该归给槽 $x$ 的部分。这就是整个方案的核心:\textbf{先减后加,差分结账}。
\section{最后一次结账}
所有操作结束后,ODT 里剩下的每个段 $[L, R, c]$,它们在最后一次换颜色之后可能又被投喂了若干次,这些投喂量还留在 BIT 里没有结算。遍历一遍 ODT,对每个非零颜色的段,把区间和加到对应的 \texttt{b[c]} 上:
\begin{minted}{cpp}
for (auto &nd : odt) {
if (nd.c != 0) b[nd.c] += bit.query(nd.l, nd.r);
}
\end{minted}
\section{为什么这样做 ODT 不会炸}
操作 \optwo{} 根本不碰 ODT,只动 BIT,每次 $O(\log n)$。ODT 只在操作 \opone{} 里被动,而每次操作 \opone{} 在推平时会删掉旧段、插入新段。ODT 的快就是因为"被删的段不会再回来"——整个生命周期里最多插入 $n + q$ 个段,所有操作 \opone{} 加在一起最多删 $n + q$ 次,每次删的时候顺便做一次 BIT 区间查询。总代价完全可控。
\section{处理 \texttt{a[i] = 0} 的马}
颜色为 $0$ 代表未分配。在 assign 遍历时:
\begin{minted}{cpp}
if (c != 0) b[c] += V; // c == 0 时直接跳过,不给任何槽结账
if (x != 0) b[x] -= V; // x == 0 时也跳过
\end{minted}
颜色为 $0$ 的段在换出去时不结账、换进来时也不做减操作。于是这段马在"属于槽 0"期间积累的投喂量就被彻底丢弃,符合题意。
\section{溢出问题}
$x$ 最大 $2 \times 10^6$,区间最大 $2 \times 10^6$,操作最多 $2 \times 10^6$ 次,最大总量接近 \texttt{long long} 的上限。而差分结账时 \texttt{b[x] -= V} 会让 \texttt{b[x]} 暂时变成负数(在 \texttt{long long} 里意味着有溢出风险)。
改用 \texttt{unsigned long long} 就没这个问题——所有加减在模 $2^{64}$ 的环里转,最终结果只要是正确的正数就能正常打印。
\section{完整代码}
\begin{minted}{cpp}
#include <iostream>
#include <vector>
#include <set>
using namespace std;
typedef unsigned long long ull;
const int MAXN = 2000005;
// BIT:支持区间加、区间查询
// 维护两个差分数组 d1[i] 和 d2[i] = i*d1[i]
// 前缀和 = (p+1)*sum(d1, p) - sum(d2, p)
ull bit1[MAXN], bit2[MAXN];
ull b[MAXN];
int a[MAXN];
int n, m, q;
void bit_add(int idx, ull v1, ull v2) {
for (; idx <= n; idx += idx & -idx) {
bit1[idx] += v1;
bit2[idx] += v2;
}
}
void range_add(int l, int r, ull val) {
bit_add(l, val, (ull)l * val);
bit_add(r + 1, 0ULL - val, 0ULL - (ull)(r + 1) * val);
}
ull prefix_sum(int idx) {
ull s1 = 0, s2 = 0, k = idx;
for (; idx > 0; idx -= idx & -idx) { s1 += bit1[idx]; s2 += bit2[idx]; }
return (k + 1) * s1 - s2;
}
ull range_query(int l, int r) {
return prefix_sum(r) - prefix_sum(l - 1);
}
// ODT 节点:[l, r] 内所有马属于槽 c
struct Node {
int l, r;
mutable int c;
Node(int l_, int r_ = 0, int c_ = 0) : l(l_), r(r_), c(c_) {}
bool operator<(const Node& o) const { return l < o.l; }
};
set<Node> odt;
auto split(int x) {
if (x > n) return odt.end();
auto it = --odt.upper_bound(Node(x));
if (it->l == x) return it;
int l = it->l, r = it->r, c = it->c;
odt.erase(it);
odt.insert(Node(l, x - 1, c));
return odt.insert(Node(x, r, c)).first;
}
void assign_color(int l, int r, int x) {
auto itr = split(r + 1);
auto itl = split(l);
for (auto it = itl; it != itr; ++it) {
int L = it->l, R = it->r, c = it->c;
ull V = range_query(L, R);
if (c != 0) b[c] += V; // 旧槽结账
if (x != 0) b[x] -= V; // 新槽扣除已积累的基准量
}
odt.erase(itl, itr);
odt.insert(Node(l, r, x));
}
// 快读快写
namespace FastIO {
const int BUFSZ = 1 << 20;
char ibuf[BUFSZ], obuf[BUFSZ];
char *ip1 = ibuf, *ip2 = ibuf;
int opos = 0;
inline char gc() {
if (ip1 == ip2) {
ip2 = (ip1 = ibuf) + fread(ibuf, 1, BUFSZ, stdin);
if (ip1 == ip2) return EOF;
}
return *ip1++;
}
inline void pc(char c) {
if (opos == BUFSZ) { fwrite(obuf, 1, BUFSZ, stdout); opos = 0; }
obuf[opos++] = c;
}
template<typename T> inline void rd(T &x) {
x = 0; char c = gc();
while (c < '0' || c > '9') { if (c == EOF) return; c = gc(); }
while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = gc(); }
}
template<typename T> inline void wr(T x) {
if (x == 0) { pc('0'); return; }
static char st[24]; int top = 0;
while (x) { st[top++] = x % 10 + '0'; x /= 10; }
while (top--) pc(st[top]);
}
inline void flush() { if (opos) { fwrite(obuf, 1, opos, stdout); opos = 0; } }
}
using namespace FastIO;
void solve() {
rd(n); rd(m); rd(q);
for (int i = 0; i <= n + 1; ++i) bit1[i] = bit2[i] = 0;
for (int i = 0; i <= m + 1; ++i) b[i] = 0;
odt.clear();
for (int i = 1; i <= n; ++i) rd(a[i]);
// 初始 ODT:相邻同色的马合并成一段
for (int i = 1; i <= n; ) {
int j = i;
while (j <= n && a[j] == a[i]) ++j;
odt.insert(Node(i, j - 1, a[i]));
i = j;
}
for (int i = 0; i < q; ++i) {
int opt, l, r; ull x;
rd(opt); rd(l); rd(r); rd(x);
if (opt == 1) assign_color(l, r, (int)x);
else range_add(l, r, x); // 操作\optwo{}:只动 BIT,不碰 ODT
}
// 最终结算:ODT 里每个残留段的积累量归给对应槽
for (auto &nd : odt)
if (nd.c != 0) b[nd.c] += range_query(nd.l, nd.r);
for (int i = 1; i <= m; ++i) {
wr(b[i]);
pc(i == m ? '\n' : ' ');
}
}
int main() {
int T; rd(T);
while (T--) solve();
flush();
return 0;
}
\end{minted}
\section{把样例跟着走一遍}
初始:\texttt{a = [1, 0, 3, 2, 4, 1]},ODT 各段:\texttt{[1,1,1] [2,2,0] [3,3,3] [4,4,2] [5,5,4] [6,6,1]},\texttt{S = [0,0,0,0,0,0]},\texttt{b = [0,0,0,0]}。
\begin{enumerate}
\item \textbf{操作 2 1 4 3}:BIT 在 $[1,4]$ 加 3,\texttt{S = [3,3,3,3,0,0]}。
\item \textbf{操作 1 2 5 1}:把 $[2,5]$ 推成颜色 1。遍历旧段结账:
\begin{itemize}
\item \texttt{[2,2,0]},$V=3$,\texttt{c=0} 跳过;\texttt{b[1] -= 3}。
\item \texttt{[3,3,3]},$V=3$,\texttt{b[3] += 3};\texttt{b[1] -= 3}。
\item \texttt{[4,4,2]},$V=3$,\texttt{b[2] += 3};\texttt{b[1] -= 3}。
\item \texttt{[5,5,4]},$V=0$,无影响。
\end{itemize}
此时 \texttt{b = [-9, 3, 3, 0]}(\texttt{ull} 环里),ODT 变为 \texttt{[1,1,1] [2,5,1] [6,6,1]}。
\item \textbf{操作 2 1 6 2}:BIT 在 $[1,6]$ 加 2,\texttt{S = [5,5,5,5,2,2]}。
\item \textbf{操作 1 3 4 4}:把 $[3,4]$ 推成颜色 4。split 出 \texttt{[3,4,1]},$V=10$,\texttt{b[1] += 10},\texttt{b[4] -= 10}。\texttt{b = [1, 3, 3, -10]},ODT 变为 \texttt{[1,1,1] [2,2,1] [3,4,4] [5,5,1] [6,6,1]}。
\item \textbf{操作 2 2 5 4}:BIT 在 $[2,5]$ 加 4,\texttt{S = [5,9,9,9,6,2]}。
\item \textbf{最终结算}:
\begin{itemize}
\item \texttt{[1,1,1]}:\texttt{b[1] += 5}。
\item \texttt{[2,2,1]}:\texttt{b[1] += 9}。
\item \texttt{[3,4,4]}:\texttt{b[4] += 18}。
\item \texttt{[5,5,1]}:\texttt{b[1] += 6}。
\item \texttt{[6,6,1]}:\texttt{b[1] += 2}。
\end{itemize}
\texttt{b[1] = 1+5+9+6+2 = 23},\texttt{b[2] = 3},\texttt{b[3] = 3},\texttt{b[4] = -10+18 = 8}。
\end{enumerate}
输出 \texttt{23 3 3 8},与样例答案一致。
\end{document}
```
:::
[PDF](https://media.znzryb.com/20260419-7ed1f949/1007MyLatexWithAI_6d24ed3e.pdf)
::: 我的简略 latex 代码
```latex
\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{amsmath} % bmatrix、cases、aligned 等数学环境
\usepackage{amssymb} % 数学符号宏包,支持 \mathbb 等
\usepackage{xeCJK} % 核心:添加这个宏包以支持中文
\usepackage{indentfirst} % 支持首段缩进
\setlength{\parindent}{2em} % 设置缩进为两个中文字符宽度
\usepackage{multicol} % 引入分栏宏包
\usepackage{tocloft} % 支持目录引导点
\usepackage{xcolor} % 用于设置颜色;须先于 minted 以便 bgcolor 等生效
\usepackage[cachedir=_minted-main]{minted} % Pygments 高亮,需 -shell-escape + pygmentize
\usepackage[a4paper, left=2cm, right=2cm, top=2.5cm, bottom=2.5cm]{geometry}
% 与原先 listings 外观大致对齐;
\setminted{
% 风格可用莫兰迪系
% style=stata-light,
% 允许长行换行
breaklines=true,
% 允许在任意位置换行,防止长 URL 戳出去
breakanywhere=true,
fontsize=\small,
frame=single,
bgcolor=gray!5,
% -8bit 以后,xelatex 才对制表符有反应
tabsize=2,
}
% --- 目录样式修改 ---
\renewcommand{\cftsecleader}{\cftdotfill{\cftdotsep}} % 让 section 也有点点
% ------------------
\usepackage[colorlinks=true, linkcolor=black, anchorcolor=black, citecolor=black, filecolor=black, menucolor=black, runcolor=black, urlcolor=blue]{hyperref} % 添加超链接和PDF书签支持
\title{}
\author{znzryb}
\date{\today}
\begin{document}
\maketitle
\section*{1007 喂马}
\textbf{Time Limit:} 20000 / 10000 MS (Java / Others)
\textbf{Memory Limit:} 524288 / 524288 K (Java / Others)
\subsection*{Problem Description}
2026 丙午马年,某马术俱乐部有 $n$ 匹马排成一排,编号 $1, 2, \ldots, n$。俱乐部设有 $m$ 个饲料槽,编号 $1, 2, \ldots, m$。初始时,第 $i$ 匹马被分配到第 $a_i$ 号饲料槽($a_i=0$ 表示该马暂未分配)。每个饲料槽有一个投喂量计数 $b_i$,初始均为 $0$。
你需要依次执行 $q$ 次操作,操作有两种类型:
\begin{enumerate}
\item[①] 给定 $l, r, x$,将编号在 $[l, r]$ 内的所有马重新分配到第 $x$ 号饲料槽。
\item[②] 给定 $l, r, x$,对编号在 $[l, r]$ 内的每匹马,若该马已分配饲料槽(即 $a_i \neq 0$),则向其饲料槽投喂 $x$ 单位饲料(即 $b_{a_i}$ 加上 $x$)。
\end{enumerate}
请输出所有操作执行完毕后,每个饲料槽的总投喂量。
\subsection*{Input}
本题有多组测试数据。第一行输入 $T\ (1 \le T \le 5)$,表示数据组数。
对于每组数据:
第一行输入三个正整数 $n, m, q\ (1 \le n, m, q \le 2 \times 10^6)$。
第二行输入 $n$ 个非负整数 $a_1, a_2, \ldots, a_n\ (0 \le a_i \le m)$。
接下来 $q$ 行,每行输入四个正整数 $opt, l, r, x\ (opt \in \{1, 2\}, 1 \le l \le r \le n, 1 \le x \le m)$,意义如题目描述所述。
保证所有数据的 $n$ 的和不超过 $2 \times 10^6$,$q$ 的和不超过 $2 \times 10^6$。
\subsection*{Output}
对于每组数据,输出一行 $m$ 个整数,第 $i$ 个整数表示第 $i$ 个饲料槽的总投喂量 $b_i$,相邻整数用空格隔开。
\subsection*{Sample Input}
\begin{minted}{text}
1
6 4 5
1 0 3 2 4 1
2 1 4 3
1 2 5 1
2 1 6 2
1 3 4 4
2 2 5 4
\end{minted}
\subsection*{Sample Output}
\begin{minted}{text}
23 3 3 8
\end{minted}
\section{明确题意以及想法}
首先,我们需要明确题意。我们需要查询的是在这个 \texttt{[l,r]} 区间内,有几个马是属于 \texttt{house1} 的,有几个马是属于 \texttt{house2} 的...?
而且,我们发现,这个东西,他不是一个全局查询,而是一个区间查询。\href{https://codeforces.com/gym/105941/problem/C}{2025 河南省赛——Problem C. Toxel 与宝可梦图鉴} 这个和这道题目还不大一样,这道题目是一个这个全局查询,只需要用一个树状数组维护全局信息即可。
这道题目还有一个这个赋值操作,用 ODT 维护即可,但是问题就在于,查询你不能用 ODT 去查,用 ODT 查,时间复杂度就炸了。
不过其实这道题目有一个显而易见的思路,就是我们在换槽的时候,再搞事情。因为说白了,这个查询连离线都算不上,最后的结果也是多个查询结果放在一起的,其实根本甚至都不需要将每次查询区分出来。
\section{进一步的这个思考(可不可能变为在线查询?)}
呃,okay 啊,我发现,其实想把这道题目改变为在线是很难的,因为在线查询,就是每次查询,都回答给 a 马厩喂了几个,给 c 马厩喂了几个,等等等,这个查询本身就是,你不考虑他是怎么查的,光是输出这个东西本身,复杂度就已经不大对了,所以说类似的,这种比较难维护的这个问题,都不大可能说改为这个在线查询。
\end{document}
```
:::
[PDF](https://media.znzryb.com/20260419-7ed1f949/1007MyLatexNoAI_cc405adc.pdf)
这个下面的这个**避免树状数组清空**的技巧还是非常有用的。
```cpp
void assign(ll la, ll ra, ll val) {
split(la);
split(ra + 1);
auto it = mpODT.lower_bound(la);
#ifdef LOCAL
assert(it->first==la);
#endif
for (; it != mpODT.end();) {
if (it->first > ra) {
break;
}
ll originV = it->second.val;
ll l = it->first, r = it->second.r;
// 这里是做一些事情,比如说累加答案
ll add = tr->query(l, r);
anss[originV] += add;
anss[val] -= add;
it = mpODT.erase(it);
}
mpODT[la] = {ra, val};
}
```

## AC代码
AC
[https://acm.hdu.edu.cn/contest/view-code?cid=1201&rid=9803](https://acm.hdu.edu.cn/contest/view-code?cid=1201&rid=9803)
::: 源代码
```cpp
// teamname: Gospel_rock
/**
* Problem: 喂马
* Contest:
* Judge: HDOJ
* URL: https://acm.hdu.edu.cn/contest/problem?cid=1201&pid=1007
* Created: 2026-04-17 20:40:26
* Author: Gospel_rock
* My blog: https://znzryb.com/
*
* Powered by AutoCp https://github.com/Pushpavel/AutoCp
*/
#include <bits/stdc++.h>
#define all(vec) vec.begin(),vec.end()
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
#define SZ(a) ((long long) a.size())
#define fsp(x) fixed<<setprecision(x)
using namespace std;
#if 1 && defined(LOCAL)
// @formatter:off
namespace DBG {
template<typename T>
void debug(T x) { cerr << x; } void debug(bool x) { cerr << (x ? "T" : "F"); } void debug(string x) { cerr << '"' << x << '"'; }
template<typename T> void debug(vector<T> v) { constexpr bool nested = requires(T t) { t.begin(); t.end(); }; int n = v.size(); cerr << "["; for (int i = 0; i < n; i++) { if constexpr (nested) { if (i) cerr << ","; cerr << "\n "; } else { if (i) cerr << ", "; } debug(v[i]); } if constexpr (nested) { if (n) cerr << "\n"; } cerr << "]"; }
template<typename T> void debug(set<T> s) { cerr << "{"; int f = 0; for (auto x: s) { if (f++) cerr << ", "; debug(x); } cerr << "}"; }
template<typename T, typename U> void debug(pair<T, U> p) { cerr << "("; debug(p.first); cerr << ", "; debug(p.second); cerr << ")"; }
template<typename T, typename U> void debug(map<T, U> m) { cerr << "{"; int f = 0; for (auto &[k, v]: m) { if (f++) cerr << ", "; debug(k); cerr << ": "; debug(v); } cerr << "}"; }
void _dbg() { cerr << endl; }
template<typename T, typename... A> void _dbg(T x, A... a) { debug(x); if (sizeof...(a)) cerr << ", "; _dbg(a...); }
}
// @formatter:on
#define dbg(x...) cerr << "[" << setw(7) << #x << "] = ", DBG::_dbg(x)
#define cend cerr<<"\n-------------------------\n"
#define cEnd cerr<<"\n*************************\n"
#else
#define dbg(x...) 11
#define cend 45
#define cEnd 14
#endif
using ll = long long;
using ull = unsigned long long;
using DB = double;
using i128 = __int128;
using CD = complex<double>;
static constexpr ll MAXN = (ll) 1e6 + 10, INF = (1ll << 61) - 1;
static constexpr ll mod = 998244353; // (ll)1e9+7;
static constexpr double eps = 1e-8;
const double PI = acos(-1.0);
ll lT, testcase;
/*
*
*/
/* 2026-04-19 luogu 线段树模板题
* AC https://www.luogu.com.cn/record/274702532
* 进一步添加了 public,private 规定了接口的可见性
*/
class BIT {
// 支持区间修改,区间查询
// 维护两个差分:d[i] 和 i*d[i]
// 前缀和 sum(p) = (p+1)*sum(d[1..p])
// - sum(i*d[i], 1..p)
vector<ll> tr1, tr2;
int n;
ll lowbit(ll x) {
return x & (-x);
}
public:
BIT(int n_) {
n = n_;
// 这个 n+1 就够了,虽然涉及到这个差分
tr1.assign(n + 2, 0);
tr2.assign(n + 2, 0);
}
private:
void add(vector<ll> &tr, ll p, ll x) {
// 防止 0 死循环
if (p <= 0) return;
while (p <= n) {
tr[p] += x;
p += lowbit(p);
}
}
ll sum(vector<ll> &tr, ll p) {
ll ret = 0;
while (p) {
ret += tr[p];
p -= lowbit(p);
}
return ret;
}
// 单点加
void add(ll p, ll x) {
add(tr1, p, x);
add(tr2, p, p * x);
}
public:
// 区间加,[l, r] 每个位置加 x
void add(ll l, ll r, ll x) {
add(tr1, l, x);
add(tr1, r + 1, -x);
add(tr2, l, l * x);
add(tr2, r + 1, -(r + 1) * x);
}
private:
// 前缀和 a[1] + a[2] + ... + a[p]
ll query(ll p) {
return (p + 1) * sum(tr1, p) - sum(tr2, p);
}
public:
// 区间和 a[l] + ... + a[r]
ll query(ll l, ll r) {
return query(r) - query(l - 1);
}
};
// 来自wida github仓库 https://github.com/hh2048/XCPC/tree/main 加了一些新功能
//46 ms https://acm.hdu.edu.cn/contest/view-code?cid=1179&rid=14912 读入字符getc(a) 的AC记录
// AC https://vjudge.net/solution/63197697 一道测试读写的codechef题目
namespace QuickRead {
// 快读
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template<typename T>
void Cin(T &a) {
T ans = 0;
bool f = 0;
char c = getc();
for (; c < '0' || c > '9'; c = getc()) {
if (c == '-') f = 1;
}
for (; c >= '0' && c <= '9'; c = getc()) {
ans = ans * 10 + c - '0';
}
a = f ? -ans : ans;
}
void getc(char &a) {
char c = getc();
while (isspace(c)) {
c = getc();
}
a = c;
}
template<typename T, typename... Args>
void Cin(T &a, Args &... args) {
Cin(a), Cin(args...);
}
// ===== 输出 =====
static constexpr size_t OUT_CAP = 1 << 21; // 大概是2MB
char obuf[OUT_CAP];
size_t op = 0;
inline void flush() {
if (op) {
fwrite(obuf, 1, op, stdout);
op = 0;
}
}
inline void putc(char c) {
if (op == OUT_CAP) flush();
obuf[op++] = c;
}
template<typename T>
void write(T x) {
// 注意,这里输出不带换行
if (x < 0) putc('-'), x = -x;
if (x > 9) write(x / 10);
putc(x % 10 + '0');
}
struct Flusher {
// 析构时自动刷新
~Flusher() { flush(); }
} flusher;
} // namespace QuickRead
using namespace QuickRead;
struct Solve {
ll N, M, Q;
struct RVal {
ll r;
ll val;
};
map<ll, RVal> mpODT;
optional<BIT> tr;
vector<ll> anss;
void split(ll pos) {
if (mpODT.empty()) return;
auto it = mpODT.upper_bound(pos);
if (it == mpODT.begin()) return;
it--;
if (it->first == pos) {
return;
}
mpODT[pos] = {it->second.r, it->second.val};
it->second.r = pos - 1;
}
void assign(ll la, ll ra, ll val) {
split(la);
split(ra + 1);
auto it = mpODT.lower_bound(la);
#ifdef LOCAL
assert(it->first==la);
#endif
for (; it != mpODT.end();) {
if (it->first > ra) {
break;
}
ll originV = it->second.val;
ll l = it->first, r = it->second.r;
// 我们现在要把这个换掉的值给累加到答案上
ll add = tr->query(l, r);
anss[originV] += add;
anss[val] -= add;
it = mpODT.erase(it);
}
mpODT[la] = {ra, val};
}
vector<ll> A;
Solve() {
// cin >> N >> M >> Q;
Cin(N, M, Q);
A.resize(N + 2);
tr.emplace(N);
anss.resize(M + 2);
for (int i = 1; i <= N; ++i) {
// cin >> A[i];
Cin(A[i]);
mpODT[i] = {i, A[i]};
}
for (int i = 1; i <= Q; ++i) {
ll opt, l, r, x;
// cin >> opt >> l >> r >> x;
Cin(opt, l, r, x);
if (opt == 1) {
assign(l, r, x);
} else {
tr->add(l, r, x);
}
}
for (auto it = mpODT.begin(); it != mpODT.end(); it++) {
ll originV = it->second.val;
ll l = it->first, r = it->second.r;
// 我们现在要把这个换掉的值给累加到答案上
ll add = tr->query(l, r);
anss[originV] += add;
}
for (int i = 1; i <= M; ++i) {
// cout << anss[i] << " ";
write(anss[i]);
putc(' ');
}
// cout << "\n";
putc('\n');
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef LOCAL
cout.setf(ios::unitbuf); // 无缓冲流,方便我们调试
#endif
// cin >> lT;
Cin(lT);
for (testcase = 1; testcase <= lT; ++testcase)
Solve solve;
return 0;
}
/*
AC
https://acm.hdu.edu.cn/contest/view-code?cid=1201&rid=9803
*/
```
:::
## 心路历程(WA,TLE,MLE……)