---恢复内容开始---

之所以这么迟才发是因为,这道题调了好久好久,结果发现是输入错误了。。。。。

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

好啦,模板的话还是直接看代码吧。区间修改模板还没有写。。。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
using namespace std;
int minx=-2147283646;
int a,f[1000010],tt,n1,m,d;
char c[10];

//建立线段树(这道题写的是后几位的最大值,不是区间和)
void build(int root,int l,int r)
{
    int mid;
    f[root]=minx;
    if (l==r) return ;
    mid=(l+r)/2;
    build(root*2,l,mid);
    build(root*2+1,mid+1,r);
    f[root]=max(f[root*2],f[root*2+1]);
}

//区间最值查询
int query(int root,int x,int y,int nowl,int nowr)
{
    int mid,sum;
    if (nowl>=x&&nowr<=y) return f[root];
    sum=minx;
    mid=(nowl+nowr)/2;
    if (x<=mid) sum=max(query(2*root,x,y,nowl,mid),sum);
    if (y>mid) sum=max(query(2*root+1,x,y,mid+1,nowr),sum);
    return sum;
}

//单点修改
void update(int root,int nowl,int nowr,int x,int val)
{
    int mid;
    if (nowl==nowr)
   {
        f[root]=val;
        return;
   }    
   mid=(nowl+nowr)/2;
   if (x<=mid) update(root*2,nowl,mid,x,val);
   else  update(root*2+1,mid+1,nowr,x,val);
   f[root]=max(f[root*2],f[root*2+1]);
}
int main()
{
    //freopen("lydsy1012in.txt","r",stdin);
    //freopen("lydsy1012out.txt","w",stdout);
    scanf("%d%d\n",&m,&d);
    build(1,1,m);
    n1=0;
    tt=0; 
    for (int i=1;i<=m;i++)
    {
        scanf("%s%d",&c,&a);
        if (c[0]=='A') 
        {
            n1++;
            update(1,1,m,n1,(a+tt)%d);
        }
        else
        {
            tt=query(1,n1-a+1,n1,1,m);
            printf("%d\n",tt);
        }
    }
    return 0;
}

 

posted on 2017-02-04 13:24  nhc2014  阅读(277)  评论(0编辑  收藏  举报