HDU--4046--Panda【線段樹】

來源:互聯網
上載者:User

標籤:線段樹

連結:http://acm.hdu.edu.cn/showproblem.php?pid=4046

題意:題目先來了一大段情書,看的我莫名其妙,然後來了一段極像《那些年》歌詞翻譯的本文,然後才是真正的題目。給你一個字串,最長50000,只有兩種字母,w和b。然後m個操作,操作有兩種,第一種是查詢l,r區間中有幾個字串是”wbw“形式,第二種操作是把字串下標k的字元變成輸入的字元c。


一眼就能看出是線段樹,但是怎麼做?我覺得線段樹寫起來就那樣,偶爾需要一些小技巧,多加些節點資訊,或者和離散化結合起來。但是線段樹最難的地方我覺得還不是這些,而是節點儲存什麼資訊,明白了這個才能著手敲線段樹,並且其他步驟就和裸線段樹差不多了。我參考著這個部落格的題來做線段樹:http://blog.csdn.net/shiqi_614/article/details/8228102,有一些題目是基礎的,都沒問題,我不會做的要麼就是英文沒讀懂,要麼就是不知道節點儲存什麼資訊,所以對於我來說,節點存什麼,是線段樹中最難的點。


這道題也一樣,我想了很久,今天下午去踢球的路上想到一種方式:如果是w就用3代替,b就用5代替,再乘它所在的位元,比如”wbw“ 就是 1*3+2*5+3*3 = 22,這樣wbw的數字是唯一的,存入線段樹中,至於父節點的pushup,如果子節點%22為0再加入到父節點的值中,最後搜到的區間值除以22就是wbw的個數,wbw是三個字母,所以只有兩個字母時肯定是不行的,r-l<2肯定也不行。查詢的時候把左區間+2,再查詢。

更新的時候需要考慮三種情況,當前k位置是第一個字母、第二個字母、第三個字母的情況。


更新位置逗比的寫成了k,調試了半天沒出範例。。。加了一堆輸出中間量,終於發現寫逗了。。。改之AC

#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 50100#define eps 1e-7#define INF 0x7FFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1int sum[MAXN<<2];int a[MAXN];int n;char s[MAXN];void pushup(int rt){    sum[rt] = (sum[rt<<1]%22==0?sum[rt<<1]:0) + (sum[rt<<1|1]%22==0?sum[rt<<1|1]:0);}void build(int l,int r,int rt){    if(l==r){        sum[rt] = a[l];//        cout<<rt<<" aaa"<<a[l]<<endl;        return ;    }    int m = (l+r)>>1;    build(lson);    build(rson);    pushup(rt);}void update(int k,int c,int l,int r,int rt){    if(l==r){        sum[rt] = c;        return ;    }    int m = (l+r)>>1;    if(k<=m) update(k,c,lson);    else    update(k,c,rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R){//        cout<<rt<<" "<<sum[rt]<<endl;        if(sum[rt]%22)  return 0;        else    return sum[rt];    }    int m = (l+r)>>1;    ll ret = 0;    if(L<=m) ret += query(L,R,lson);    if(R>m)    ret += query(L,R,rson);    return ret;}int main(){    int t,i,j,aa,b,c,k=1;    int m;    char str[5];    scanf("%d",&t);    while(t--){        memset(a,0,sizeof(a));        memset(sum,0,sizeof(sum));        scanf("%d%d",&n,&m);        scanf("%s",s);        //cout<<s<<endl;        int l = strlen(s);        for(i=2;i<l;i++){            a[i] = (s[i-2]=='w'?3:5)*1+(s[i-1]=='w'?3:5)*2+(s[i]=='w'?3:5)*3;        }        build(0,n,1);//        for(i=0;i<n;i++){//            cout<<a[i]<<" ";//        }//        cout<<endl;        printf("Case %d:\n",k++);        while(m--){//            cout<<s<<endl;//            for(i=0;i<n;i++){//                cout<<sum[8]<<" "<<sum[9]<<" "<<sum[5]<<" "<<sum[12]<<" "<<sum[13]<<endl;//            }            scanf("%d",&aa);            if(aa==0){                scanf("%d%d",&b,&c);                b += 2;                if(b>c) puts("0");                else    printf("%d\n",query(b,c,0,n,1)/22);            }            else{                scanf("%d%s",&b,str);                if(str[0]==s[b])    continue;                s[b] = str[0];                if(b>=2){                    a[b] = (s[b-2]=='w'?3:5)*1+(s[b-1]=='w'?3:5)*2+(s[b]=='w'?3:5)*3;                    update(b,a[b],0,n,1);                }                if(b>=1&&b<n-1){                    a[b+1] = (s[b-1]=='w'?3:5)*1+(s[b]=='w'?3:5)*2+(s[b+1]=='w'?3:5)*3;                    update(b+1,a[b+1],0,n,1);                }                if(b<n-2){                    a[b+2] = (s[b]=='w'?3:5)*1+(s[b+1]=='w'?3:5)*2+(s[b+2]=='w'?3:5)*3;                    update(b+2,a[b+2],0,n,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.