fzu 1962 树状数组

优雅的树状数组!人们发明了复杂度为logn的求解第k小的方法,常数小且代码量小,实在是用来求解本类题目的最佳方法,可惜的是我的Treap超时了......

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5
 6 const int N = 500001;
 7 int c[N];
 8 int n, m, maxn;
 9
10 int lb( int i )
11 {
12     return i & -i;
13 }
14
15 void update( int i, int v )
16 {
17     while ( i <= n )
18     {
19         c[i] += v;
20         i += lb(i);
21     }
22 }
23
24 int sum( int i )
25 {
26     int ans = 0;
27     while ( i )
28     {
29         ans += c[i];
30         i -= lb(i);
31     }
32     return ans;
33 }
34
35 int kth( int k )
36 {
37     int ans = 0, cnt = 0;
38     for ( int i = 20; i >= 0; i-- )
39     {
40         ans += ( 1 << i );
41         if ( ans >= maxn || cnt + c[ans] >= k )
42         {
43             ans -= ( 1 << i );
44         }
45         else
46         {
47             cnt += c[ans];
48         }
49     }
50     return ans + 1;
51 }
52
53 int main ()
54 {
55     while ( scanf("%d%d", &n, &m) != EOF )
56     {
57         memset( c, 0, sizeof(c) );
58         for ( int i = 1; i <= n; i++ )
59         {
60             update( i, 1 );
61         }
62         maxn = n;
63         while ( m-- )
64         {
65             char op[2];
66             int num;
67             scanf("%s%d", op, &num);
68             if ( op[0] == ‘L‘ )
69             {
70                 int kk = kth(num);
71                 update( kk, -1 );
72             }
73             else if ( op[0] == ‘R‘ )
74             {
75                 if ( sum(num) == sum( num - 1 ) )
76                 {
77                     update( num, 1 );
78                 }
79             }
80             else if ( op[0] == ‘Q‘ )
81             {
82                 int kk = kth(num);
83                 printf("%d\n", kk);
84             }
85         }
86     }
87     return 0;
88 }
时间: 2024-10-10 17:25:03

fzu 1962 树状数组的相关文章

FZU 2277 树状数组

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2277  Problem Description There is a rooted tree with n nodes, number from 1-n. Root's number is 1.Each node has a value ai. Initially all the node's value is 0. We have q operations. There are two kinds of

FZU 2176 easy problem (DFS序+树状数组)

对于一颗树,dfs遍历为每个节点标号,在进入一个树是标号和在遍历完这个树的子树后标号,那么子树所有的标号都在这两个数之间,是一个连续的区间.(好神奇~~~) 这样每次操作一个结点的子树时,在每个点的开始结束两个点标记一下就可以,用后缀数组求前缀和就可知道每个点的值. 这道题虽然很麻烦(dep[y]-dep[x])%k .但是注意到K很小(1<=k<=5),可以维护k个树状数组. 提交时编译器选GUN C++迷之RE...换Visual C++ #include <cstdio> #

FZU 2225 小茗的魔法阵 扫描线+树状数组

这个题和一个CF上的找"Z"的题差不多,都是扫描线+树状数组 从右上角的主对角线开始扫描,一直扫到左下角,每次更新,右延伸等于该扫描线的点,注意在其所在的树状数组更新就好了 时间复杂度O(n^2logn) #include <stdio.h> #include <iostream> #include <vector> #include <math.h> #include <set> #include <map> #

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

hdu 5775 Bubble Sort(2016 Multi-University Training Contest 4——树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 636    Accepted Submission(s): 378 Problem Description P is a permutation of the

HDU 5775 树状数组

Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 853    Accepted Submission(s): 504 Problem Description P is a permutation of the integers from 1 to N(index starting from 1).Here is t

HDU 5775 Bubble Sort(树状数组)

题目链接:HDU 5775 题面: Bubble Sort Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 709    Accepted Submission(s): 418 Problem Description P is a permutation of the integers from 1 to N(index startin

代码与算法集锦-归并排序+树状数组+快排+深度优先搜索+01背包(动态规划)

归并排序 求逆序数 归并排序是建立在归并操作上的一种有效的排序算法.该算法是采用分治法(Divide and Conquer)的一个非常典型的应用. 首先考虑下如何将将二个有序数列合并.这个非常简单,只要从比较二个数列的第一个数,谁小就先取谁,取了后就在对应数列中删除这个数.然后再进行比较,如果有数列为空,那直接将另一个数列的数据依次取出即可. //将有序数组a[]和b[]合并到c[]中 void MemeryArray(int a[], int n, int b[], int m, int c

HDU 5542 The Battle of Chibi dp+树状数组

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5542 题意:给你n个数,求其中上升子序列长度为m的个数 可以考虑用dp[i][j]表示以a[i]结尾的长度为j的上升子序列有多少 裸的dp是o(n2m) 所以需要优化 我们可以发现dp的第3维是找比它小的数,那么就可以用树状数组来找 这样就可以降低复杂度 #include<iostream> #include<cstdio> #include<cstring> #include