【线段树】P1198 [JSOI2008]最大数

 1 #include<iostream>
 2 #include<string>
 3 #include<queue>
 4 #include<stack>
 5 #include<vector>
 6 #include<map>
 7 #include<cstdio>
 8 #include<cstdlib>
 9 #include<algorithm>
10 #include<set>
11 #include<iomanip>
12 #include<cstring>
13 #include<cmath>
14 #include<limits>
15 using namespace std;
16 
17 #define au auto
18 #define debug(i) cout<<"debug: "<<i<<endl
19 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++)
20 #define mrep(i,a,b) for(register int i=(a);i>=(b);i--)
21 #define LLL __int128
22 #define Re register
23 #define il inline
24 #define mem(a,b) memset(a,(b),sizeof(a))
25 typedef pair<int, int> intpair;
26 typedef long long int LL;
27 const int INF = 0x3f3f3f3f;
28 const long long int INFLL = 0x3f3f3f3f3f3f3f3f;
29 
30 const int maxn = 2000010;
31 int n;
32 int m, mod;
33 int temp;
34 
35 LL a[maxn];
36 
37 inline int lc(int x)
38 {
39     return x << 1;
40 }
41 
42 inline int rc(int x)
43 {
44     return x << 1 | 1;
45 }
46 
47 void add(int x, int l, int r, int k)
48 {
49     if (l == r)
50     {
51         a[x] = k;
52         return;
53     }
54     int mid = (l + r) >> 1;
55     if (mid >= n) add(lc(x), l, mid, k);
56     if (mid < n) add(rc(x), mid + 1, r, k);
57     a[x] = max(a[lc(x)], a[rc(x)]) % mod;
58 }
59 
60 long long int query(int left, int right, int l, int r, int x)
61 {
62     long long int res = -INFLL;
63     if (left <= l && r <= right) return a[x];
64     int mid = (l + r) >> 1;
65     if (left <= mid) res = max(res, query(left, right, l, mid, lc(x)));
66     if (right > mid) res = max(query(left, right, mid + 1, r, rc(x)), res);
67     return res;
68 }
69 
70 int main()
71 {
72     cin.sync_with_stdio(0);
73     cin >> m >> mod;
74     char op;
75     long long int t;
76     mem(a, -0x3f);
77     mfor(i, 1, m)
78     {
79         cin >> op >> t;
80         //debug(op);
81         //debug(t);
82         if (op == 'A')
83         {
84             n++;
85             //debug(a[0]);
86             add(1, 1, m, (temp + t) % mod);
87             continue;
88         }
89         if (!t) cout << 0 << endl;
90         else cout << (temp = query(n - t + 1, n, 1, m, 1) % mod) << endl;
91     }
92 }
View Code

猜你喜欢

转载自www.cnblogs.com/thjkhdf12/p/11656643.html
今日推荐