【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天放 ...
随机推荐
- linux 关闭ping探测
先切换到root echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 1代表关闭 0代表开启 用iptables iptables -I INPUT ...
- django 删除表后如何恢复
大家都知道Python manage.py makemigrations 创建迁移文件也就是sql语句,python manage.py migrate执行makemigrations创建的文件,如果 ...
- Java的身份证号码工具类
/** * Copyright (C) 2009-2010 Yichuan, Fuchun All rights reserved. * Licensed to the Apache Software ...
- [转载]How to Install Google Chrome 39 in CentOS/RHEL 6 and Fedora 19/18
FROM: http://tecadmin.net/install-google-chrome-in-centos-rhel-and-fedora/ Google Chrome is a freewa ...
- 2017.5.27 使用propagation实现:根据参数决定是否需要事务管理
1.功能描述 要实现rest接口:POST ***/entry,其中参数中有action参数. 当action=rollback时,批量新增出错时需要回滚. 当action!=rollback时,批量 ...
- 2017.3.31 spring mvc教程(八) <mvc:annotation-driven />所做的工作
学习的博客:http://elf8848.iteye.com/blog/875830/ 我项目中所用的版本:4.2.0.博客的时间比较早,11年的,学习的是Spring3 MVC.不知道版本上有没有变 ...
- Html5 Canvas之arc函数图示详解
在不变换坐标系的情况下,arc函数画弧的方向如下图所示: 举例来说,arc(x,y,r,Math.PI/6,Math.PI/3,false);是以x,y为圆心,r为半径,从x正轴顺时针30度为起始角, ...
- java学习笔记——日期处理
获取系统当前时间使用:java.util.Date类,而这个Date的构造方法如下: 无参构造:public Date() 有参构造:public Date(long date) 第一个实例: imp ...
- OpenCV2学习笔记(十五):利用Cmake高速查找OpenCV函数源代码
在使用OpenCV时,在对一个函数的调用不是非常了解的情况下,通常希望查到该函数的官方声明.而假设想进一步研究OpenCV的函数,则必须深入到源码. 在VS中我们能够选中想要查看的OpenCV函数,点 ...
- Android中Activity的生命周期图