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' ...
随机推荐
- Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible
Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible bundle exec rake ...
- 大神给你分析HTTPS和HTTP的区别(转)
http://www.php100.com/html/it/biancheng/2015/0209/8582.html 今天在做雅虎的时候,发现用第三方工具截取不到客户端与服务端的通讯,以前重来没碰到 ...
- UVaLive 6832 Bit String Reordering (模拟)
题意:给定一个01序列,然后让你你最少的操作数把这它变成目标. 析:由于01必须是交替出现的,那么我们就算两次,然后取最值. 代码如下: #pragma comment(linker, "/ ...
- 栗染-Myeclispe连接SQL Server数据库
第一步,在SQL server方面 这里是以身份验证登录. 这里我是建了一个hw的数据库,其他没啥说的. 第二步,最主要的一部分 因为第一次连接SQL Server数据库,所以就不知道还有这一步.不然 ...
- robotframework - create dictionary 操作
1.创建字典 2.从字典中获取的项 -- 打印出 item 3.获取字典的key -- 打印出 key 4.获取字典的value -- 打印出 value 5.获取字典key,value 6.打印出字 ...
- EntityFramework:An error occurred while executing the command definition. See the inner exception for details.
错误描述: 调用EF中的FirstOrDefault()时,报错误:An error occurred while executing the command definition. See the ...
- ASP.NET MVC 导出CSV文件
ASP.NET MVC 导出CSV文件.直接贴代码 /// <summary> /// ASP.NET MVC导出CSV文件Demo1 /// </summary> /// ...
- Linux环境下修改MySQL数据库对表名大小写不敏感
Linux系统中MySQL对数据库名称和表名是大小写敏感的,这就导致了一些麻烦,虽然已经建立了表和数据,但因为大小写导致无法找到表. MySQL数据库对表名大小写不敏感的设置方法如下: 1.查看MyS ...
- Android 性能优化(27)*zipalign让apk数据对齐,运行更快。
1.zipalign 简介 zipalign is an archive alignment tool that provides important optimization to Android ...
- 274 H-Index H指数
给定一位研究者的论文被引用次数的数组(被引用次数是非负整数).写一个方法计算出研究者的H指数.H-index定义: “一位科学家有指数 h 是指他(她)的 N 篇论文中至多有 h 篇论文,分别被引用了 ...