BZOJ 3747 洛谷 3582 [POI2015]Kinoman
【题解】
扫描线+线段树。
我们记第i部电影上次出现的位置是$pre[i]$,我们从$1$到$n$扫描,每次区间$(pre[i],i]$加上第i部电影的贡献$w[f[i]]$,区间$[pre[pre[i]],pre[i]]$减去第i部电影的贡献$w[f[i]]$.
#include<cstdio>
#include<algorithm>
#define N 1000010
#define rg register
#define ls (u<<1)
#define rs (u<<1|1)
#define mid ((l+r)>>1)
#define LL long long
using namespace std;
int n,m,f[N],w[N],pos[N],pre[N];
LL ans;
struct tree{
LL mx,del;
}a[N<<];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline LL max(LL x,LL y){
return x>y?x:y;
}
inline void pushup(int u){
a[u].mx=max(a[ls].mx,a[rs].mx);
}
inline void pushdown(int u){
if(!a[u].del) return;
LL d=a[u].del; a[u].del=;
a[ls].mx+=d; a[ls].del+=d;
a[rs].mx+=d; a[rs].del+=d;
}
void add(int u,int ql,int qr,int l,int r,LL d){
//printf("%d %d %d %d\n",ql,qr,l,r);
if(ql<=l&&r<=qr){
a[u].del+=d; a[u].mx+=d; return;
}
pushdown(u);
if(ql<=mid) add(ls,ql,qr,l,mid,d);
if(qr>mid) add(rs,ql,qr,mid+,r,d);
pushup(u);
}
int main(){
n=read(); m=read();
for(int i=;i<=n;i++){
f[i]=read();
pre[i]=pos[f[i]]; pos[f[i]]=i;
}
for(int i=;i<=m;i++) w[i]=read();
for(int i=;i<=n;i++){
int prefix=pre[i];
add(,prefix+,i,,n,w[f[i]]);
if(prefix) add(,pre[prefix]+,prefix,,n,-w[f[i]]);
ans=max(ans,a[].mx);
}
printf("%lld\n",ans);
return ;
}
BZOJ 3747 洛谷 3582 [POI2015]Kinoman的更多相关文章
- BZOJ 4385 洛谷3594 POI2015 WIL-Wilcze doły
[题解] 手残写错调了好久QAQ...... 洛谷的数据似乎比较水.. n个正整数!!这很重要 这道题是个类似two pointer的思想,外加一个单调队列维护当前区间内长度为d的子序列中元素之和的最 ...
- [BZOJ 3039&洛谷P4147]玉蟾宫 题解(单调栈)
[BZOJ 3039&洛谷P4147]玉蟾宫 Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地. ...
- bzoj 4816: 洛谷 P3704: [SDOI2017]数字表格
洛谷很早以前就写过了,今天交到bzoj发现TLE了. 检查了一下发现自己复杂度是错的. 题目传送门:洛谷P3704. 题意简述: 求 \(\prod_{i=1}^{N}\prod_{j=1}^{M}F ...
- bzoj 1014: 洛谷 P4036: [JSOI2008]火星人
题目传送门:洛谷P4036. 题意简述: 有一个字符串,支持插入字符,修改字符. 每次需要查询两个后缀的LCP长度. 最终字符串长度\(\le 100,\!000\),修改和询问的总个数\(\le 1 ...
- bzoj 3680(洛谷1337) 吊打XXX——模拟退火
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3680 https://www.luogu.org/problemnew/show/P1337 ...
- bzoj 4592(洛谷 4344) [Shoi2015]脑洞治疗仪——线段树上二分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4592 1操作就是用线段树来二分找到第一个有 k 个0的位置. 在洛谷上A了,与暴力和网上题解 ...
- Guard Duty (medium) Codeforces - 958E2 || (bzoj 2151||洛谷P1792) 种树 || 编译优化
https://codeforces.com/contest/958/problem/E2 首先求出N个时刻的N-1个间隔长度,问题就相当于在这些间隔中选K个数,相邻两个不能同时选,要求和最小 方法1 ...
- bzoj 2005 & 洛谷 P1447 [ Noi 2010 ] 能量采集 —— 容斥 / 莫比乌斯反演
题目:bzoj 2005 https://www.lydsy.com/JudgeOnline/problem.php?id=2005 洛谷 P1447 https://www.luogu.org/ ...
- BZOJ 1901 洛谷 P2617 ZOJ 2112 Dynamic Rankings
以下时空限制来自zoj Time limit 10000 ms Memory limit 32768 kB OS Linux Source Online Contest of Christopher' ...
随机推荐
- 使用inet_ntoa() 时编译提示错误:
char*inet_ntoa(struct in_addr in)将一个IP转换成一个互联网标准点分格式的字符串. 我把 inet_ntoa 需要的头文件加上去: #include <sy ...
- sql复杂查询语句总结
转自:http://blog.csdn.net/fengfeng91/article/details/15029173 create table student( sno varchar2(10) p ...
- 基于Spark的GBDT + LR模型实现
目录 基于Spark的GBDT + LR模型实现 数据预处理部分 GBDT模型部分(省略调参部分) GBDT与LR混合部分 基于Spark的GBDT + LR模型实现 测试数据来源http://arc ...
- SSM整合配置错误记录
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dic ...
- vue父组件访问子组件
1.父组件(父组件访问子组件的方法drop) <!--父组件访问子组件的方法v-ref:shopcart--> <template> <div id="pare ...
- 慕课网4-6 编程练习:jQuery后排兄弟选择器
4-6 编程练习 结合所学的兄弟选择器" ~ ",实现如下图所示效果: 任务 (1)使用兄弟选择器" ~ "将技术语言的背景色变成红色 (2)使用jQuery的 ...
- JAVA POI的使用
最近开发遇到了要通过Java处理Excel文件的场景,于是乎在网上了解了一番,最后自己做了个demo,已上传gitee:https://gitee.com/github-26930945/JavaCo ...
- spring cloud config搭建说明例子(三)-添加actuator
添加心跳 服务端 ConfigServer pom.xml添加actuator包 <dependency> <groupId>org.springframework.cloud ...
- ToolBar教程:AppCompatActivity下用toolbar当actionbar用
参考: https://developer.android.com/training/appbar/index.html 1,自定义toolbar主题 2,在布局xml中使用toolbar 3,在代码 ...
- MongoDB学习笔记~复杂条件拼接和正则的使用
在大叔lind框架里有日志组件logger,而在日志实现方式上有file,mongodb,sql,json等方式,对分布式日志处理上大叔推荐使用mongodb进行存储,除了它的高效写入,灵活的结构外, ...