版本比较
线段树进阶 [学习笔记]
正文
增加 5 行删除 5 行
280 行未改动全部展开281282283284285285286287288289149 行未改动全部展开439440441442443443444445446447292 行未改动全部展开740741742743744744745746747748789 行未改动全部展开153815391540154115421542154315441545154668 行未改动161516161617161816191619162016211622162352 行未改动280 行未改动全部展开 x>>=1; res+=minn[x]; } return res;} }```
在理解了查询的机制后,来探究探究修改的方式。标记永久化在查询时某种意义上就是**差分思想**,那么在本来就是差分的 zkw 线段树上,我们对于对应区间直接加上修改值,像建树一样层层上传即可。
149 行未改动全部展开```cppvoid update(int root,int l,int r){ //古早时期代码,格式比较混乱,故只给出核心部分 if(tree[root].maxn<=1){ //玄学(划掉)剪枝 return; } } if(tree[root].l==tree[root].r){ //暴力更新 tree[root].num=sqrt(tree[root].num); tree[root].maxn=sqrt(tree[root].maxn); return;292 行未改动全部展开
有了下传与标记,下面就是**更新操作**了。像正常线段树一样,只是特别判断历史值就行;两者代码如下,需要注意细节:
```cppvoid update(int root,int l,int r,int k){ //区间加 void update(int root,int l,int r,int k){ //区间加 if(l<=tree[root].l&&tree[root].r<=r){ tree[root].sum+=1ll*(tree[root].r-tree[root].l+1)*k;//这个时候两种值一视同仁,统一更新 tree[root].maxn+=k,tree[root].his=max(tree[root].his,tree[root].maxn); if(tree[root].sec!=-inf) tree[root].sec+=k; //注意次大值的存在性789 行未改动全部展开 int lc,rc; int maxn,pos; //最大值,最大值下标}tree[N*128],em;int add(){ return (top?sta[top--]:++tot); } //新增节点,注意运算顺序void del(int p){ sta[++top]=p,tree[p]=em; } //清空并压入栈 void del(int p){ sta[++top]=p,tree[p]=em; } //清空并压入栈void pushup(int root){ if(tree[tree[root].lc].maxn>=tree[tree[root].rc].maxn){ tree[root].maxn=tree[tree[root].lc].maxn; tree[root].pos=tree[tree[root].lc].pos;68 行未改动#### 势能线段树
1. [P4145 上帝造题的七分钟 2 / 花神游历各国](https://www.luogu.com.cn/problem/P4145)2. [SPOJ Can you answer these queries IV](https://www.luogu.com.cn/problem/SP2713)(双倍经验?3. [P7334 [JRKSJ R1] 吊打](https://www.luogu.com.cn/problem/P7334) 3. [P7334 [JRKSJ R1] 吊打](https://www.luogu.com.cn/problem/P7334)4. [CF438D The Child and Sequence](https://www.luogu.com.cn/problem/CF438D)5. [HUD5306 Gorgeous Sequence](http://acm.hdu.edu.cn/showproblem.php?pid=5306)(注:这个更像吉司机板子)6. [P6242 [模板] 线段树 3](https://www.luogu.com.cn/problem/P6242)7. [CF855F Nagini](https://www.luogu.com.cn/problem/CF855F)52 行未改动