[JSOI 2008]最大数

Description

题库链接

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

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

操作总数为 (m)(1leq mleq 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('
'))
#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