[bzoj1012](JSOI2008)最大数maxnumber(Fenwick Tree)

Description

现在请求你维护一个数列,要求提供以下两种操作: 1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。 2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的 末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。

Input

第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足(0

Output

对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。

Sample Input

5 100
A 96
Q 1
A 97
Q 1
Q 2

Sample Output

96
93
96

分析

     由于区间min/max信息不可减,这里似乎不可能直接用树状数组实现。但我们发现,这里的插入和查询都是在末尾进行的,如果我们把数组倒过来,每次在前面插入一个数,或者查询一个前缀数组的最值,就只需要利用最值的可合并性质就可以实现了。

     ——本来以为我这个做法爆掉了线段树还可以排进前几名来着……然而衡中的ztx同学却发现这个数组的最值信息可以直接用一个单调数组(栈)维护,而查询操作只要在数组上二分查找L就可以了……给跪了Orzorzorz

 

ExpandedBlockStart.gif
 1  /* *************************************************************
 2      Problem: 1012
 3      User: AsmDef
 4      Language: C++
 5      Result: Accepted
 6      Time:364 ms
 7      Memory:2836 kb
 8  *************************************************************** */
 9  
10  // Asm_Def
11  #include <iostream>
12 #include <cctype>
13 #include <cstdio>
14 #include <vector>
15 #include <algorithm>
16 #include <cmath>
17 #include <queue>
18  using  namespace std;
19 template<typename T>inline  void getd(T &x){
20      char c = getchar();
21      bool minus =  0;
22      while(!isdigit(c) && c !=  ' - ')c = getchar();
23      if(c ==  ' - ')minus =  1, c = getchar();
24     x = c -  ' 0 ';
25      while(isdigit(c = getchar()))x = x *  10 + c -  ' 0 ';
26      if(minus)x = -x;
27 }
28  /* ====================================================== */
29  const  int maxn =  200002;
30 typedef  long  long LL;
31 inline  int lowbit( int x){ return x & -x;}
32  struct Fenwick{
33     LL A[maxn];
34      int iter;
35     Fenwick():iter(maxn -  1){}
36     inline  void insert(LL x){
37         A[iter] = x;
38          int i = iter + lowbit(iter);--iter;
39          while(i < maxn){
40              if(x > A[i])A[i] = x;
41             i += lowbit(i);
42         }
43     }
44     inline  int getmax( int L){
45          int i = iter + L, ans =  0;
46          while(i >= iter){
47              if(ans < A[i])ans = A[i];
48             i ^= lowbit(i);
49         }
50          return ans;
51     }
52 }BIT;
53  int main(){
54      #if defined DEBUG
55     freopen( " test "" r ", stdin);
56      #else
57      // freopen("bzoj_1012.in", "r", stdin);
58       // freopen("bzoj_1012.out", "w", stdout);
59       #endif
60     LL D, x, t =  0;
61      int M, opt;
62     getd(M), getd(D);
63      while(M--){
64          while(!isalpha(opt = getchar()));
65         getd(x);
66          if(opt ==  ' Q ')
67             printf( " %d\n ", t = BIT.getmax(x));
68          else BIT.insert((t + x) % D);
69     }
70      
71      
72      #if defined DEBUG
73     cout << endl << ( double)clock()/CLOCKS_PER_SEC << endl;
74      #endif
75      return  0;
76 }
Fenwick Tree

 

转载于:https://www.cnblogs.com/Asm-Definer/p/4369516.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值