BZOJ 1012 最大数maxnumber

来源:互联网 发布:游戏美工培训费用 编辑:程序博客网 时间:2024/04/27 20:19

Description

现在请求你维护一个数列,要求提供以下两种操作: 1、 查询操作。语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。限制:L不超过当前数列的长度。 2、 插入操作。语法:A n 功能:将n加上t,其中t是最近一次查询操作的答案(如果还未执行过查询操作,则t=0),并将所得结果对一个固定的常数D取模,将所得答案插入到数列的末尾。限制:n是非负整数并且在长整范围内。注意:初始时数列是空的,没有一个数。

Input

第一行两个整数,M和D,其中M表示操作的个数(M <= 200,000),D如上文中所述,满足(0

Output

对于每一个查询操作,你应该按照顺序依次输出结果,每个结果占一行。

Sample Input

5 100
A 96
Q 1
A 97
Q 1
Q 2

Sample Output

96
93
96


//RE的话改用scanf, printf就好了....#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <list>#include <deque>#include <queue>#include <cctype>#include <map>#include <set>#include <bitset>#include <functional>#include <numeric>#include <utility>#include <sstream>#include <iomanip>#include <cstdlib>#include <ctime>#include <cassert>#include <limits>#include <fstream>using namespace std;#define mem(A, X) memset(A, X, sizeof A)#define pb(x) push_back(x)#define mp(x,y) make_pair((x),(y))#define vi vector<int>#define all(x) x.begin(), x.end()#define foreach(e,x) for(__typeof(x.begin()) e=x.begin();e!=x.end();++e)#define sz(x) (int)((x).size())#define sl(a) strlen(a)#define rep(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))#define Rep(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))#define min3(a,b,c) min(a,min(b,c))#define max3(a,b,c) max(a,max(b,c))#define dbg(a) cout << a << endl;#define fi first#define se secondtypedef long long int64;int gcd(const int64 &a, const int64 &b) { return b == 0 ? a : gcd(b, a % b); }int64 int64pow(int64 a, int64 b){ if (b == 0) return 1; int64 t = int64pow(a, b / 2); if (b % 2) return t * t * a; return t * t; }const int inf = 0x7fffffff;const double eps = 1e-8;const double pi = acos(-1.0);const int MAX_N = 200005;int n, mod;struct node {    int l, r, MAX;};node tree[MAX_N << 2];void build(int root, int l, int r){    tree[root].l = l; tree[root].r = r;    if (l == r) {        tree[root].MAX = -inf;        return;    }    int mid = (l + r) >> 1;    build(root << 1, l, mid);    build(root << 1 | 1, mid + 1, r);}void update(int root, int b, int e){    int l = tree[root].l, r = tree[root].r;    if (l == r) {        tree[root].MAX = e;        return;    }    int mid = (l + r) >> 1;    if (b <= mid) {        update(root << 1, b, e);    }    else {        update(root << 1 | 1, b, e);    }    tree[root].MAX = max(tree[root << 1].MAX, tree[root << 1 | 1].MAX);}int query(int root, int b, int e){    int l = tree[root].l, r = tree[root].r;    if (l == b && r == e) {        return tree[root].MAX;    }    int mid = (l + r) >> 1;    if (e <= mid) {        return query(root << 1, b, e);    }    else if (b > mid) {        return query(root << 1 | 1, b, e);    }    else {        return max(query(root << 1, b, mid), query(root << 1 | 1, mid + 1, e));    }}void work(){    build(1, 1, n);    char C[5];    int x, cnt = 0, last = 0;    rep(i, 0, n) {        scanf("%s", C);        if (C[0] == 'A') {            scanf("%d", &x);            ++cnt;            x = (x + last) % mod;            update(1, cnt, x);        }        else {            scanf("%d", &x);            last = query(1, cnt - x + 1, cnt);            printf("%d\n", last);        }    }}int main(){    while (~scanf("%d%d", &n, &mod)) {        work();    }    return 0;}



0 0
原创粉丝点击