题目分析:我们枚举所有点R作为终点,求【1,R】内有多少个符合条件的L使得【L,R】的lis等于【1,n】的LIS。为每个终点R,找到能得到LIS的最右端的起点L,这样从1~L这段区间内的点都是可以和R构成lis等于LIS的合法区间的。这个我们就求LIS的时候顺便保存二分得到的数组中每个位置对应最右端的起点,然后插入或替换点的时候就从上一个位置推过来,同时记录此时该长度下的合法区间个数,当更新LIS长度时,记录下的合法区间重置。
代码如下:
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std ;
typedef long long LL ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define root 1 , 1 , n
#define rt o , l , r
#define mid ( ( l + r ) >> 1 )
const int MAXN = 100005 ;
int cnt[MAXN] ;
int s[MAXN] , top ;
int n ;
int search ( int x , int l , int r ) {
while ( l < r ) {
int m = mid ;
if ( s[m] >= x ) r = m ;
else l = m + 1 ;
}
return l ;
}
void scanf ( int& x , char c = 0 ) {
while ( ( c = getchar () ) < '0' ) ;
x = c - '0' ;
while ( ( c = getchar () ) >= '0' ) x = x * 10 + c - '0' ;
}
void solve () {
int ans = 0 , t = 0 ;
int x ;
top = 0 ;
For ( i , 1 , n ) {
scanf ( x ) ;
if ( !top || s[top] < x ) {
s[++ top] = x ;
if ( top == 1 ) cnt[1] = i ;
else cnt[top] = cnt[top - 1] ;
ans = cnt[top] ;
t = 0 ;
} else {
int y = search ( x , 1 , top ) ;
if ( y == 1 ) cnt[1] = i ;
else cnt[y] = cnt[y - 1] ;
s[y] = x ;
ans += cnt[top] ;
if ( ans >= 1000000000 ) {
ans -= 1000000000 ;
++ t ;
}
}
}
printf ( "%I64d\n" , ans + t * 1000000000LL ) ;
}
int main () {
while ( ~scanf ( "%d" , &n ) ) solve () ;
return 0 ;
}