日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hdu 4046 Panda 线段树

發布時間:2025/3/13 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 4046 Panda 线段树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

五場的網絡賽的題目出現了大量的線段樹的題目,每次都只能坐在那里干瞪眼。。

看來需要好好的啃啃線段樹了。。

?

#include<iostream>
#include<string>?
#define maxn 50010?
#define lson l , m , rt << 1?
#define rson m + 1 , r , rt << 1 | 1?
using namespace std;?
int sum[maxn*4];?
char str[maxn];?
int a[maxn];?
void PushUP(int rt)
?{?
??? sum[rt] = sum[rt<<1] + sum[rt<<1|1];?
}?
void build(int l,int r,int rt)
?{?
??? if (l == r) {?
??????? sum[rt]=a[l];?
??????? return ;?
??? }?
??? int m = (l + r) >> 1;?
??? build(lson);?
??? build(rson);?
??? PushUP(rt);?
}?

void update(int p,int add,int l,int r,int rt)
?{?
??? if (l == r)
?{?
??????? sum[rt] = add;?
??????? return ;?
??? }?
??? int m = (l + r) >> 1;?
??? if (p <= m) update(p , add , lson);?
??? else update(p , add , rson);?
??? PushUP(rt);?
}?
int query(int L,int R,int l,int r,int rt)??
{?
??? if (L <= l && r <= R) {?
??????? return sum[rt];?
??? }?
??? int m = (l + r) >> 1;?
??? int ret = 0;?
??? if (L <= m) ret += query(L , R , lson);?
??? if (R > m) ret += query(L , R , rson);?
??? return ret;?
}?
int main()?
{?
??? int cas,T=0,n,m,r,l,c;?
??? char st;?
??? scanf("%d",&cas);?
??? while(cas--)?
??? {?
??????? scanf("%d %d",&n,&m);?
??????? scanf("%s",str);?
??????? memset(a,0,sizeof(a));?
??????? printf("Case %d:\n",++T);?
??????? for(int i=2;i<n;i++)//標記以該下標結尾的長度為3的子串是否符合要求?
??????? {?
??????????? if(str[i]!='w')?
??????????????? a[i]=0;?
??????????? else if(str[i-1]=='b'&&str[i-2]=='w')?
??????????????? a[i]=1;?
??????? }?
??????? build(0,n,1);?
??????? while(m--)?
??????? {?
??????????? scanf("%d",&c);?
??????????? if(c==0)?
??????????? {?
??????????????? scanf("%d %d",&l,&r);?
????l+=2;?
??????????????? if(l>r) printf("0\n");?
??????????????? else?
?????printf("%d\n",query(l,r,0,n,1));?
??????????? }?
??????????? else?
??????????? {?
??????????????? scanf("%d %c",&l,&st);?
??????????????? if(str[l]==st)//若要就該的字符跟原先相同,則不用修改了?
??????????????????? continue;?
??????????????? str[l]=st;?
??????????????? if(l>=2&&str[l-2]=='w'&&str[l-1]=='b'&&str[l]=='w')?
??????????????? {?
??????????????????? if(a[l]==0)//若改之前是不符合要求的串,則更新?
??????????????????????? update(l,1,0,n,1);?
??????????????????? a[l]=1;?
??????????????? }?
??????????????? else if(l>=2&&a[l]==1)//若改之前是符合要求的串,則更新?
??????????????? {?
??????????????????? update(l,0,0,n,1);?
??????????????????? a[l]=0;?
??????????????? }?
??????????????? if(l>=1&&str[l]=='b'&&str[l-1]=='w'&&str[l+1]=='w')?
??????????????? {?
??????????????????? if(a[l+1]==0)?
??????????????????????? update(l+1,1,0,n,1);?
??????????????????? a[l+1]=1;?
??????????????? }?
??????????????? else if(l<n-1&&l>=1&&a[l+1]==1) a[l+1]=0,update(l+1,0,0,n,1);?
??????????????? if(str[l]=='w'&&str[l+1]=='b'&&str[l+2]=='w')?
??????????????? {?
??????????????????? if(a[l+2]==0)?
??????????????????????? update(l+2,1,0,n,1);?
??????????????????? a[l+2]=1;?
??????????????? }?
??????????????? else
?????if(l<n-2&&a[l+2]==1)
??????a[l+2]=0,update(l+2,0,0,n,1);?
??????????? }?
??????? }?
??? }?
??? return 0;?
}

轉載于:https://www.cnblogs.com/Mu-Tou/archive/2011/09/21/2184445.html

總結

以上是生活随笔為你收集整理的hdu 4046 Panda 线段树的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。