1355 Chocolate (segment tree dot + interval)

Source: Internet
Author: User

Continue the most water segment tree simple rough

#include <cstdio> #include <cstring> #include <algorithm> #define LC p<<1,s,mid#define RC p< <1|1,mid+1,e#define Mid ((s+e) >>1) using namespace std;const int N = 100005;int add[4 * N], maxv[4 * n];void Pushu P (int p) {Maxv[p] = max (maxv[p << 1], maxv[p << 1 | 1]);}    void pushdown (int p) {add[p << 1] + = add[p];    Add[p << 1 | 1] + + add[p];    Maxv[p << 1] + = add[p];    Maxv[p << 1 | 1] + + add[p]; Add[p] = 0;}    void build (int p, int s, int e) {add[p] = 0;    if (s = = e) scanf ("%d", &maxv[p]);        else {build (LC);        Build (RC);    Pushup (P);        }}void Update (int p, int s, int e, int l, int r, int v, int op) {if (s = = L && E = = r) {if (OP)            {Add[p] + = V;        Maxv[p] + = V;        } else maxv[p] = v;    Return    } pushdown (P);    if (r <= mid) update (LC, L, R, V, op);    else if (L > Mid) Update (RC, L, R, V, op); else Update (LC, L, MID, V, op), update (RC, mid + 1, R, V, op); Pushup (P);}    int query (int p, int s, int e, int l, int r) {if (s = = L && E = = r) return maxv[p];    Pushdown (P);    if (R <= mid) return query (LC, L, R);    if (L > Mid) return query (RC, L, R); Return Max (query (LC, L, mid), query (RC, mid + 1, r));}    int main () {int n, m, x, y;    Char cmd[10];        while (~SCANF ("%d%d", &n, &m)) {build (1, 1, N);            while (m--) {scanf ("%s", cmd);                if (!strcmp (cmd, "Ask")) {scanf ("%d", &x);                printf ("%d\n", Query (1, 1, N, x + 1, x + 1));            Continue            } scanf ("%d%d", &x, &y);            if (cmd[0] = = ' Q ') printf ("%d\n", Query (1, 1, N, x + 1, y + 1));            else if (cmd[0] = = ' C ') update (1, 1, N, x + 1, x + 1, y, 0);        else update (1, 1, N, x + 1, y + 1, 1, 1); }} return 0;}

1355: Chocolate
Problem Description

Ty favorite thing to do is to eat chocolate, often fantasy to have to eat the chocolate, as a acmer (dish machine), icy out a question to prepare for test her, if answer out, that chocolate is a steady stream of.

Icy gives a list of rows of chocolates, some of them, some Ferrero, and they all have different delicious values ... Now that icy has changed these chocolates by magic, Ty must be able to point out that the K in the arrangement is the delicious value of chocolate and what is the most delicious value in a certain piece of chocolate, in order to eat chocolate, otherwise, hem, go obediently to do the problem. Now, Ty comes to ask for your help, can you let poor ty eat chocolate?

Input

The input data has many groups, ending with EOF.

The first row of each set of data is 2 integer n,m. n represents the initial number of chocolates, and M represents the operand.

The second line contains n positive integers, which represent the delicious value of WI for each piece of chocolate. Each piece of chocolate is subscript from 0--n-1.

The next M-line represents m operations.

The operation is divided into 4 kinds:

Query x y represents the maximum value of the delicacy in a range.

Ask x is a good value for querying a piece of chocolate.

Change x y represents the value of the delicacy of the x block into Y

Add x y represents a 1 increase in the delicious value of chocolate from block X to block Y, respectively.

( 1 <= n<= 100000 1<= M <= 100000 Wi <= )

Output

For each query output an integer that represents the delicious maximum value within the interval.

For each ask output an integer that represents the delicious value of the chocolate.

Sample Input
2 3 4 5 6 7 8 9 10Ask 0Change 0 1Add 0 2Query 0 2
Sample Output
14


1355 Chocolate (segment tree dot + interval)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.