A15767 | Chain Chips
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given an undirected graph consisting of $n$ vertices and $n-1$ edges. The $i$ -th edge has weight $a_i$ ; it connects the vertices $i$ and $i+1$ .
Initially, each vertex contains a chip. Each chip has an integer written on it; the integer written on the chip in the $i$ -th vertex is $i$ .
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 $q$ queries of the form:
- $k$ $x$ — change the weight of the $k$ -th edge (the one which connects the vertices $k$ and $k+1$ ) to $x$ .
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.
Initially, each vertex contains a chip. Each chip has an integer written on it; the integer written on the chip in the $i$ -th vertex is $i$ .
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 $q$ queries of the form:
- $k$ $x$ — change the weight of the $k$ -th edge (the one which connects the vertices $k$ and $k+1$ ) to $x$ .
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.
输入格式
The first line contains one integer $n$ ( $2 \le n \le 2 \cdot 10^5$ ).
The second line contains $n-1$ integers $a_1, a_2, \dots, a_{n-1}$ ( $1 \le a_i \le 10^9$ ).
The third line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ).
Then $q$ lines follow. The $i$ -th of them contains two integers $k$ and $x$ ( $1 \le k \le n-1$ ; $1 \le x \le 10^9$ ) for the $i$ -th query.
The second line contains $n-1$ integers $a_1, a_2, \dots, a_{n-1}$ ( $1 \le a_i \le 10^9$ ).
The third line contains one integer $q$ ( $1 \le q \le 2 \cdot 10^5$ ).
Then $q$ lines follow. The $i$ -th of them contains two integers $k$ and $x$ ( $1 \le k \le n-1$ ; $1 \le x \le 10^9$ ) for the $i$ -th query.
输出格式
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
暂无题解
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted