zoj 3279 树状数组

其实就是找一个最小的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 }
时间: 2024-10-15 09:09:57

zoj 3279 树状数组的相关文章

ZOJ 3635 树状数组+二分

这题那时怎么想就是想不出来--而且今晚没有多大状态,自己都晕了--一题没做出来-- baoge解释好久才懂--唉--线段树,树状数组用得还是不够熟啊-- WA了二发,才知道二分错了,二分好久不用,老是出错了现在-- #include<iostream> #include<cstring> #include<string> #include<cstdio> #define sca(a) scanf("%d",&a) #define

ZOJ 2112 Dynamic Rankings(主席树套树状数组+静态主席树)

题意:给定一个区间,求这个区间第k大的数,支持单点修改. 思路:主席树真是个神奇的东西.........速度很快但是也有一个问题就是占用内存的很大,一般来说支持单点修改的主席树套树状数组空间复杂度为O(n*logn*logn), 如果查询较少的话,可以初始的时候用一颗静态主席树,这样空间复杂度可以降为O(n*logn+q*logn*logn),勉强可以过zoj这道题. 这道题看了好久好久才懂...网上题解一般就几句话.......下面是自己的一些理解. 首先静态主席树这个东西其实比较好懂,就是对

zoj 3635 Cinema in Akiba (树状数组求第K大)

Cinema in Akiba Cinema in Akiba (CIA) is a small but very popular cinema in Akihabara. Every night the cinema is full of people. The layout of CIA is very interesting, as there is only one row so that every audience can enjoy the wonderful movies wit

ZOJ - 3635 Cinema in Akiba(树状数组+二分)

题意:已知有n个人,从第一个人开始每个人被安排在第ai个空座上,有m组询问,问某人所坐的位置. 分析: 1.用树状数组维护空座的个数,方法: 将所有的空座初始化为1,sum(x)则表示从座位1到座位x空座的个数. 2.对于每个人,根据sum(mid),二分找使sum(mid)大于等于a[i]的最小的mid,即第ai个空座的位置,并将该位置加上-1,则该位置的值变为0,从而不参与空座数的统计. 3.vis[q]即为标号为q的人所坐的位置. #include<cstdio> #include<

zoj 2112 Dynamic Rankings(树状数组套主席树)

题意:对于一段区间,每次求[l,r]的第k大,存在单点修改操作: 思路: 学习主席树参考: http://blog.csdn.net/wjf_wzzc/article/details/24560117(各种形式) http://blog.csdn.net/bossup/article/details/31921235(推荐) http://blog.csdn.net/xiaofengcanyuexj/article/details/25553521?utm_source=tuicool(图解)

ZOJ 2112 Dynamic Rankings(树状数组+主席树)

The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with the query like to simply find the k-th smallest number of the given N numbers. They have developed a more powerful system such that for N numbers a[1],

[hdu1394]Minimum Inversion Number(树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18395    Accepted Submission(s): 11168 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 1394 Minimum Inversion Number (树状数组求逆序数)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13942    Accepted Submission(s): 8514 Problem Description The inversion number of a given number sequence a1, a2, ..., a

hdu 1394(树状数组)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16948    Accepted Submission(s): 10292 Problem Description The inversion number of a given number sequence a1, a2, ..., a