【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;
}
posted @ 2017-10-04 18:45  AWCXV  阅读(192)  评论(0编辑  收藏  举报