树状数组+dp(不太懂) Codeforces 597C

C. Subsequences
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Sample test(s)
input
5 2
1
2
3
5
4
output
7
 
         

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long n;
long long dp[12][100010];
long long query(long long x,long long i)
{
    long long ans=0;
    while(i>0)
    {
        ans+=dp[x][i];
        i-=i&-i;
    }
    return ans;
}
void update(long long x,long long i,long long num)
{
    while(i<=n)
    {
        dp[x][i]+=num;
        i+=i&-i;
    }
    return ;
}
int   main()
{
    long long  k;
    while(scanf("%lld%lld",&n,&k)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        for(long long  i=0; i<n; i++)
        {
            long long  x;
            scanf("%lld",&x);
            for(long long j=1; j<=k+1; j++)
            {
                long long res;
                if(j==1)
                    res=1;
                else res=query(j-1,x-1);
                update(j,x,res);
            }
        }
        printf("%lld\n",query(k+1,n));

    }
}


你可能感兴趣的:(树状数组+dp(不太懂) Codeforces 597C)