hdu2227 树状数组

 1 #include<stdio.h>
 2 #include<string.h>
 3 #define MOD 1000000007
 4 __int64 ans[100005],c[100005];
 5 int a[100005],wei[100005],n;
 6 void sort(int l,int r)
 7 {
 8     int i,j,x,y,x1;
 9     x=a[(l+r)/2]; x1=wei[(l+r)/2];
10     i=l; j=r;
11     while (i<=j)
12     {
13         while (a[i]<x||(a[i]==x&&wei[i]<x1)) i++;
14         while (x<a[j]||(a[j]==x&&wei[j]>x1)) j--;
15         if (i<=j)
16         {
17             y=a[i]; a[i]=a[j]; a[j]=y;
18             y=wei[i]; wei[i]=wei[j]; wei[j]=y;
19             i++; j--;
20         }
21     }
22     if (i<r) sort(i,r);
23     if (l<j) sort(l,j); 
24 }
25 int lowbit(int x)
26 {
27     return(x&(-x)); 
28 }
29 __int64 sum(int x)
30 {
31     __int64 ret=0;
32     while (x>0){
33         ret=(ret+c[x])%MOD;
34         x-=lowbit(x);
35     }
36     return ret;
37 }
38 void add(int x,__int64 d)
39 {
40     while (x<=n){
41         c[x]=(c[x]+d)%MOD;
42         x+=lowbit(x);
43     }
44 }
45 int main()
46 {
47     int i;
48     __int64 aa;
49     while (~scanf("%d",&n))
50     {
51         for (i=1;i<=n;i++) 
52         {
53             scanf("%d",&a[i]);
54             ans[i]=1; wei[i]=i;
55         }
56         sort(1,n);
57         memset(c,0,sizeof(c));
58         for (i=1;i<=n;i++)
59         {
60             ans[wei[i]]=(1+sum(wei[i]-1))%MOD;
61             add(wei[i],ans[wei[i]]);
62             
63         }
64         printf("%I64d\n",sum(n));
65     }
66 }

http://acm.hdu.edu.cn/showproblem.php?pid=2227

posted on 2014-07-16 16:05  xiao_xin  阅读(107)  评论(0编辑  收藏  举报

导航