洛谷 P1198 [JSOI2008]最大数

                         洛谷 P1198 [JSOI2008]最大数

题目描述

现在请求你维护一个数列,要求提供以下两种操作:

1、 查询操作。

语法:Q L

功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值。

限制: L不超过当前数列的长度。 (L>0)

2、 插入操作。

语法:A n

功能:将 n 加上 t ,其中 t 是最近一次查询操作的答案(如果还未执行过查询操作,则 t=0 ),并将所得结果对一个固定的常数 D 取模,将所得答案插入到数列的末尾。

限制: n是整数(可能为负数)并且在长整范围内。

注意:初始时数列是空的,没有一个数。

输入输出格式

输入格式:

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

接下来的 M 行,每行一个字符串,描述一个具体的操作。语法如上文所述。

输出格式:

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

输入输出样例

输入样例#1:  
5 100
A 96
Q 1
A 97
Q 1
Q 2
输出样例#1 :
96
93
96

说明

[JSOI2008]

本题数据已加强

题解:

线段树

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdio>
 4 using namespace std;
 5 const int N(1000005);
 6 int t,mod,n,x[N],ans;
 7 char ch,s[N];
 8 struct Tree {
 9     int l,r,sum;
10 } tr[N<<2];
11 #define lc (now<<1)
12 #define rc (now<<1|1)
13 #define mid (tr[now].l+tr[now].r>>1)
14 inline void Tree_up(int now) {
15     tr[now].sum=max(tr[lc].sum,tr[rc].sum);
16 }
17 void Tree_build(int now,int l,int r) {
18     tr[now].l=l;
19     tr[now].r=r;
20     if(l==r) return ;
21     Tree_build(lc,l,mid);
22     Tree_build(rc,mid+1,r);
23 }
24 void Tree_change(int now,int to,int x) {
25     if(tr[now].l==tr[now].r) {
26         tr[now].sum=x;
27         return ;
28     }
29     if(to<=mid) Tree_change(lc,to,x);
30     else Tree_change(rc,to,x);
31     Tree_up(now);
32 }
33 int Tree_query(int now,int l,int r) {
34     if(tr[now].l==l&&r==tr[now].r) return tr[now].sum;
35     if(r<=mid) return Tree_query(lc,l,r);
36     else if(l>mid) return Tree_query(rc,l,r);
37     else return max(Tree_query(lc,l,mid),Tree_query(rc,mid+1,r));
38 }
39 
40 int main() {
41     scanf("%d%d",&t,&mod);
42     for(int i=1; i<=t; i++) {
43         cin>>ch>>x[i];
44         s[i]=ch;
45         if(ch=='A') n++;
46     }
47     Tree_build(1,1,n);
48     int cnt=0;
49     for(int i=1; i<=t; i++) {
50         if(s[i]=='A')
51             Tree_change(1,++cnt,(ans+x[i])%mod);
52         else {
53             ans=Tree_query(1,cnt-x[i]+1,cnt);
54             printf("%d
",ans);
55         }
56     }
57     return 0;
58 }
View Code

一世安宁

原文地址:https://www.cnblogs.com/GTBA/p/9231270.html