unlucky_13

LOJ_1085 - All Possible Increasing Subsequences

Jul 24th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.27 KB | None | 0 0
  1. /* unlucky_13 Jul 22, 2013 10:21:02 PM
  2.  *
  3.  */
  4. #include <cmath>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <iostream>
  9. #include <vector>
  10. #include <queue>
  11. #include <map>
  12. #include <set>
  13. #include <algorithm>
  14. using namespace std;
  15. #define maxn 111111
  16. #define mod 1000000007
  17. #define LL  long long int
  18. LL n ,Tree[4*maxn];
  19. struct INT{
  20.  
  21.     LL idx,val ;
  22.     bool operator < ( const INT& p ) const {
  23.         if(val!=p.val) return val < p.val;
  24.         else return idx > p.idx ;
  25.     }
  26. };
  27.  
  28. INT A[maxn] ;
  29. LL query(LL n,LL s,LL e,LL i,LL j){
  30.  
  31.     if(j<s || e<i){
  32.         return 0 ;
  33.     }
  34.  
  35.     if(i==s && e==j){
  36.         return Tree[n] ;
  37.     }
  38.  
  39.     LL mid = (s+e)>>1 ;
  40.     if(j<=mid){
  41.         return query(2*n,s,mid,i,j) ;
  42.     }
  43.     else if(i>mid){
  44.         return query(2*n+1,mid+1,e,i,j) ;
  45.     }
  46.  
  47.     else{
  48.  
  49.        LL p1 = query(2*n,s,mid,i,mid) ;
  50.        LL p2 = query(2*n+1,mid+1,e,mid+1,j) ;
  51.        return (p1+p2)%mod ;
  52.     }
  53.  
  54. }
  55.  
  56. void update(LL n,LL s,LL e,LL pos,LL val){
  57.  
  58.  
  59.     if(s<=pos && pos<=e){
  60.         Tree[n]+=val;
  61.         Tree[n] %=mod ;
  62.     }
  63.  
  64.     if(s==e){
  65.         return ;
  66.     }
  67.  
  68.     LL mid = (s+e)>>1 ;
  69.     LL next = n<<1 ;
  70.     if(pos<=mid){
  71.         update(next,s,mid,pos,val) ;
  72.     }
  73.  
  74.     else if(pos>mid){
  75.         update(next+1,mid+1,e,pos,val) ;
  76.     }
  77.  
  78.     else{
  79.         update(next,s,mid,pos,val) ;
  80.         update(next+1,mid+1,e,pos,val) ;
  81.     }
  82.  
  83. }
  84.  
  85. int main() {
  86.  
  87.     //freopen("//home//unlucky_13//workspace//Hello_World//src//in.txt","r",stdin) ;
  88.     LL tc,ct=0;
  89.     scanf("%lld",&tc) ;
  90.     while(tc!=ct){
  91.         memset(Tree,0,sizeof(Tree)) ;
  92.         scanf("%lld",&n) ;
  93.         for(LL i=0;i<n;i++) {
  94.             scanf("%lld",&A[i].val) ;
  95.             A[i].idx = i+1 ;
  96.         }
  97.  
  98.         sort(A,A+n) ;
  99.         //for(LL i=0;i<n;i++) cout<<A[i].idx<<" "<<A[i].val<<endl ;
  100.         LL res = 0 ;
  101.         LL add = 0 ;
  102.         //LL ADD[maxn] ;
  103.         for(LL i=0;i<n;i++){
  104.  
  105.                    /*if(i==0 || A[i].val!=A[i-1].val) {
  106.                        add = query(1,1,n,1,A[i].idx-1)+1 ; //to handle duplicate numbers
  107.                    }
  108.                    else{
  109.                        cout<<"this"<<endl ;
  110.                        add = add+query(1,1,n,A[i-1].idx+1,A[i].idx-1)+1 ;
  111.                    }*/
  112.                    add = query(1,1,n,1,A[i].idx-1)+1 ;
  113.                    //cout<<add<<endl ;
  114.                    add %=mod ;
  115.                    res+=add ;
  116.                    res %=mod ;
  117.                    update(1,1,n,A[i].idx,add) ;
  118.  
  119.         }
  120.  
  121.         printf("Case %lld: %lld\n",++ct,res) ;
  122.  
  123.     }
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment