[最大数MaxNumber] 树状数组

From: http://www.lydsy.com/JudgeOnline/problem.php?id=1012

Solution:

用树状数组维护最大值

/**************************************************************
    Problem: 1012
    User: leezy
    Language: C++
    Result: Accepted
    Time:500 ms
    Memory:1588 kb
****************************************************************/
 
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <functional>
#define N 200015
#define M 1000000
#define INF 0x6fffffff
#define MAX_LEVEL 32
#define NIL (0)
#define lowbit(i) ((i)&(-i))
int arr[N], s;
void update(int x){
    arr[++s] = 0;
    for (int i = s; i > 0; i -= lowbit(i) ) arr[i] = std::max(arr[i], x);
}
int query(int k){
    int ans = 0;
    for (int i = k; i <= s; i += lowbit(i) ) ans = std::max(ans, arr[i]);
    return ans;
}
int main()
{
    //const char path[] = "D:\\Project\\AlgorithmExam\\test.txt";
    //freopen(path, "r+", stdin);
 
    int n, d;
    while ( scanf("%d%d", &n, &d) != EOF ){
        char op[5];
        int t = 0, x;
        s = 0;
        for ( int i = 0; i < n; ++i ){
            scanf("%s%d", op, &x);
            if ( 'A' == op[0] ) update( (x + t)%d );
            else if ( 'Q' == op[0] ) printf("%d\n", t=query(s-x+1));
        }
    }
    return 0;
}
View Code

 

posted on 2013-12-07 14:31  leezyli  阅读(279)  评论(0编辑  收藏  举报

导航