#P17210. 【模板】半在线决策单调性

    ID: 19737 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>分治决策单调性模板题

【模板】半在线决策单调性

Problem Description

You are given a sequence a1⋯ana_1 \cdots a_n of length nn and a sequence b1⋯bnb_1 \cdots b_n.

Define the weight w(l,r)w(l,r) of an interval [l,r][l,r] as

$$b_r+\sum\limits_{i=l}^r\sum\limits_{j=i+1}^r[a_i=a_j]$$

where [cond]=1[\text{cond}] = 1 if and only if cond\text{cond} is true, otherwise =0= 0.

For each p=1,2,⋯np = 1,2,\cdots n, you need to partition 1⋯p1 \cdots p into several segments so that the sum of the weights of all segments is minimized. Formally, you need to find some indices x0⋯xkx_0 \cdots x_k such that:

  • x1=0,xk=px_1 = 0, x_k = p.
  • ∀i=0,1,⋯k−1,xi<xi+1\forall i = 0,1,\cdots k-1, x_i < x_{i+1}.

Based on this, minimize ∑i=1kw(xi−1+1,xi)\sum\limits_{i=1}^{k} w(x_{i-1}+1,x_i).

Input Format

The first line contains an integer nn, representing the length of the sequences.

The second line contains nn integers describing the sequence a1⋯ana_1 \cdots a_n of length nn.

The third line contains nn integers describing the sequence b1⋯bnb_1 \cdots b_n of length nn.

Output Format

Output one line with nn integers, where the ii-th integer is the answer for the prefix 1⋯i1 \cdots i.

6
1 2 1 1 2 1
2 2 2 2 2 2

2 2 3 5 5 6

Hint

Constraints

  • For 20%20\% of the testdata, n≤5000n \leq 5000.
  • For 50%50\% of the testdata, n≤105n \leq 10^5.
  • For 100%100\% of the testdata, $n \leq 5\times 10^5, 1 \leq a_i \leq n, 0 \leq b_i \leq 10^9$.

Translated by ChatGPT 5