luoguP1198 [JSOI2008] The maximum number of

\ (\ Theta (nlog_2n) \ ) algorithm is a tree line, not discussed here.

So how do you write \ (\ Theta (n \ alpha (n)) \) algorithm do?

We can engage in a monotonous stack.

Each addition of a number. Just stack val value is greater than deleting his number, and the number of points on his father's position which corresponds to the current position, with disjoint-set to maintain it.

#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define il inline
#define rg register
#define gi read<int>
#define pii pair<int,int>
using namespace std;
typedef long long ll;
const int O = 2e5 + 10, inf = (1 << 31) - 1;
template<class TT>
il TT read() {
    TT o = 0,fl = 1; char ch = getchar();
    while (!isdigit(ch) && ch != '-') ch = getchar();
    if (ch == '-') fl = -1, ch = getchar();
    while (isdigit(ch)) o = o * 10 + ch - '0', ch = getchar();
    return fl * o;
}
char ch[2];
ll x;
int t, m, n, mod, fa[O], a[O];
stack<pii >s;
il int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int main() {
    m = gi(), mod = gi();
    while (m--) {
        scanf("%s %lld", ch, &x);
        if (ch[0] == 'A') {
            (x += t) %= mod;
            fa[++n] = n;
            a[n] = x;
            while (!s.empty() && x > s.top().first) fa[s.top().second] = n, s.pop();
            s.push(pii(x, n));
        }
        else printf("%d\n", t = (x ? a[find(n - x + 1)] : 0));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/lylyl/p/11688681.html