【BZOJ 1012】 [JSOI2008]最大数maxnumber(线段树做法)

【题目链接】:http://www.lydsy.com/JudgeOnline/problem.php?id=1012

【题意】

【题解】

预开一个20W长度的线段树;
这里a[1..20W]={0};
每次对单个点进行修改操作;
然后返回区间的最大值;

【完整代码】

/**************************************************************
    Problem: 1012
    User: chengchunyang
    Language: C++
    Result: Accepted
    Time:1032 ms
    Memory:9104 kb
****************************************************************/

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 2e5+100;

LL a[N], ma[N << 2],d,lastans = 0,ne_w;
int m,len = 0;

void up_data(int pos, int l, int r, int rt)
{
    if (l == r)
    {
        ma[rt] = ne_w;
        return;
    }
    int m = (l + r) >> 1;
    if (pos <= m)
        up_data(pos, lson);
    else
        up_data(pos, rson);
    ma[rt] = max(ma[rt << 1], ma[rt << 1 | 1]);
}

LL query(int L, int R, int l, int r, int rt)
{
    if (L <= l && r <= R)
        return ma[rt];
    int m = (l + r) >> 1;
    LL temp1 = -1, temp2 = -1;
    if (L <= m)
        temp1 = query(L, R, lson);
    if (m < R)
        temp2 = query(L, R, rson);
    return max(temp1, temp2);
}

int main()
{
    //freopen("F:\\rush.txt", "r", stdin);
    rei(m), rel(d);
    rep1(i, 1, m)
    {
        char key; LL t;
        key = getchar();
        scanf("%c %lld", &key, &t);
        if (key == 'A')
        {
            ne_w = (t + lastans) % d;
            len++;
            up_data(len, 1, 200000, 1);
        }
        else
            lastans = query(len - t + 1, len, 1, 200000, 1), printf("%lld\n", lastans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值