BZOJ 1012 最大数maxnumber 线段树

题目链接:

https://www.lydsy.com/JudgeOnline/problem.php?id=1012

题目大意:

见链接

思路:

直接用线段树模拟一下就可以了。

 1 #include<bits/stdc++.h>
 2 #define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
 3 #define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
 4 #define Min(a, b) ((a) < (b) ? (a) : (b))
 5 #define Mem(a) memset(a, 0, sizeof(a))
 6 #define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
 7 #define MID(l, r) ((l) + ((r) - (l)) / 2)
 8 #define lson ((o)<<1)
 9 #define rson ((o)<<1|1)
10 #pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
11 using namespace std;
12 inline int read()
13 {
14     int x=0,f=1;char ch=getchar();
15     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
16     while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
17     return x*f;
18 }
19 
20 typedef long long ll;
21 const int maxn = 1000000 + 10;
22 const int maxm = 1000000 + 10;
23 const int mod = 1000000007;//const引用更快,宏定义也更快
24 const int INF = 1000000007;
25 struct node
26 {
27     ll l, r, x;
28 }tree[maxn];
29 void build(int o, ll l, ll r)
30 {
31     tree[o].l = l, tree[o].r = r;
32     if(l == r)return;
33     ll m = MID(l, r);
34     build(lson, l, m);
35     build(rson, m + 1, r);
36 }
37 ll p, v;
38 void update(int o)
39 {
40     if(tree[o].l == tree[o].r)
41     {
42         tree[o].x = v;
43         return;
44     }
45     if(p <= tree[lson].r)update(lson);
46     else update(rson);
47     tree[o].x = Max(tree[lson].x, tree[rson].x);
48 }
49 ll ql, qr;
50 ll ans;
51 void query(int o)
52 {
53     if(ql <= tree[o].l && qr >= tree[o].r)
54     {
55         ans = Max(ans, tree[o].x);
56         return;
57     }
58     if(ql <= tree[lson].r)query(lson);
59     if(qr >= tree[rson].l)query(rson);
60 }
61 int main()
62 {
63     ll n, d;
64     scanf("%lld%lld", &n, &d);
65     build(1, 1, n);
66     p = 1;
67     ll t = 0, x;
68     char s[10];
69     while(n--)
70     {
71         scanf("%s%lld", s, &x);
72         if(s[0] == 'A')
73         {
74             v = t + x;
75             v %= d;
76             update(1);
77             p++;
78         }
79         else
80         {
81             ql = p - x;
82             qr = p - 1;
83             ans = 0;
84             query(1);
85             t = ans;
86             printf("%lld\n", ans);
87         }
88     }
89     return 0;
90 }

 

posted @ 2018-09-10 23:07  _努力努力再努力x  阅读(169)  评论(0编辑  收藏  举报