Every year, the ACM/ICPC team will hold many contests, some of them are training while others are school contests.
In the year of 2017, there are n contests to be held, and at the beginning of year, we plans the time of each contest.
However, as things are changing. Some other events might affect the time of contest and our team leader wants to know some interesting things about the time of some events.
The first line contains an integer n. (0<n≤105)
The second line contains n integers, the i-th one of them is ti, which is the planning time of contest i at the beginning of this year. (0≤ti≤109)
The third line contains an integer q. And then q lines follows. (0<q≤105)
Then each line contains three integers. Li, Ri, Ti, means the time of Li-th contest to Ri-th contest will be changed by Ti. And then you should output the earlist time of Li-th contest to Ri-th contest in the history after the change of time. (0<Li≤Ri≤n, ∣Ti∣≤109)
Output contains q lines, i-th line is the answer after i-th query.
4
1 2 3 4
3
1 2 2
2 3 3
3 4 -10
1
2
-6
At the beginning, ti are (1,2,3,4). After the first change on time, ti becomes (3,4,3,4), and then (3,7,6,4), and at last becomes (3,7,−4,−6).
Be careful that as the times are relative times, so they can be negative.
题意:n场比赛,m次操作,每场比赛有个初值,操作每次把一个区间的比赛时间修改,每次修改完需要统计这个区间历史的时间最小值。
分析:线段树问题,区间修改和区间询问。
坑点:区间问题需要一个延迟标记 cur,如果多次对一个区间进行覆盖,延迟标记cur多次累加,而下面的节点没有随之进行更新就会出现无法更新到最小值。
比如这个测试数据:
8
0 0 0 0 0 0 0 0
4
1 2 -1
1 4 -1
1 4 1
2 2 0
最后的一组 答案是-2
处理:首先是变量,每个节点需要 有一个当前最小值m、一个历史最小值 lm,一个延迟标记累加和(和指的是延迟标记累加的得到的结果) cur,一个用来保存当cur多次累加时出现的最小和 mcur,一个延迟标记累加前的m值用 变量 sm 储存。当 rt 节点 pushdown时,需要更新 rt 节点 左右孩子节点lc和rc。更新lc节点的mcur时 ,mcur就等于min(cur(lc)+mcur(rt),mcur(lc)),然后 lm(lc) =min(mcur(lc)+sm(lc),lm(lc)),接着更新lc节点的cur和m,cur(lc)+=cur(rt),m(lc)+=cur(rt);
代码;
#include
#include
#include
#include
#include
#include
#include