码迷,mamicode.com
首页 > 其他好文 > 详细

cf 833B 线段树优化dp

时间:2019-10-06 18:42:52      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:inf   long   pre   查询   记录   uil   线段   int   void   

写出转移方程即可。
贡献值计算有通用方法:记录每一个颜色上一个位置,这个颜色只在上个位置到当前位置有贡献,为1,这个可以通过线段树做到。同样,dp值也可以用线段树查询,所以我们把他们同时丢到一颗线段树里面就行了。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<climits>
#include<stack>
#include<vector>
#include<queue>
#include<set>
#include<map>
//#include<regex>
#include<cstdio>
#define up(i,a,b)  for(int i=a;i<b;i++)
#define dw(i,a,b)  for(int i=a;i>b;i--)
#define upd(i,a,b) for(int i=a;i<=b;i++)
#define dwd(i,a,b) for(int i=a;i>=b;i--)
//#define local
typedef long long ll;
const double esp = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int inf = 1e9;
using namespace std;
int read()
{
    char ch = getchar(); int x = 0, f = 1;
    while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
    return x * f;
}
typedef pair<int, int> pir;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lrt root<<1
#define rrt root<<1|1
const int N = 3e4 + 5100;
int pre[N];
int pos[N];
int n, k;
int a[N];
ll maxx[N<<2];
ll lazy[N<<2];
ll dp[N][55];
 
void pushup(int root)
{
    maxx[root] = max(maxx[lrt], maxx[rrt]);
}
void build(int l, int r, int root,int dep)
{
    lazy[root] = 0;
    if (l == r)
    {
        maxx[root] = dp[l][dep]; return;
    }
    int mid=(l + r) >> 1;
    build(lson,dep); build(rson,dep);
    pushup(root);
}
void pushdown(int root)
{
    if (lazy[root])
    {
        maxx[lrt] += lazy[root]; maxx[rrt] += lazy[root];
        lazy[lrt] += lazy[root];
        lazy[rrt] += lazy[root];
        lazy[root] = 0;
    }
}
void update(int l, int r, int root, int lf, int rt, ll val)
{
    if (lf <= l && r <= rt)
    {
        maxx[root] += val;
        lazy[root] += val;
        return;
    }
    pushdown(root);
    int mid = (l + r) >> 1;
    if (lf <= mid)update(lson, lf, rt, val);
    if (rt > mid)update(rson, lf, rt, val);
    pushup(root);
}
ll querry(int l, int r, int root, int lf, int rt)
{
    if (lf <= l && r <= rt)
    {
        return maxx[root];
    }
    pushdown(root);
    int mid = (l + r) >> 1;
    ll ans = 0;
    if (lf <= mid)ans = max(ans,querry(lson, lf, rt));
    if (rt > mid)ans = max(ans,querry(rson, lf, rt));
    return ans;
}
int main()
{
    n = read(); k = read();
    upd(i, 0, n)pos[i] = 0;
    upd(i, 0, n)dp[i][0] = 0;
    upd(i, 1, n)
    {
        a[i] = read(); pre[i] = pos[a[i]]; pos[a[i]] = i;
    }
    upd(j,1,k)
    {
        build(0, n, 1,j-1);
        upd(i, j, n)
        {
            update(0, n, 1, pre[i], i - 1, 1ll);
            dp[i][j] = querry(0, n,1, 0, i - 1);
        }
    }
    cout << dp[n][k] << endl;
    return 0;
}

cf 833B 线段树优化dp

标签:inf   long   pre   查询   记录   uil   线段   int   void   

原文地址:https://www.cnblogs.com/LORDXX/p/11627810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!