CF1814E.Chain Chips

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an undirected graph consisting of nn vertices and n−1n-1 edges. The ii-th edge has weight aia_i; it connects the vertices ii and i+1i+1.

Initially, each vertex contains a chip. Each chip has an integer written on it; the integer written on the chip in the ii-th vertex is ii.

In one operation, you can choose a chip (if there are multiple chips in a single vertex, you may choose any one of them) and move it along one of the edges of the graph. The cost of this operation is equal to the weight of the edge.

The cost of the graph is the minimum cost of a sequence of such operations that meets the following condition:

  • after all operations are performed, each vertex contains exactly one chip, and the integer on each chip is not equal to the index of the vertex where that chip is located.

You are given qq queries of the form:

  • kk xx — change the weight of the kk-th edge (the one which connects the vertices kk and k+1k+1) to xx.

After each query, print the cost of the graph. Note that you don't actually move any chips; when you compute the cost, the chips are on their initial positions.

你被给定一个包含 nn 个顶点和 n−1n-1 条边的无向图。第 ii 条边的权重为 aia_i,它连接顶点 ii 和 i+1i+1。

初始时,每个顶点上都放置着一枚棋子。每枚棋子上写有一个整数;第 ii 个顶点上的棋子所写的整数为 ii。

在一次操作中,你可以选择一枚棋子(若某个顶点上有多个棋子,可任选其中一枚),并将其沿图中的一条边移动。该操作的代价等于该边的权重。

图的代价定义为:满足以下条件的操作序列的最小总代价:

  • 所有操作执行完毕后,每个顶点上恰好有一枚棋子,且每枚棋子上的整数不等于该棋子所在顶点的编号。

你将收到 qq 个形如以下格式的查询:

  • kk xx — 将第 kk 条边(即连接顶点 kk 和 k+1k+1 的边)的权重修改为 xx。

对每次查询,输出当前图的代价。注意:你实际上并不移动任何棋子;在计算代价时,所有棋子均处于其初始位置。

输入格式

The first line contains one integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5).

The second line contains n−1n-1 integers a1,a2,…,an−1a_1, a_2, \dots, a_{n-1} (1≤ai≤1091 \le a_i \le 10^9).

The third line contains one integer qq (1≤q≤2⋅1051 \le q \le 2 \cdot 10^5).

Then qq lines follow. The ii-th of them contains two integers kk and xx (1≤k≤n−11 \le k \le n-1; 1≤x≤1091 \le x \le 10^9) for the ii-th query.

第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)。

第二行包含 n−1n-1 个整数 a1,a2,…,an−1a_1, a_2, \dots, a_{n-1}(1≤ai≤1091 \le a_i \le 10^9)。

第三行包含一个整数 qq(1≤q≤2⋅1051 \le q \le 2 \cdot 10^5)。

接下来是 qq 行。其中第 ii 行包含两个整数 kk 和 xx(1≤k≤n−11 \le k \le n-1;1≤x≤1091 \le x \le 10^9),表示第 ii 个查询。

输出格式

For each query, print one integer — the cost of the graph after the query is performed.

对于每个查询,输出一个整数——执行该查询后图的代价。

输入输出样例

  • 输入#1

    10
    12 6 12 15 20 8 17 12 15
    8
    4 10
    7 3
    6 14
    9 9
    2 10
    3 5
    4 11
    7 11

    输出#1

    126
    108
    120
    108
    112
    98
    98
    114

输入解题思路,AI测评打分。不知道怎么写?

首页