CodeForces 597C_Subsequences

Description

For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.

Input

First line contain two integer values n and k(1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.

Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.

Output

Print one integer — the answer to the problem.


大意是从n个数中找有多少个长度为k+1的升序子串。

dp[k][m]代表以m结尾的长度为k的子串数目,每次更新的时候都需要把dp[k-1][1]~dp[k-1][a[i]]的所有值加起来,直接加绝对会tle

一开始也不会做,后来问了同学之后才知道有树状数组,推荐: http://dongxicheng.org/structure/binary_indexed_tree/

#include
using namespace std;
typedef unsigned long long ull;
int n;
int k;
const int MAXN = 1e5 + 10;
const int MAXK = 15;
int a[MAXN];
ull dp[MAXK][MAXN];//以n为结尾长度为k的数量
//树状数组------------------------
int lowbit(int t){
	return t&(t ^ (t - 1));
}
ull sum(int k,int end){
	ull sum = 0;
	while (end > 0){
		sum += dp[k][end];
		end -= lowbit(end);
	}
	return sum;
}
void pluss(int k, int pos, ull num){
	while (pos <= n){
		dp[k][pos] += num;
		pos += lowbit(pos);
	}
}
//--------------------------------
int main()
{
	while (cin >> n >> k){
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= n; ++i){
			cin >> a[i];
			for (int tk = k + 1; tk >= 2; --tk){
				pluss(tk, a[i], sum(tk - 1, a[i]));
			}
			pluss(1, a[i], 1);
		}
		ull ans = 0;
		ans = sum(k + 1, n);
		cout << ans << endl;
	}
	return 0;
}


你可能感兴趣的:(DP,codeforces,dp,树状数组)