BZOJ 1012 最大数maxnumber 线段树

题目链接:

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

题目大意:

见链接

思路:

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

 #include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false);//不可再使用scanf printf
#define Max(a, b) ((a) > (b) ? (a) : (b))//禁用于函数,会超时
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Mem(a) memset(a, 0, sizeof(a))
#define Dis(x, y, x1, y1) ((x - x1) * (x - x1) + (y - y1) * (y - y1))
#define MID(l, r) ((l) + ((r) - (l)) / 2)
#define lson ((o)<<1)
#define rson ((o)<<1|1)
#pragma comment(linker, "/STACK:102400000,102400000")//栈外挂
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} typedef long long ll;
const int maxn = + ;
const int maxm = + ;
const int mod = ;//const引用更快,宏定义也更快
const int INF = ;
struct node
{
ll l, r, x;
}tree[maxn];
void build(int o, ll l, ll r)
{
tree[o].l = l, tree[o].r = r;
if(l == r)return;
ll m = MID(l, r);
build(lson, l, m);
build(rson, m + , r);
}
ll p, v;
void update(int o)
{
if(tree[o].l == tree[o].r)
{
tree[o].x = v;
return;
}
if(p <= tree[lson].r)update(lson);
else update(rson);
tree[o].x = Max(tree[lson].x, tree[rson].x);
}
ll ql, qr;
ll ans;
void query(int o)
{
if(ql <= tree[o].l && qr >= tree[o].r)
{
ans = Max(ans, tree[o].x);
return;
}
if(ql <= tree[lson].r)query(lson);
if(qr >= tree[rson].l)query(rson);
}
int main()
{
ll n, d;
scanf("%lld%lld", &n, &d);
build(, , n);
p = ;
ll t = , x;
char s[];
while(n--)
{
scanf("%s%lld", s, &x);
if(s[] == 'A')
{
v = t + x;
v %= d;
update();
p++;
}
else
{
ql = p - x;
qr = p - ;
ans = ;
query();
t = ans;
printf("%lld\n", ans);
}
}
return ;
}
上一篇:VB.NET TextBox 只允许输入1-100之间的整数 简洁篇


下一篇:二、UITableView和它的亲戚们