zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    课堂作业1
    懒人创造了方法
    四则运算
    动手动脑与原码反码补码
    java测试感受
    暑假进度报告四
    暑假进度报告三
    暑假进度报告二
    暑假进度报告一
    《大道至简》读后感
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8426096.html
Copyright © 2011-2022 走看看