最大数(线段树)

题目

#include<bits/stdc++.h>
#define st static
#define re return
#define MAX(a,b) (a)>(b)?(a):(b)
#define inc(i,l,r) for(int i=l;i<=r;i++)
const int maxn=200005;


using namespace std;

char buf[1<<21],*p1=buf,*p2=buf;
inline int getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}

inline int red() 
{
    int x= 0,f = 0;char ch = getc();    
    while (!isdigit (ch)) {if (ch == '-') f = 1;ch = getc();}
    while (isdigit (ch)) {x = x* 10 + ch - 48;ch = getc();}
    return f?-x:x;
    
}

int n,last,k,t[maxn<<2],maxx[maxn<<2];

void insert(int l,int r,int x,int rt,int add)
{
    if(l==r&&r==x)
    {
        t[rt]=add;
        maxx[rt]=add;
        re;
    }
    
    int mid=(l+r)>>1;
    if(mid<x)insert(mid+1,r,x,rt<<1|1,add);
    else	 insert(l,mid,x,rt<<1,add);
    maxx[rt]=MAX(maxx[rt<<1|1],maxx[rt<<1]);
}

int ask(int l,int r,int x,int y,int rt)
{
    if(x<=l&&r<=y)re maxx[rt];
    int mid=(l+r)>>1;
    int a=-2147483364,b=-2147483364;
    if(mid<y)
        a=ask(mid+1,r,x,y,rt<<1|1);
    if(mid>=x)
        b=ask(l,mid,x,y,rt<<1);
    re MAX(a,b);
}

int main()
{
    //freopen("in.txt","r",stdin);
    int n,mod,tt=0;
    char c;long long vv;
    scanf("%d%d\n",&n,&mod);
    inc(i,1,n)
    {
        c=getchar();
        if(c=='A')
        {
            scanf("%lld\n",&vv);
            last++;
            insert(1,n,last,1,(vv+tt)%mod);
        }
        else 
        {
            scanf("%lld\n",&vv);
            tt=ask(1,n,last-vv+1,last,1);
            printf("%d\n",tt);
        }
    }
    re 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值