1355 巧克力 (線段樹點+區間)

來源:互聯網
上載者:User

標籤:acm   資料結構   演算法   

繼續最水的線段樹  簡單粗暴

#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 pushup(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: 巧克力
Problem Description

TY最喜歡做的事情就是吃巧克力,經常幻想擁有吃不完的巧克力,作為一個acmer(菜機),IcY出了個問題準備考考她,如果回答出來,那巧克力自然是源源不斷的啦。

IcY給出了一列排好的的巧克力,有的是德芙,有的是費列羅,它們都擁有不同的美味值……現在IcY通過魔法更改了這些巧克力,TY必須能指出排列中第K個是巧克力的美味值是多少和某一段巧克力中最美味的值是多少,才能吃到巧克力,否則,哼哼,就去乖乖的做題吧。現在,TY來尋求你的協助,你能讓poor TY吃上巧克力嗎?

Input

輸入資料有很多組,以EOF結尾。

每組資料第一行是2個整數N,M。N代表初始的巧克力數目,M代表運算元。

第二行含有n個正整數,代表每塊巧克力的美味值wi。每塊巧克力的下標從0--n-1。

接下來的M行,表示M個操作。

操作分4種:

Query x y 代表查詢某一個區間內的美味最大值。

Ask x 代表查詢某一塊巧克力的美味值。

Change x y 代表將第x塊的美味值變成y

Add x y 代表講從第x塊到第y塊巧克力的美味值分別增加1.

(1 <= N<= 100000   1<= M <= 100000   Wi <= 5000 )

Output

對於每一個Query輸出一個整數,代表區間內的美味最大值。

對於每一個Ask 輸出一個整數,代表這塊巧克力的美味值。

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


1355 巧克力 (線段樹點+區間)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.