Qiuqiqiu  
不管道路多么崎岖坎坷,我永远不停下追逐梦想的脚步!

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

View Code
 1 #include <cstdio>
 2 #include <cstring>
 3 using namespace std;
 4 
 5 typedef __int64 LL;
 6 const int N=270000;
 7 int c[N];
 8 int lowbit(int x)
 9 {
10     return x&(-x);
11 }
12 void add(int x,int p,int maxn)
13 {
14     for(int i=p;i<=maxn;i+=lowbit(i)) c[i]+=x;
15 }
16 int sum(int x)
17 {
18     int s=0;
19     for(int i=x;i>0;i-=lowbit(i)) s+=c[i];
20     return s;
21 }
22 int lowbound(int x,int l,int r)
23 {
24     while(l<r)
25     {
26         int m=(l+r)/2;
27         if(sum(m)<x) l=m+1;
28         else r=m;
29     }
30     return l;
31 }
32 int main()
33 {
34     int T,C=0;
35     scanf("%d",&T);
36     while(T--)
37     {
38         memset(c,0,sizeof(c));
39         int n,k;
40         scanf("%d%d",&n,&k);
41         for(int i=1;i<=n;i++) add(1,i,n);
42         LL ans=0;
43         while(k--)
44         {
45             int x;
46             scanf("%d",&x);
47             int p=lowbound(x,1,n);
48             ans+=p;
49             add(-1,p,n);
50         }
51         printf("Case %d: %I64d\n",++C,ans);
52     }
53     return 0;
54 }

 

posted on 2012-04-22 14:43  Qiuqiqiu  阅读(178)  评论(0编辑  收藏  举报