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' ...
随机推荐
- mysql数据类型和java对应表(copy)
[说明] 资料来自:http://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-type-conversions.html My ...
- E20171230-hm
refine vt. 提炼; 改善; 使高雅; revert vi. 恢复; 重提; 回到…上; <律>归还; vt. 使恢复原状; n ...
- JavaSwing输入对话框,点击取消抛出异常的解决方法
在做产品管理系统的时候,遇到一个问题: 在得到一个输入框对话框的时候 String textPrice = JOptionPane.showInputDialog("请输入要调整的价格增(减 ...
- [Swift通天遁地]一、超级工具-(16)使用JTAppleCalendar制作美观的日历
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Mybatis Generator插件升级版
一.目的: 1. *mapper.java 文件名称 改为*DAO.java2. mapper以及mapper.xml 重复执行,只会覆盖原模板方法,不会覆盖自定义方法3. 实体类添加中文注释 二.步 ...
- Spring 中 ApplicationContext 和 BeanFactory 的区别,以及 Spring bean 作用域
//从ApplicationContext 中取 bean ApplicationContext ac = new ClassPathXmlApplicationContext ( "com ...
- 题解报告:poj 3669 Meteor Shower(bfs)
Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteo ...
- WebSphere设置会话超时时间
WebSphere Application Server的会话超时时间可以在三个层面进行设置,分别为:应用程序服务器级别.应用程序级别和代码层面进行设置. 设置方式:应用程序级别级别和应用级别可以通过 ...
- Apache JServ protocol服务 怎么关闭?
Apache JServ protocol = AJP 解决方案:修改tomcat 的service.xml配置文件 将 <Connector port="8009" ...
- Git——github高级
分支管理 分支不是越多越好,只求一个稳定的分支,即master不要轻易去更改 对应master要有一个开发者分支,保证mater分支的稳定性 所有的功能都在开发者分支上进行 在所有功能开发后新建发布分 ...