Time Limit: 3 second(s) | Memory Limit: 64 MB |
An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold
1. i1 < i2 < i3 < ... < ik and
2. Ai1 < Ai2 < Ai3 < ... < Aik
Now you are given a sequence, you have to find the number of all possible increasing subsequences.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.
Output
For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.
Sample Input | Output for Sample Input |
3 3 1 1 2 5 1 2 1000 1000 1001 3 1 10 11 | Case 1: 5 Case 2: 23 Case 3: 7 |
Notes
1. For the first case, the increasing subsequences are (1), (1, 2), (1), (1, 2), 2.
2. Dataset is huge, use faster I/O methods.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5+100;
const int MOD = 1000000007;
int bit[MAX_N],n,T,a[MAX_N],b[MAX_N];
void add(int i,int x){
while(i<=n){
bit[i] += x;
while(bit[i]>MOD) bit[i] -= MOD;
i+=i&-i;
}
}
int sum(int i){
int res = 0;
while(i>0){
res += bit[i];
i -= i&-i;
while(res>MOD) res -= MOD;
}
return res;
}
int main(){
scanf("%d",&T);
for(int t=1;t<=T;t++){
memset(bit,0,sizeof(bit));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
b[i] = a[i];
}
// 以下代码为离散化操作
sort(a,a+n);
int N = unique(a,a+n)-a;
for(int i=0;i<n;i++){
b[i] = lower_bound(a,a+N,b[i])-a;
b[i]+=1;
}
// 树状数组维护值
for(int i=0;i<n;i++){
int s = sum(b[i]-1);
s++;
add(b[i],s);
}
int ans = sum(n);
printf("Case %d: %d\n",t,ans);
}
return 0;
}