首页 > 代码库 > SRM12 T2

SRM12 T2

技术分享
#include<iostream>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;const int maxn=40010,inf=1e9;struct poi{int max,delta;}tree[maxn*10];int n,K,N,cnt;int a[maxn],b[maxn],pre[maxn],root;int f[maxn][51];void read(int &k){    int f=1;k=0;char c=getchar();    while(c<0||c>9)c==-&&(f=-1),c=getchar();    while(c<=9&&c>=0)k=k*10+c-0,c=getchar();    k*=f;}inline int max(int a,int b){return a>b?a:b;}inline void pushup(int x){tree[x].max=max(tree[x<<1].max,tree[x<<1|1].max);}inline void pushdown(int x){    if(!tree[x].delta)return;    tree[x<<1].delta+=tree[x].delta;    tree[x<<1|1].delta+=tree[x].delta;    tree[x<<1].max+=tree[x].delta;    tree[x<<1|1].max+=tree[x].delta;    tree[x].delta=0;}void build(int x,int l,int r,int ty){    if(l==r){tree[x].max=f[l-1][ty];tree[x].delta=0;return;}    int mid=(l+r)>>1;tree[x].delta=0;    build(x<<1,l,mid,ty);    build(x<<1|1,mid+1,r,ty);    pushup(x);}inline void add(int x,int l,int r,int cl,int cr){    if(cl<=l&&r<=cr){tree[x].max++;tree[x].delta++;return;}    pushdown(x);    int mid=(l+r)>>1;    if(cl<=mid)add(x<<1,l,mid,cl,cr);    if(cr>mid)add(x<<1|1,mid+1,r,cl,cr);    pushup(x);}inline void insert(int x,int l,int r,int cx,int delta){    if(l==r){tree[x].max=delta;return;}    int mid=(l+r)>>1;    if(cx<=mid)insert(x<<1,l,mid,cx,delta);    else insert(x<<1|1,mid+1,r,cx,delta);    pushup(x);}inline int query(int x,int l,int r,int cl,int cr){    if(cl<=l&&r<=cr)return tree[x].max;    pushdown(x);    int mid=(l+r)>>1,ans=0;    if(cl<=mid)ans=query(x<<1,l,mid,cl,cr);    if(cr>mid)ans=max(ans,query(x<<1|1,mid+1,r,cl,cr));    return ans;}int main(){    freopen("camp.in","r",stdin);    freopen("camp.ans","w",stdout);    read(n);read(K);    for(int i=1;i<=n;i++)read(a[i]),b[i]=a[i];N=n;    N=unique(b+1,b+1+N)-b-1;sort(b+1,b+1+N);    for(int i=1;i<=n;i++)a[i]=lower_bound(b+1,b+1+n,a[i])-b;    for(int j=1;j<=K;j++)    {        for(int i=1;i<=n;i++)        {            add(1,1,n+1,pre[a[i]]+1,i);            f[i][j]=query(1,1,n+1,1,i);            pre[a[i]]=i;        }        if(j==K)continue;        build(1,1,n+1,j);for(int i=1;i<=n;i++)pre[a[i]]=0;    }    printf("%d\n",f[n][K]);}
View Code

 

SRM12 T2