【BZOJ 1012】 [JSOI2008]最大数maxnumber(单调队列做法)

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1012

【题意】

【题解】

后加入的元素,如果比之前的元素大,
那么之前的元素比它小的元素,就不可能构成某个询问的答案了;
因为它会被放在最后一个位置;
所以维护一个值单调递减的队列;
显然这个值单调递减的队列,它的值对应的下标是单调递增的;
我们每次只要选取len-l+1..len这个范围里面的最大值就好;
又值是单调递减的;
所以选取的值应尽量靠近len-l+1;
用个lower_bound就好;

【完整代码】

/**************************************************************
    Problem: 1012
    User: chengchunyang
    Language: C++
    Result: Accepted
    Time:572 ms
    Memory:4416 kb
****************************************************************/

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+100;

LL a[N],d,lastans = 0,ne_w,dl[N],len = 0;
int m,tail = 0;

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    rei(m), rel(d);
    rep1(i, 1, m)
    {
        char key; LL t;
        key = getchar();
        scanf("%c %lld", &key, &t);
        if (key == 'A')
        {
            ne_w = (t + lastans) % d;
            a[++len] = ne_w;
            while (1 <= tail && a[dl[tail]] <= ne_w) tail--;
            dl[++tail] = len;
        }
        else
        {
            lastans = lower_bound(dl + 1, dl + 1 + tail, len - t + 1) - dl;
            lastans = a[dl[lastans]];
            printf("%lld\n", lastans);
        }
    }
    return 0;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(87)  评论(0编辑  收藏  举报