【BZOJ3747】[POI2015]Kinoman 线段树
【BZOJ3747】[POI2015]Kinoman
Description
Input
Output
Sample Input
2 3 1 1 4 1 2 4 1
5 3 6 6
Sample Output
样例解释:
观看第2,3,4,5,6,7天内放映的电影,其中看且仅看过一次的电影的编号为2,3,4。
题解:还是用到这一个思路:每个子串都是一个前缀的后缀,那么我们枚举每个前缀,然后用线段树维护它的每个后缀的答案即可。
具体地,如果当前位置是i,i的前缀是pre[i],那么在(pre[i],i]中的后缀的和都会加上w;还要减掉原来pre[i]的权值,即在(pre[pre[i]],pre[i]]里的后缀都要减去w。再用线段树查询最大值即可。
#include <cstdio>
#include <iostream>
#include <cstring>
#define lson x<<1
#define rson x<<1|1
using namespace std;
typedef long long ll;
const int maxn=1000010;
int n,m;
ll ans;
int w[maxn],f[maxn],pre[maxn],last[maxn];
ll s[maxn<<2],tag[maxn<<2];
void updata(int l,int r,int x,int a,int b,ll val)
{
if(a<=l&&r<=b)
{
s[x]+=val,tag[x]+=val;
return ;
}
if(tag[x]) s[lson]+=tag[x],tag[lson]+=tag[x],s[rson]+=tag[x],tag[rson]+=tag[x],tag[x]=0;
int mid=(l+r)>>1;
if(a<=mid) updata(l,mid,lson,a,b,val);
if(b>mid) updata(mid+1,r,rson,a,b,val);
s[x]=max(s[lson],s[rson]);
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-')f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=getchar();
return ret*f;
}
int main()
{
n=rd(),m=rd();
int i;
for(i=1;i<=n;i++) f[i]=rd(),pre[i]=last[f[i]],last[f[i]]=i;
for(i=1;i<=m;i++) w[i]=rd();
for(i=1;i<=n;i++)
{
updata(1,n,1,pre[i]+1,i,w[f[i]]);
if(pre[i]) updata(1,n,1,pre[pre[i]]+1,pre[i],-w[f[i]]);
ans=max(ans,s[1]);
}
printf("%lld",ans);
return 0;
}
【BZOJ3747】[POI2015]Kinoman 线段树的更多相关文章
- BZOJ3747:[POI2015]Kinoman(线段树)
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- 【bzoj3747】[POI2015]Kinoman 线段树区间合并
题目描述 一个长度为n的序列,每个数为1~m之一.求一段连续子序列,使得其中之出现过一次的数对应的价值之和最大. 输入 第一行两个整数n,m(1<=m<=n<=1000000). 第 ...
- 【bzoj3747】[POI2015]Kinoman - 线段树(经典)
Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[i]部. 你可以选择l,r(1<=l< ...
- [bzoj3747][POI2015]Kinoman_线段树
Kinoman bzoj-3747 POI-2015 题目大意:有m部电影,第i部电影的好看值为w[i].现在放了n天电影,请你选择一段区间l~r使得l到r之间的好看值总和最大.特别地,如果同一种电影 ...
- Bzoj 3747: [POI2015]Kinoman 线段树
3747: [POI2015]Kinoman Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 553 Solved: 222[Submit][Stat ...
- 【BZOJ-3747】Kinoman 线段树
3747: [POI2015]Kinoman Time Limit: 60 Sec Memory Limit: 128 MBSubmit: 715 Solved: 294[Submit][Stat ...
- 3747: [POI2015]Kinoman|线段树
枚举左区间线段树维护最大值 #include<algorithm> #include<iostream> #include<cstdlib> #include< ...
- BZOJ3747 POI2015 Kinoman 【线段树】*
BZOJ3747 POI2015 Kinoman Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放映的是第f[ ...
- BZOJ_3747_[POI2015]Kinoman_线段树
BZOJ_3747_[POI2015]Kinoman_线段树 Description 共有m部电影,编号为1~m,第i部电影的好看值为w[i]. 在n天之中(从1~n编号)每天会放映一部电影,第i天放 ...
随机推荐
- ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计
ylbtech-DatabaseDesgin:ylbtech-KeFuYunWei(服务运维考核系统)-数据库设计 DatabaseName:KEFUYUNWEI Model:Admin 用户后台管理 ...
- 图文介绍openLDAP在windows上的安装配置
目录 概述 测试环境 安装过程 配置启动 客户端介绍 多级DC的ldif文件的配置 [一].概述 什么叫LDAP呢,概念的东西这里就不多讲了,网上搜索下有很多,本文的重点是介绍如何在windows平台 ...
- CodeForces 388A Fox and Box Accumulation (模拟)
A. Fox and Box Accumulation time limit per test:1 second memory limit per test:256 megabytes Fox Cie ...
- Appium Android Bootstrap源代码分析之简单介绍
在上一个系列中我们分析了UiAutomator的核心源代码,对UiAutomator是怎么执行的原理有了根本的了解.今天我们会開始另外一个在安卓平台上基于UiAutomator的新起之秀--Appiu ...
- Python——os.path.dirname(__file__) 与 os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- lodash 数组裁剪 drop
_.drop(array, [n=1]) 裁剪数组中的前 N 个数组,返回剩余的部分. <!DOCTYPE html> <html lang="zh"> & ...
- Websphere: Stop Server and Uninstall Application
In WAS, stopping server and uninstalling application are important steps to re-deploy. SET ProfileLo ...
- 【BIEE】10_资料库查看数据报错
导入元数据后,在资料库右键物理表名,[查看数据]报错: 出现这个问题,没搞明白是为啥- 后来百度意外发现一个方法,修改NQSConfig.INI文件即可解决问题 那么就开始来搞定这个问题 [1]打开路 ...
- <转>Windows平台下Makefile学习笔记(一)
本文转自:http://blog.csdn.net/clever101/article/details/8147352 决心学习Makefile,一方面是为了解决编译开源代码时需要跨编译平台的问题(发 ...
- oc 经常用到弹出view的方法
#pragma mark 弹出view -(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur { CAKeyframeA ...