HDU 1698 Just a Hook(線段樹區間替換)

來源:互聯網
上載者:User

標籤:acm   c語言   演算法   編程   線段樹   

題目地址:HDU 1698

區間替換裸題。同樣利用lazy延遲標記數組,這裡只是當lazy下放的時候把下面的lazy也全部改成lazy就好了。

代碼如下:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;#define lson l, mid, rt<<1#define rson mid+1, r, rt<<1|1const int MAXN=1e5+10;int sum[MAXN<<3], lazy[MAXN<<3];void PushUp(int rt){    sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void PushDown(int rt, int m){    if(lazy[rt])    {        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];        sum[rt<<1]=lazy[rt]*(m-(m>>1));        sum[rt<<1|1]=lazy[rt]*(m>>1);        lazy[rt]=0;    }}void build(int l, int r, int rt){    lazy[rt]=0;    if(l==r)    {        sum[rt]=1;        return ;    }    int mid=l+r>>1;    build(lson);    build(rson);    PushUp(rt);}void update(int ll, int rr, int x, int l, int r, int rt){    if(ll<=l&&rr>=r)    {        lazy[rt]=x;        sum[rt]=(r-l+1)*x;        return ;    }    PushDown(rt, r-l+1);    int mid=l+r>>1;    if(ll<=mid) update(ll,rr,x,lson);    if(rr>mid) update(ll,rr,x,rson);    PushUp(rt);}int main(){    int n, i, a, b, c, t, q, num=0;    scanf("%d",&t);    while(t--)    {        num++;        scanf("%d",&n);        build(1,n,1);        scanf("%d",&q);        while(q--)        {            scanf("%d%d%d",&a,&b,&c);            update(a,b,c,1,n,1);        }        printf("Case %d: The total value of the hook is %d.\n",num,sum[1]);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.