bzoj 1012: [JSOI2008] maximum number maxnumber (segment tree)

1012: [JSOI2008] maxnumber maxnumber

Time Limit: 3 Sec  Memory Limit: 162 MB
Submit: 13081  Solved: 5654
[Submit][Status][Discuss]

Description

  Now I ask you to maintain an array and provide the following two operations: 1. Query operation. Syntax: QL Function: Query
the largest number among the last L numbers in the current sequence, and output the value of this number. Restriction: L does not exceed the length of the current sequence. 2. Insert operation. Syntax: A n Function:
Add t, where t is the answer of the most recent query operation (if the query operation has not been performed, then t=0), and the result is
modulo a fixed constant D, the The resulting answer is inserted at the end of the sequence. Restrictions: n is a non-negative integer and is in the range of long integers. Note: Initially, the number column is empty, without a
number.

Input

  The first line contains two integers, M and D, where M represents the number of operations (M <= 200,000), and D, as described above, satisfies D within longint. Next
M lines, query operation or insert operation.

Output

  For each query operation, output one line. The row has only one number, the maximum number of the last L numbers in the sequence.

Sample Input

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

Sample Output

96
93
96
 
Ideas:
water problem
 
Implementation code:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1
const ll M = 2e5+10;
const ll inf = 2e5+10;
ll sum[M<<2],n;
ll m;
void pushup(ll rt){
    sum[rt] = max(sum[rt<<1],sum[rt<<1|1]);
}

void build(ll l,ll r,ll rt){
    if(l == r){
        sum[rt] = 0;
        return ;
    }
    mid;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(ll p,ll c,ll l,ll r,ll rt){
    if(l == r){
        sum[rt] = c;
        return ;
    }
    mid;
    if(p <= m) update(p,c,lson);
    else update(p,c,rson);
    pushup(rt);
}

ll query(ll L,ll R,ll l,ll r,ll rt){
    if(L <= l&&R >= r){
        return sum[rt];
    }
    mid;
    ll ret = 0;
    if(L <= m) ret = max(ret,query(L,R,lson));
    if(R > m) ret = max(ret,query(L,R,rson));
    return ret ;
}

int main(){
    scanf("%lld%lld",&n,&m);
    ll len = 0,t = 0;
    build(1,inf,1);
    char c[3]; ll x;
    while(n--){
        scanf("%s",c);
        scanf("%lld",&x);
        if(c[0]=='A'){
            len ++ ;
            update(len,(t+x)%m,1,inf,1);
        }
        else{
            t = query (len-x + 1 , len, 1 , inf, 1 );
            printf("%lld\n",t);
        }
    }
}

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325249541&siteId=291194637