P1198 [JSOI2008] Maximum (segment tree base)

P1198 [JSOI2008] The maximum number of

Topic Description

Now request you to maintain a number of columns, it requires the following two actions:

1, the query operation.

grammar:Q L

Function: the maximum number of queries in the end of the number of columns in the current number L, and outputs the value of this number.

Restrictions: L L does not exceed the length of the current sequence. (L> 0) ( L > 0 )

2, the insertion operation.

grammar:A n

Function: n- n-plus T T, where T T is the answer to the last query operation (if not already performed a query operation, then T = 0 T = 0), and the result of a fixed constant D D taken die, the resultant answer inserted at the end of the number of columns.

Restrictions: n- n-integer (possibly negative) and in the entire length range.

Note: The number of columns is initially empty, not a number.

Input Format

The first line of two integers, M M and D D, where M M represents the number of operations (M \ Le 200,000) ( M 2 0 0 , 0 0 0 ), D D described hereinabove, satisfying (0 < D <2,000,000,000) ( 0 < D < 2 , 0 0 0 , 0 0 0 , 0 0 0 )

The next M M rows, each row a string describing a specific operation. Syntax as described above.

Output Format

For each query, you should turn the output in the order, and each result row.

Sample input and output

Input # 1
5 100
A 96
Q 1
A 97
Q 1
Q 2
Output # 1
96
93
96

Description / Tips

[JSOI2008]

This question has been strengthened data

 

Solution: for the tree line basis ...... just learning segment tree for people to open up about ideas to write about. According to the topic we can change the maximum segment sum of the segment, so many will simply fast, specific codes below;

#define _CRT_SECURE_NO_DepRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <algorithm>
#include <bitset>
#include <cstdlib>
#include <cctype>
#include <iterator>
#include <vector>
#include <cstring>
#include <cassert>
#include <map>
#include <queue>
#include <set>
#include <stack>
#define ll long long
#define INF 0x3f3f3f3f
#define ld long double
const ld pi = acos(-1.0L), eps = 1e-8;
int qx[4] = { 0,0,1,-1 }, qy[4] = { 1,-1,0,0 }, qxx[2] = { 1,-1 }, qyy[2] = { 1,-1 };
using namespace std;
struct node
{
    ll l = 0, r = 0, sum = 0, plz = 0, mlz = 1;
}tree[1000000];
ll  p = INF, maxx;
inline void Build ( int I, int L, int R & lt, int INSERT, int NUM) // tree ID leftmost rightmost insert values into subscript 
{
    tree[i].l = l;
    Tree [I] .r = R & lt;
     IF (R & lt == L) // find the point at which replace INSERT 
    {
        tree[i].sum = insert;
    }
    else
    {
        if ((l + r) / 2 >= num)
        {
            build(i << 1, l, (l + r) / 2, insert, num);
        }
        else
        {
            build(i << 1 | 1, (l + r) / 2 + 1, r, insert, num);
        }
        Tree [I] .sum = max (Tree [I << . 1 ] .sum, Tree [I << . 1 | . 1 ] .sum); // maximum sum for each line segment 
    }
}
inline ll search_max(int i, int l, int r)
{
    IF (Tree [I] .L> Tree && = L [I] .r <= R & lt) // because the maximum value of which represents the sum of the segment, the segment is fully contained so finding can directly return sum 
    {
         return Tree [I ] .sum;
    }
    if (tree[i].l > r || tree[i].r < l)
    {
        return 0;
    }
    ll ans = -INF;
    if (l <= tree[i << 1].r)
    {
        ans = max(ans, search_max(i << 1, l, r));
    }
    if (r >= tree[i << 1 | 1].l)
    {
        ans = max(ans, search_max(i << 1 | 1, l, r));
    }
    return ans;
}
int main ()
{
    ios::sync_with_stdio(false);
    cin.tie ( 0 );
    ll m, d, t = 0, sum = 1, b,input;
    char x;
    cin >> m >> d;
    for (int i = 0; i < m; i++)
    {
        cin >> x;
        if (x == 'A')
        {
            cin >> input;
            input = (input + t) % d;
            Build ( . 1 , . 1 , m, INPUT, SUM); // Because of m can only insert, it may be directly in m R & lt 
            SUM ++ ;
        }
        else
        {
            cin >> b;
            t = search_max(1, sum - b, sum - 1);
            cout << t << endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Load-Star/p/12670810.html