线段树内存下mx[k]的值是动态的1-i这个区间的贡献答案

实际上点存的就是区间答案,但用max是为了求最大区间答案(有可能虽然贡献被消除但后来有更大的贡献填补答案空缺)

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i=x;i<=y;i++)
#define dec(i,x,y) for(register int i=x;i>=y;i--)
#define LL long long
#define In freopen("7.in","r",stdin)
#define In2 freopen("8.in","r",stdin)
using namespace std;
const int N=1e6+;
inline int read(){
int x=,f=;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-;ch=getchar();}
while(isdigit(ch)){x=(x<<)+(x<<)+(ch^);ch=getchar();}
return x*f;}
int n,m,w[N],f[N],mark[N],pre[N];
LL mx[N<<],tag[N<<],ans=;
inline void pushup(int k){
mx[k]=max(mx[k<<],mx[k<<|]);}
inline void pushdown(int k,int l,int r,int mid){
if(l==r) return;
LL v=tag[k];tag[k]=;
tag[k<<]+=v;mx[k<<]+=v;
tag[k<<|]+=v;mx[k<<|]+=v;}
inline void change(int k,int l,int r,int x,int y,LL d){
if(x<=l&&r<=y){
tag[k]+=d;mx[k]+=d;return;}
int mid=(l+r)>>;
if(tag[k]) pushdown(k,l,r,mid);
if(x<=mid) change(k<<,l,mid,x,y,d);
if(mid<y) change(k<<|,mid+,r,x,y,d);
pushup(k);
}
inline LL query(int k,int l,int r,int x,int y){
if(x<=l&&r<=y) return mx[k];LL res=;
int mid=(l+r)>>;if(tag[k])pushdown(k,l,r,mid);
if(x<=mid) res=max(res,query(k<<,l,mid,x,y));
if(mid<y) res=max(res,query(k<<|,mid+,r,x,y));
return mx[k];}
int main(){
n=read();m=read();
rep(i,,n) f[i]=read();
rep(i,,m) w[i]=read();
rep(i,,n) pre[i]=mark[f[i]],mark[f[i]]=i; rep(i,,n){
change(,,n,pre[i]+,i,w[f[i]]);
if(pre[i]) change(,,n,pre[pre[i]]+,pre[i],-w[f[i]]);
ans=max(ans,query(,,n,,i));
}
printf("%lld\n",ans);return ;
}

luogu 3582 线段树的更多相关文章

  1. 【luogu P3372 线段树1】 模板

    线段树的模板题 题目链接:https://www.luogu.org/problemnew/show/P3372 update区间修改,query区间求和 #include <iostream& ...

  2. 【luogu P3373 线段树2】 模板

    题目链接:https://www.luogu.org/problemnew/show/P3373 lazy标记两个,先乘后加 #include <iostream> #include &l ...

  3. [线段树]Luogu P3372 线段树 1【模板】

    #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #d ...

  4. 【BZOJ1798】【AHOI2009】维护序列(线段树)

    题目链接 题解 这不就是luogu的线段树2的板子吗.... 没有任何的区别... 上代码吧... #include<iostream> #include<cstdio> #i ...

  5. Luogu 45887 全村最好的嘤嘤刀(线段树 树状数组)

    https://www.luogu.org/problemnew/show/T45887 题目背景 重阳节到了,我们最好的八重樱拥有全村最好的嘤嘤刀…… 题目描述 在绯玉丸力量的影响下,八重村成了一条 ...

  6. Luogu P4643 【模板】动态dp(矩阵乘法,线段树,树链剖分)

    题面 给定一棵 \(n\) 个点的树,点带点权. 有 \(m\) 次操作,每次操作给定 \(x,y\) ,表示修改点 \(x\) 的权值为 \(y\) . 你需要在每次操作之后求出这棵树的最大权独立集 ...

  7. Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树)

    Luogu 2590 [ZJOI2008]树的统计 / HYSBZ 1036 [ZJOI2008]树的统计Count (树链剖分,LCA,线段树) Description 一棵树上有n个节点,编号分别 ...

  8. NOIP 2016 天天爱跑步 (luogu 1600 & uoj 261) - 线段树

    题目传送门 传送点I 传送点II 题目大意 (此题目不需要大意,我认为它已经很简洁了) 显然线段树合并(我也不知道哪来这么多显然) 考虑将每条路径拆成两条路径 s -> lca 和 t -> ...

  9. Luogu 3373 又乘又加的线段树

    Luogu 3373 又乘又加的线段树 当给一个节点加上一个加法标记时,直接把加法标记 += 新值: 当给一个节点加上一个乘法标记时,把乘法标记和加法标记同时 *= 新值.(注意pushdown函数中 ...

随机推荐

  1. Linux里文件和文件夹权限的含义

      文件的权限: r : 可以读取此文件的实际内容. w: 可以编辑.新增或者是修改该文件的内容(但不含删除该文件),如果没有r权限,无法w. x : 该文件具有被系统执行的权限,可以删除. 文件夹的 ...

  2. request.getRequestDispatcher 页面跳转,样式丢失。

    在页面中引用样式和其它资源的时候,尽量不要用相对路径,因为"当前路径"这个概念在J2EE中是不稳定的. 所以最好都是绝对路径,类似于: <% String cp = requ ...

  3. 1145. Hashing - Average Search Time

      The task of this problem is simple: insert a sequence of distinct positive integers into a hash ta ...

  4. A1125. Chain the Ropes

    Given some segments of rope, you are supposed to chain them into one rope. Each time you may only fo ...

  5. 【CF131D】Subway

    题目大意:给定一棵 N 个节点的基环树,求各个点到环的最小距离. 题解:除了找环的必须参数之外,对每个点维护一个 fa 即可. 代码如下 #include <bits/stdc++.h> ...

  6. ImageMagick: 6.8.3 升级到 6.8.9 遇到的问题

    最终还是决定升级到目前最新版:6.8.9,不知何时才真正明白为什么现在都是java8,但还是有很多软件系统使用在java5上. 虽然新版本能带来各种好处,但现实中不能忽略一个问题:原来的代码很可能无法 ...

  7. Unable to load script from assets 'index.android.bundle'.make sure you bundle is packaged correctly

    解决此问题 以下方法每次都需要执行命令2才能更新 1.创建assets目录 mkdir android/app/src/main/assets 2.执行命令 react-native bundle - ...

  8. MySQL字符集 utf8 和 utf8mb4 区别及排序规则 general_ci 和 unicode_ci 和 bin 的区别

    先说字符集 utf8mb4说明:MySQL在5.5.3之后增加了这个utf8mb4的编码,mb4就是most bytes 4的意思,专门用来兼容四字节的unicode.好在utf8mb4是utf8的超 ...

  9. windows环境下 安装python2和python3

    一.  python 安装 1. 下载安装包 https://www.python.org/ftp/python/2.7.14/python-2.7.14.amd64.msi # 2.7安装包 htt ...

  10. maven+testng+eclipse

    1.安装maven 2.安装testng 3.配置maven的dependency,和build <project xmlns="http://maven.apache.org/POM ...