CodeForces 597C 树状数组 dp

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35346829/article/details/54571751

CodeForces 597C

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll maxn = 1e5;
ll d[maxn+10];
ll dp[15][maxn+10];
ll a[maxn+10];
ll n,k;
ll lowbit(ll x){
	return x&(-x);
}
ll updata(ll x,ll k){
	while(x <= n){
		d[x] += k;
		x += lowbit(x);
	}
}
ll getSum(ll x){
//	if(x == 0) return 0;
	ll ans = 0;
	while(x > 0){
		ans += d[x];
		x -= lowbit(x);
	}
	return ans;
}
// dp[长度][结尾的数] = t sum(1,结尾的数-1)dp[长度-1][t] // 树状数组维护这个前n项和 
int main(){
	while(scanf("%I64d%I64d",&n,&k) != EOF){
		memset(dp,0,sizeof(dp));
		for(int i = 1;i <= n;++i) scanf("%I64d",&a[i]);
		for(int i = 1;i <= n;++i) dp[1][i] = 1;
		for(int i = 2;i <= k+1;++i){
			memset(d,0,sizeof(d));
			for(int j = 1;j <= n;++j){
				dp[i][a[j]] += getSum(a[j]-1);
				updata(a[j],dp[i-1][a[j]]);
			}
		}
		ll ans = 0;
		for(int i = 1;i <= n;++i) ans += dp[k+1][i];
		printf("%I64d\n",ans);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq_35346829/article/details/54571751