# Luogu P1198 BZOJ 1012 最大数 (线段树)

Luogu 同时被 3 个专栏收录
9 篇文章 0 订阅
12 篇文章 0 订阅
5 篇文章 0 订阅

(1) Q L表示查询当前数列后L个数的最大值。

(2) A N表示在当前数列末尾添加一个新数，这个新数的值等于上一次Q操作的答案加上给定的参数N. 若之前未进行Q操作，则添加的数为N.

(Luogu: Time: 712 MS; Memory: 12.05 MB; Code: 2.04 KB)

(BZOJ: Time: 1088 MS; Memory: 13324 KB; Code: 2333 B)

#include<cstdio>
#include<algorithm>
using namespace std;

const int MAXN = 2e5;
int MODN;
struct Node
{
int left,right;
int maxi;
};
struct SegmentTree
{
Node nd[MAXN*4+2];

void init()
{
for(int i=1; i<=MAXN*4; i++)
{
nd[i].left = nd[i].right = nd[i].maxi = 0;
}
}

void build(int lbound,int rbound,int pos,int a[])
{
nd[pos].left = lbound;
nd[pos].right = rbound;
if(lbound==rbound)
{
nd[pos].maxi = a[lbound];
return;
}
int mid = (lbound+rbound)/2;
build(lbound,mid,2*pos,a);
build(mid+1,rbound,2*pos+1,a);
nd[pos].maxi = max(nd[2*pos].maxi,nd[2*pos+1].maxi);
}

void modify(int bound,int val,int pos)
{
int mid = (nd[pos].left+nd[pos].right)/2;
if(nd[pos].left==nd[pos].right)
{
nd[pos].maxi = val;
return;
}
if(bound<=mid) modify(bound,val,2*pos);
else modify(bound,val,2*pos+1);
nd[pos].maxi = max(nd[2*pos].maxi,nd[2*pos+1].maxi);
}

int query(int lbound,int rbound,int pos)
{
int mid = (nd[pos].left+nd[pos].right)/2;
if(lbound==nd[pos].left && rbound==nd[pos].right) return nd[pos].maxi;
int ans;
if(rbound<=mid) ans = query(lbound,rbound,2*pos);
else if(lbound>mid) ans = query(lbound,rbound,2*pos+1);
else ans = max(query(lbound,mid,2*pos),query(mid+1,rbound,2*pos+1));
return ans;
}
};
SegmentTree st;
int a[MAXN+2];
int p[MAXN+2];
int ql[MAXN+2];
int qr[MAXN+2];
int n,m,q;

int main()
{
char ch[5];
int x;
n = q = 0;
scanf("%d%d",&m,&MODN);
for(int i=1; i<=m; i++)
{
scanf("%s",ch);
switch(ch[0])
{
case 'A':
{
scanf("%d",&x);
a[++n] = x;
p[n] = q;
break;
}
case 'Q':
{
scanf("%d",&x);
ql[++q] = n-x+1;
qr[q] = n;
break;
}
}
}
st.init();
st.build(1,n,1,a);
int j = 1;
while(j<=n && !p[j])
{
j++;
}
for(int i=1; i<=q; i++)
{
int ans = st.query(ql[i],qr[i],1);
printf("%d\n",ans);
while(j<=n && p[j]==i)
{
st.modify(j,(int)((long long)a[j]+ans)%MODN,1);
j++;
}
}

return 0;
}

• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

11-11 528
07-28 722

03-31 348
02-23 109
05-10 95
07-30 312
10-13 16
05-28 111