|BZOJ 1012|线段树|[JSOI2008]最大数maxnumber

BZOJ传送门
水题一道,线段树维护即可。(省选题出模板?)

#include<cstdio>  
#include<algorithm>  
#include<cstring>  
#include<vector>
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
const int MAXM = 200000+5;
int m,d;
#define lc (o<<1)
#define rc (o<<1|1)
#define M ((l+r)>>1)
struct st
{
    int maxv[MAXM*4];
    void pushup(int o)
    {
        maxv[o] = max(maxv[lc], maxv[rc]); 
    }
    void build(int o, int l, int r)
    {
        maxv[o] = 0;
        if (l==r) return ;
        build(lc, l, M);
        build(rc, M+1,r);
    }
    void update(int o, int l, int r, int p, int v)
    {
        if (l==r)
        {
            maxv[o] += v;
            return ;
        }
        if (p<=M) update(lc, l, M, p, v);
        else if (M<p) update(rc, M+1, r, p, v);
        pushup(o);
    }
    int query(int o, int l, int r, int x, int y)
    {
        if (x<=l&&r<=y)
        {
            return maxv[o];
        }
        int ans = -1;
        if (x<=M) ans = max(query(lc, l, M, x, y), ans);
        if (M<y) ans = max(query(rc, M+1, r, x, y), ans);
        return ans;
    }
}tree;
int getc()
{
    char ans;
    while (true)
    {
        ans = getchar();
        if (ans=='A') return 0;
        else if (ans=='Q') return 1;
    }
}
int main()  
{  
    scanf("%d%d", &m, &d);
    tree.build(1,1,m);
    int last = 0;
    int cnt = 0;
    for (int i=1;i<=m;i++)
    {
        int opt = getc();
        int x;
        scanf("%d", &x);
        if (opt==0)
        {
            cnt++;
            x = (x+last)%d;
            tree.update(1,1,m,cnt, x);
        } else
        {
            printf("%d\n", last = tree.query(1,1,m,cnt-x+1,cnt));
        }
    }
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值