hdu 4046 Panda 线段树
2011-09-21 21:04
423 查看
五场的网络赛的题目出现了大量的线段树的题目,每次都只能坐在那里干瞪眼。。
看来需要好好的啃啃线段树了。。
#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;
}
看来需要好好的啃啃线段树了。。
#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;
}
相关文章推荐
- hdu 4046 Panda(线段树+单点更新)
- hdu_4046 Panda线段树_点修改_区间求和
- HDU 4046 Panda(线段树单点更新)
- hdu 4046 Panda (线段树 单点更新 区间查询)
- HDU 4046 Panda(RMQ 线段树 树状数组)
- 线段树 HDU 4046 panda
- [HDU 4046]Panda[线段树]
- hdu 4046 Panda 线段树 单点修改 求区间010个数
- HdU 4046 Panda 线段树
- [置顶] HDU 4046 Panda (分块 或 线段树区间合并)
- hdu 4046 Panda (线段树,单点更新,区间求和)
- HDU 4046 Panda(线段树)
- hdu 4046 Panda [线段树]
- HDU 4046 - Panda (线段树 + 单点更新)
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
- hdu 4046 Panda 树状数组
- HDU 4046 Panda 树状数组
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
- hdu 4046 2011北京赛区网络赛G 线段树 ***
- hdu 4046 Panda