题目大意:给你一个序列(n<=35000),最多分不大于m块(m<=50),求每个块内不同元素的数量之和的最大值

考试的时候第一眼建图,没建出来,第二眼贪心 ,被自己hack掉了,又随手写了个dp方程,感觉可以用splay维护,发现有区间操作并可取,又发现这个方程只能用线段树维护,然后只剩40min了我没调完,而且线段树打错了......

定义f[i][j]表示以第i个数为这个块的结尾,已经分了j块的答案的最大值

很明显,sum表示不同元素数量

对于每个元素,记录一个表示数 i 上一次出现的位置,那么遍历到i的时候,能更新sum的位置只有到i-1,线段树维护区间修改!

而最大,线段树维护区间最大值!

算好空间,建立M颗线段树即可

 #include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define il inline
#define mod 905229641
#define N 35100
#define M 52
#define il inline
using namespace std;
//re
int n,m;
int a[N],lst[N];
int f[N][M];
struct Seg{
int ma[N<<],tag[N<<];
il void pushup(int rt){ma[rt]=max(ma[rt<<],ma[rt<<|]);}
il void pushdown(int rt){
if(tag[rt])
ma[rt<<]+=tag[rt],ma[rt<<|]+=tag[rt],
tag[rt<<]+=tag[rt],tag[rt<<|]+=tag[rt],tag[rt]=;}
void update(int L,int R,int l,int r,int rt,int w)
{
if(L>R) return;
if(L<=l&&r<=R){ma[rt]+=w;tag[rt]+=w;return;}
pushdown(rt);
int mid=(l+r)>>;
if(L<=mid) update(L,R,l,mid,rt<<,w);
if(R>mid) update(L,R,mid+,r,rt<<|,w);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L>R) return ;
if(L<=l&&r<=R) {return ma[rt];}
pushdown(rt);
int mid=(l+r)>>,ans=;
if(L<=mid) ans=max(query(L,R,l,mid,rt<<),ans);
if(R>mid) ans=max(query(L,R,mid+,r,rt<<|),ans);
pushup(rt);
return ans;
}
}s[M];
int main()
{
freopen("handsome.in","r",stdin);
freopen("handsome.out","w",stdout);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
s[j-].update(lst[a[i]],i-,,n,,),
f[i][j]=s[j-].query(,i-,,n,),
s[j].update(i,i,,n,,f[i][j]);
lst[a[i]]=i;
}
int ans=;
for(int i=;i<=m;i++)
ans=max(ans,f[n][i]);
printf("%d\n",ans);
return ;
}

CF833B The Bakery (线段树+DP)的更多相关文章

  1. CF833B The Bakery 线段树,DP

    CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...

  2. codeforces#426(div1) B - The Bakery (线段树 + dp)

    B. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

  3. Codeforces.833B.The Bakery(线段树 DP)

    题目链接 \(Description\) 有n个数,将其分为k段,每段的值为这一段的总共数字种类,问最大总值是多少 \(Solution\) DP,用\(f[i][j]\)表示当前在i 分成了j份(第 ...

  4. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  5. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  6. Codeforces Round #426 (Div. 1) B The Bakery (线段树+dp)

    B. The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard inp ...

  7. CodeForces 834D The Bakery(线段树优化DP)

    Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought required ingredient ...

  8. Codeforces Round #426 (Div. 2) D The Bakery(线段树 DP)

     The Bakery time limit per test 2.5 seconds memory limit per test 256 megabytes input standard input ...

  9. Codeforces Round #426 (Div. 2) D. The Bakery 线段树优化DP

    D. The Bakery   Some time ago Slastyona the Sweetmaid decided to open her own bakery! She bought req ...

随机推荐

  1. anaconda jupyter

    本文主要讲解在Ubuntu系统中,如何在Anaconda下安装TensorFlow以及配置Jupyter Notebook远程访问的过程. 在官方文档中提到,TensorFlow的安装主要有以下五种形 ...

  2. CF914A Perfect Squares

    CF914A Perfect Squares 题意翻译 给定一组有n个整数的数组a1,a2,…,an.找出这组数中的最大非完全平方数. 完全平方数是指有这样的一个数x,存在整数y,使得x=y^2y2  ...

  3. Java泛型(一):入门、原理、使用

    远在 JDK 1.4 版本的时候,那时候是没有泛型的概念的.当时 Java 程序员们写集合类的代码都是类似于下面这样: List list = new ArrayList(); list.add(&q ...

  4. Qt Quick 之 QML 与 C++ 混合编程具体解释

    Qt Quick 技术的引入.使得你能够高速构建 UI ,具有动画.各种绚丽效果的 UI 都不在话下.但它不是万能的.也有非常多局限性,原来 Qt 的一些技术,比方低阶的网络编程如 QTcpSocke ...

  5. [Tailwind] Abstract Utility Classes to BEM Components in Tailwind

    When creating UIs with utility classes, a lot of repetition can occur within the HTML markup. In thi ...

  6. poj 3356

    Description Let x and y be two strings over some finite alphabet A. We would like to transform x int ...

  7. Threejs 官网 - 怎样执行本地的东西(How to run things locally)

    Threejs 官网 - 怎样执行本地的东西(How to run things locally) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循&qu ...

  8. 前端 自定义format函数

    为字符串创建format方法,用于字符串格式化  {# 前端没有字符串占位符%s的替代方法,以下是自定义字符串替换的方法,以后前端拓展方法都可以使用下面的形式 #} String.prototype. ...

  9. Flex AsDoc 完整版

    Flex 生成AsDoc用的是SDK自带的asdoc.exe工具 生成AsDoc文档的方式有两种:ant或者FlashBuilder 外部配置工具 方法一:外部配置工具 新增一个外部配置工具.过程例如 ...

  10. QT使用tableWidget显示双排列表 而且选中用红框圈出来

    如需转载请标明出处:http://blog.csdn.net/itas109 整个project下载地址:http://download.csdn.net/detail/itas109/7607735 ...