zoukankan      html  css  js  c++  java
  • [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

     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 ", 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
  • 相关阅读:
    关于界面和UI
    Windows Form编程中的Command模式
    转载:从地理学透视中国现代化
    [3sNews, 关外飞雪]2005年3S业界盘点暨《3S新闻周刊》创刊题记
    Bridge? 一个GIS二次开发中常用的设计模式
    2005年GIS技术盘点
    [3sNews]建立GIS人自己的工会,抵制低薪无薪上岗
    2005国产空间信息系统软件测评结果揭晓
    从语义(semantic)GIS和知识表达谈起
    使用编译器来使用宏变量
  • 原文地址:https://www.cnblogs.com/Asm-Definer/p/4369516.html
Copyright © 2011-2022 走看看