[JSOI2008]最大数maxnumber

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

用的是并查集,然后用了路径压缩。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<string>
 6 #include<fstream>
 7 using namespace std;
 8 //ifstream cin("cin.in");ofstream fout("test.out");
 9 
10 long long m,mod,f[200005],d[200005],t=0,size=0;
11 
12 int cind(int x){
13     if(x==f[x]) return x;
14     int fx=cind(f[x]);
15     if(d[x]<d[f[x]]) d[x]=d[f[x]];  //小心观看 
16     f[x]=fx;
17     return fx;
18     }
19 
20 void Q(int x){
21      int z=size-x+1;
22      cind(z);
23      t=d[z];
24      cout<<d[z]<<endl;
25      }
26 
27 int main()
28 {
29     cin>>m>>mod;
30     char s;int n;
31     for(int i=1;i<=m;++i)
32     {
33       cin>>s>>n;
34       if(s=='Q') Q(n);
35       else {size++;f[size-1]=size;f[size]=size;d[size]=(t+n)%mod;}
36             }
37    // system("pause");
38     return 0;
39     
40     } 
原文地址:https://www.cnblogs.com/noip/p/2969865.html