### 【洛谷1198 JSOI】最大數 單點更新線段樹+區間查詢最大值

1、 查詢操作。

2、 插入操作。

5 100
A 96
Q 1
A 97
Q 1
Q 2

96
93
96

``/*Do not delay anything that adds laughter and joy to your life.*/#include<iostream>#include<cstdio>#define N 2147283647using namespace std;struct node{    int val;    int l,r;}tree[3000005];int m,d,num,t;void build(int root,int l,int r)  //建樹 {    int mid;    tree[root].l=l;tree[root].r=r;    if (l==r) tree[root].val=-N;    else    {        mid=(l+r)/2;        build(root*2,l,mid);        build(root*2+1,mid+1,r);        tree[root].val=max(tree[root*2].val,tree[root*2+1].val);    }}void add(int root,int id,int addval)  //單點更新 {    int mid;    if (tree[root].l==tree[root].r)    {        tree[root].val=addval;        return;    }    else    {        mid=(tree[root].l+tree[root].r)/2;        if (id<=mid) add(root*2,id,addval);        else add(root*2+1,id,addval);        tree[root].val=max(tree[root*2].val,tree[root*2+1].val);    }}int ask(int root,int l,int r){    int mid;    if (tree[root].l>=l&&tree[root].r<=r)        return tree[root].val;    else     {        mid=(tree[root].l+tree[root].r)/2;        if (mid>=r)            return ask(root*2,l,r);        else if (mid<l)            return ask(root*2+1,l,r);         else return max(ask(root*2,l,mid),ask(root*2+1,mid+1,r));    }}int main(){    char c;    int i,x,tmp;    scanf("%d%d",&m,&d);    build(1,1,m);    for (i=1;i<=m;i++)    {        cin>>c;        scanf("%d",&x);        if (c=='A')        {            num++;            tmp=(x+t)%d;            add(1,num,tmp);        }        else         {            t=ask(1,num-x+1,num)%d;            printf("%d\n",t);        }    }}``