[JSOI 2008]最大数

Description

题库链接

给你一个序列,初始为空。资瓷下列操作:

  1. 在序列末尾加上一个数;
  2. 查询后 \(L\) 个数中的最大值。

操作总数为 \(m\)\(1\leq m\leq 200000\)

Solution

单调栈的板子。

Code

//It is made by Awson on 2018.2.7
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 200000;
void read(int &x) {
    char ch; bool flag = 0;
    for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }

int n, d, t, x, S[N+5], top, a[N+5], cnt;
char ch[5];

int divide(int x) {
    int l = 1, r = top, ans;
    while (l <= r) {
        int mid = (l+r)>>1;
        if (S[mid] >= x) r = mid-1, ans = mid;
        else l = mid+1;
    }
    return ans;
}
void work() {
    scanf("%d%d", &n, &d);
    for (int i = 1; i <= n; i++) {
        scanf("%s%d", ch, &x);
        if (ch[0] == 'A') {
            a[++cnt] = x = (1ll*x+1ll*t)%d;
            while (top >= 1 && a[S[top]] <= x) --top;
            S[++top] = cnt;
        }else writeln(t = (x == 0 ? 0 : a[S[divide(cnt-x+1)]]));
    }
}
int main() {
    work(); return 0;
}

转载于:https://www.cnblogs.com/NaVi-Awson/p/8426096.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值