P1198 [JSOI2008]最大数

#include 
using namespace std;

int n,d;
int const maxn =200001;
int const INF = -2147483647;

int  thenum = 200000;

int addv[maxn],maxv[maxn<<2];

void build(int s ,int left,int right)
{
    if(left==right) {
        maxv[s]=INF;
        return ;
    }
    int mid = left+(right-left)/2;
    int l = s<<1,r= l+1;

    build(l,left,mid);
    build(r,mid+1,right);
    maxv[s] = max(maxv[l],maxv[r]);
}
int q ,v;
void update(int s,int le,int ri)
{
    if(le==ri) {
        maxv[s]=v;
        return ;
    }
    int mid = (le+ri)/2;
    int l = s<<1,r= l+1;
    if(q<=mid) update(l,le,mid);
    if(q>mid) update(r,mid+1,ri);
    maxv[s] = max(maxv[l],maxv[r]);
}
int ql,qr;
int query(int s,int l,int r) {

    if(ql<=l&&qr>=r) return maxv[s];
    int mid = (l+r)/2;
    int a1=INF,a2=INF;
    if(mid>=ql) a1 = query(s*2,l,mid);
    if(qr>mid) a2 = query(s*2+1,mid+1,r);
    return max(a1,a2);

}

int main()
{
    freopen("in.txt","r",stdin);
    cin>>n>>d;
    char ch;
    int num;
    int pre = 0;
    int size_tree=0;
    build(1,1,thenum);
    for (int i=0;i>ch>>num;
        if(ch=='A')
        {
            size_tree++;
            num = (num+pre)%d;
            q = size_tree;
            v=num;
            update(1,1,thenum);
        }
        else
        {
            qr = size_tree;
            ql = qr-num+1;
            pre = query(1,1,thenum);
            cout<

简单的线段树模板,为什么是4倍,不应该是2倍呢。

你可能感兴趣的:(P1198 [JSOI2008]最大数)