首页 > 编程语言 > 详细

zoj 3279 树状数组

时间:2015-08-18 21:14:52      阅读:257      评论:0      收藏:0      [点我收藏+]

其实就是找一个最小的level k使得level 1到k的ant数量和不小于rank。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 const int N = 100001;
 7 int a[N];
 8 int c[N];
 9 int n, m;
10 
11 int lb( int i )
12 {
13     return i & -i;
14 }
15 
16 void update( int i, int v )
17 {
18     while ( i <= n )
19     {
20         c[i] += v;
21         i += lb(i);
22     }
23 }
24 
25 int kth( int k )
26 {
27     int ans = 0, cnt = 0;
28     for ( int i = 16; i >= 0; i-- )
29     {
30         ans += ( 1 << i );
31         if ( ans >= n || cnt + c[ans] >= k )
32         {
33             ans -= ( 1 << i );
34         }
35         else
36         {
37             cnt += c[ans];
38         }
39     }
40     return ans + 1;
41 }
42 
43 int main ()
44 {
45     while ( scanf("%d", &n) != EOF )
46     {
47         memset( c, 0, sizeof(c) );
48         for ( int i = 1; i <= n; i++ )
49         {
50             scanf("%d", a + i);
51             update( i, a[i] );            
52         }
53         scanf("%d", &m);
54         while ( m-- )
55         {
56             char op[2];
57             int x, y;
58             scanf("%s", op);
59             if ( op[0] == q )
60             {
61                 scanf("%d", &x);
62                 printf("%d\n", kth(x));
63             }
64             else
65             {
66                 scanf("%d%d", &x, &y);
67                 update( x, y - a[x] );
68                 a[x] = y;
69             }
70         }
71     }
72     return 0;
73 }

 

zoj 3279 树状数组

原文:http://www.cnblogs.com/huoxiayu/p/4740466.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!