P1198 [JSOI2008] 最大数 ( 线段树

#include 
//using int_max = 0x3f3f3f3f;
#define long_max 9223372036854775807;
using namespace std;
typedef long long ll;
typedef pair PII;
typedef pair PDD;
using VI = vector;
typedef unsigned long long ull;
const int MAXN = 1e6;
int m,d;
ll data[2000050];
void change(int s,int p,int k,int l,int r){
    if(l == r){
        data[p] = k;
        return;
    }
    int mid = (l + r) >> 1;
    if(s <= mid) change(s,2*p,k,l,mid);
    if(s > mid) change(s,2*p+1,k,mid+1,r);
    data[p] = max(data[2*p] , data[2*p+1])%d;
}

ll ask(int nl,int nr,int p,int l,int r){
    if(nl <= l && r <= nr){
        return data[p];
    }
    int mid = (l + r) >> 1;
    ll a = -long_max;
    ll b = a;
    if(nl <= mid) a = ask(nl,nr,2*p,l,mid);
    if(nr > mid) b = ask(nl,nr,2*p+1,mid+1,r);
    return max(a,b);
}

int main() {
    cin>>m>>d;
    ll n = m;
    int line = 0;
    ll t = 0;
    while (m--){
        char a;
        ll b;
        cin>>a>>b;
        if(a == 'A'){
            line++;
            change(line,1,(b+t)%d,1,n);
        }else{
            t = ask(line-b+1,line,1,1,n);
            cout<

关于线段树不不建树(针对单点修改时),单点修改也不用打lazy

把线段树的大小作为参数  函数中

你可能感兴趣的:(数据结构题,算法,数据结构,c++)