P1198 最大数 线段树水题

这道题模拟一下可以过,但是我们发现线段树也可以安全水过......

写的线段树只需要滋磁单点修改,区间求max即可

我一开始犯了一个很SB的错误:每次插入修改了t,然后疯狂爆0到怀疑人生...

而且我写的线段树还不明不白的碾了胡雨菲几年前写的。

 #include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
const int N = ;
typedef long long LL;
LL mo;
LL Max[N<<]; void build(int l,int r,int o)
{
Max[o]=-;
if(l==r)
{
return;
}
int mid=(l+r)>>;
build(l,mid,o<<);
build(mid+,r,o<<|);
return;
} void add(int x,int v,int l,int r,int o)
{
//printf("add:x=%d v=%d l=%d r=%d o=%d\n",x,v,l,r,o);
if(l==x&&r==x)
{
//printf("Max[%d]=%d\n",o,v);
Max[o]=v;
return;
}
int mid=(l+r)>>;
if(x<=mid)add(x,v,l,mid,o<<);
else add(x,v,mid+,r,o<<|);
Max[o]=max(Max[o<<],Max[o<<|]);
return;
} LL ask(int L,int R,int l,int r,int o)
{
if(L<=l&&r<=R) return Max[o];
if(R<l||r<L) return -;
int mid=(l+r)>>;
return max(ask(L,R,l,mid,o<<),ask(L,R,mid+,r,o<<|));
} int main()
{
LL m,top=,x,t=;char flag;
scanf("%lld%lld",&m,&mo);
build(,m,);
for(int i=;i<=m;i++)
{
cin>>flag>>x;
if(flag=='Q')
{
if(!x) {printf("0\n");continue;}
t=ask(top-x,top-,,m,);
printf("%lld\n",t);
}
else
{ add(top,(t+x)%mo,,m,);
top++;
}
}
return ;
}

AC代码:

上一篇:Python 防止mysql 注入的两种方式


下一篇:javascript遍历子元素