Advertisement
LinKin

last

Jun 12th, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.11 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:16777216")
  2.  
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<math.h>
  6. #include<stdlib.h>
  7. #include<ctype.h>
  8. #include<assert.h>
  9. #include<iostream>
  10. #include<vector>
  11. #include<stack>
  12. #include<queue>
  13. #include<set>
  14. #include<map>
  15. #include<string>
  16. #include<utility>
  17. #include<algorithm>
  18. #include<list>
  19. using namespace std;
  20.  
  21. #define CLR(a) memset(a,0,sizeof(a))
  22. #define SET(a) memset(a,-1,sizeof(a))
  23. #define pb push_back
  24. #define SZ(a) ((long)a.size())
  25. #define ALL(a) a.begin(),a.end()
  26. #define FOREACH(i, c) for( __typeof( (c).begin() ) i = (c).begin(); i != (c).end(); ++i )
  27. #define AREA2(x1,y1,x2,y2,x3,y3) ( x1*(y2-y3) + x2*(y3-y1) + x3*(y1-y2) )
  28. #define SQR(x) ((x)*(x))
  29. #define STR string
  30. #define IT iterator
  31. #define ff first
  32. #define ss second
  33. #define MP make_pair
  34. #define EPS 1e-9
  35. #define INF 1000000007
  36.  
  37. #define chk(a,k) ((bool)(a&(1<<(k))))
  38. #define set0(a,k) (a&(~(1<<(k))))
  39. #define set1(a,k) (a|(1<<(k)))
  40.  
  41. typedef long long Long;
  42. typedef vector<long> Vl;
  43. typedef vector<double> VD;
  44. typedef vector<Long> VL;
  45. typedef pair<long,long> Pll;
  46. typedef pair<Long,Long> PLL;
  47.  
  48. inline long FastMax(long x, long y) { return (((y-x)>>(32-1))&(x^y))^y; }
  49. inline long FastMin(long x, long y) { return (((y-x)>>(32-1))&(x^y))^x; }
  50.  
  51. long IR[] = { 0,-1,0,1,-1,-1,1,1 };
  52. long IC[] = { 1,0,-1,0,1,-1,-1,1 };
  53.  
  54. #define MAX 100007
  55. #define MOD 1000000007
  56.  
  57. Long nCr[MAX+7][14];
  58. Long N;
  59. Long A[MAX+7];
  60. vector<VL> col;
  61. vector<VL> first_grp,second_grp;
  62. vector<Long> arc_list[MAX+7];
  63.  
  64. void update_BIT( VL &A, Long i, Long v ){
  65.  
  66.     if( !v ) return;
  67.     while( i and i<A.size() ){
  68.         A[i] += v;
  69.         i += i&-i;
  70.     }
  71. }
  72.  
  73. Long find_BIT( VL &A, Long i ){
  74.  
  75.     if( i<0 ) return 0;
  76.     i = min( i,(Long)A.size()-1 );
  77.     Long s = 0;
  78.     while( i ){
  79.         s += A[i];
  80.         s %= MOD;
  81.         i -= i&-i;
  82.     }
  83.     return s;
  84. }
  85.  
  86. Long range_BIT( VL &A, Long i, Long j ){
  87.  
  88.     if( i>j ) return 0;
  89.     return ( find_BIT( A, j ) - find_BIT( A,i-1 ) )%MOD;
  90. }
  91.  
  92.  
  93.  
  94. int main( void ){
  95.  
  96.     Long i,j,Icase,k = 0;
  97.  
  98.     freopen("text1.txt","r",stdin );
  99.  
  100.     nCr[0][0] = 1;
  101.     for( i=1;i<=MAX;i++ ){
  102.         nCr[i][0] = 1;
  103.         if( i<=10 ) nCr[i][i] = 1;
  104.         for( j=1;j<=min( 10LL,i-1 );j++ ){
  105.             nCr[i][j] = ( nCr[i-1][j] + nCr[i-1][j-1] )%MOD;
  106.         }
  107.     }
  108.  
  109.     cin>>N;
  110.     col.resize( MAX+7 );
  111.     for( i=1;i<=N;i++ ){
  112.         cin>>A[i];
  113.         col[A[i]].pb( i );
  114.     }
  115.     Long Lim = (Long)sqrt(N)+1;
  116.     for( i=0;i<=MAX;i++ ){
  117.         if( !col[i].size() ) continue;
  118.         if( 0 and col[i].size() <= Lim ) first_grp.pb( col[i] );
  119.         else second_grp.pb( col[i] );
  120.     }
  121.     /****************************/
  122.     /*calculation for first grp*/
  123.     /***************************/
  124.     Long ans1 = 0;
  125.     for( i=0;i<first_grp.size();i++ ){
  126.         ans1 -= nCr[first_grp[i].size()][4];
  127.         ans1 %= MOD;
  128.         for( j=0;j<first_grp[i].size();j++ ){
  129.             for( k=j+1;k<first_grp[i].size();k++ ){
  130.                 arc_list[first_grp[i][k]].pb( first_grp[i][j]);
  131.             }
  132.         }
  133.     }
  134.     vector<Long> st_BIT( N+7 ),ed_BIT( N+7 );
  135.     Long t = 0;
  136.     for( i=1;i<=MAX;i++ ){
  137.         for( j=0;j<arc_list[i].size();j++ ){
  138.             Long u = arc_list[i][j],v = i;
  139.             ans1 += t;
  140.             ans1 %= MOD;
  141.             ans1 -= range_BIT( ed_BIT, 1, u );
  142.             ans1 %= MOD;
  143.             ans1 -= range_BIT( st_BIT, u, v );
  144.             ans1 %= MOD;
  145.         }
  146.         for( j=0;j<arc_list[i].size();j++ ){
  147.             update_BIT( st_BIT, arc_list[i][j], 1 );
  148.         }
  149.         update_BIT( ed_BIT, i, arc_list[i].size() );
  150.         t += arc_list[i].size();
  151.     }
  152.  
  153.     /****************************/
  154.     /*calculation for second grp*/
  155.     /***************************/
  156.     Long ans2 = 0;
  157.     for( i=0;i<second_grp.size();i++ ){
  158.         for( j=i+1;j<second_grp.size();j++ ){
  159.             ans2 += nCr[second_grp[i].size()][2] * nCr[second_grp[j].size()][2];
  160.             ans2 %= MOD;
  161.         }
  162.     }
  163.     for( i=0;i<second_grp.size();i++ ){
  164.         if( second_grp[i].size() <= 1 ) continue;
  165.         for( j=0;j<second_grp.size();j++ ){
  166.             if( i==j or second_grp[j].size() <= 1 ) continue;
  167.             for( k=1;k<second_grp[i].size();k++ ){
  168.                 Long u = second_grp[i][k-1],v = second_grp[i][k];
  169.                 Long uI = lower_bound( ALL( second_grp[j] ), u ) - second_grp[j].begin();
  170.                 Long vI = upper_bound( ALL( second_grp[j] ), v ) - second_grp[j].begin();
  171.                 vI--;
  172.                 if( uI>= second_grp[j].size()) continue;
  173.                 else if( vI<0 ) continue;
  174.                 else if( uI>vI ) continue;
  175.                 ans2 -= nCr[second_grp[i].size()][2] * nCr[vI-uI+1][2];
  176.                 ans2 %= MOD;
  177.             }
  178.             for( k=1;k<second_grp[i].size();k++ ){
  179.                 Long v = second_grp[i][k];
  180.                 Long vI = lower_bound( ALL( second_grp[j] ), v ) - second_grp[j].begin();
  181.                 ans2 -= nCr[k+1][2] * nCr[second_grp[j].size()-vI][2];
  182.                 ans2 %= MOD;
  183.             }
  184.         }
  185.     }
  186.  
  187.     cout<<ans2<<endl;
  188.  
  189.  
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement