Panda
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2900 Accepted Submission(s): 966
We have known for 15 years, which has exceeded one-fifth of my whole life. I still remember the first time we went to the movies, the first time we went for a walk together. I still remember the smiling face you wore when you were dressing in front of the mirror. I love your smile and your shining eyes. When you are with me, every second is wonderful.
The more expectation I had, the more disappointment I got. You said you would like to go to U.S.I know what you really meant. I respect your decision. Gravitation is not responsible for people falling in love. I will always be your best friend. I know the way is difficult. Every minute thinking of giving up, thinking of the reason why you have held on for so long, just keep going on. Whenever you’re having a bad day, remember this: I LOVE YOU.
I will keep waiting, until you come back. Look into my eyes and you will see what you mean to me.
There are two most fortunate stories in my life: one is finally the time I love you exhausted. the other is that long time ago on a particular day I met you.
Saerdna.
It comes back to several years ago. I still remember your immature face.
The yellowed picture under the table might evoke the countless memory. The boy will keep the last appointment with the girl, miss the heavy rain in those years, miss the love in those years. Having tried to conquer the world, only to find that in the end, you are the world. I want to tell you I didn’t forget. Starry night, I will hold you tightly.
Saerdna loves Panda so much, and also you know that Panda has two colors, black and white.
Saerdna wants to share his love with Panda, so he writes a love letter by just black and white.
The love letter is too long and Panda has not that much time to see the whole letter.
But it's easy to read the letter, because Saerdna hides his love in the letter by using the three continuous key words that are white, black and white.
But Panda doesn't know how many Saerdna's love there are in the letter.
Can you help Panda?
For each test case:
First line is two integers n, m
n means the length of the letter, m means the query of the Panda. n<=50000,m<=10000
The next line has n characters 'b' or 'w', 'b' means black, 'w' means white.
The next m lines
Each line has two type
Type 0: answer how many love between L and R. (0<=L<=R<n)
Type 1: change the kth character to ch(0<=k<n and ch is ‘b’ or ‘w’)
The answer of the question.
#include "map"
#include "stack"
#include "queue"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "algorithm"
using namespace std;
#define N 50012
#define inf 2000000000
#define pi 3.1415926535897932384626433832795028841971
int num[N*5];// 表示以该下标结束的长度为3的子串是否为"wbw"
char s[N*5];//输入的字符串
struct seg
{
int l;//左范围
int r;//右范围
int n;//这个区间代表的值
}T[N*4];
void build(int l,int r,int k)//建树(前序)
{
int mid;
mid=(l+r)/2;
T[k].l=l;
T[k].r=r;
T[k].n=0;
if(l==r)
{
T[k].n=num[l];
return;
}
build(l,mid,2*k);
build(mid+1,r,2*k+1);
T[k].n=T[2*k].n+T[2*k+1].n;//更新父亲节点
}
void insert(int n,int d,int k)//d:改变的叶子节点,n:改变的量
{
int mid;
if(T[k].l==T[k].r&&T[k].l==d)
{
T[k].n=n;
return ;
}
mid=(T[k].l+T[k].r)/2;
if(d<=mid) insert(n,d,2*k);
else insert(n,d,2*k+1);
T[k].n=T[2*k].n+T[2*k+1].n;//更新父亲节点
}
int search(int l,int r,int k)//l,r查找的区间
{
int mid;
if(T[k].l==l&&T[k].r==r)
{
return T[k].n;
}
mid=(T[k].l+T[k].r)/2;
if(r<=mid) return search(l,r,2*k);
else if(l>mid) return search(l,r,2*k+1);
else
{
return search(l,mid,2*k)+search(mid+1,r,2*k+1);
}
}
int main()
{
int t,n,m,i,p,l,r,cas=1;
char k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
scanf("%s",s);
memset(num,0,sizeof(num));
printf("Case %d:\n",cas++);
for(i=2;i<n;i++)
{
if(s[i-2]=='w'&&s[i-1]=='b'&&s[i]=='w')
num[i]=1;
else
num[i]=0;
}
build(0,n-1,1);
while(m--)
{
scanf("%d",&p);
if(!p)
{
scanf("%d%d",&l,&r);
if(l+2>r)//询问的区间小于3时,直接输出0
{
printf("0\n");
continue;
}
printf("%d\n",search(l+2,r,1));
}
else
{
scanf("%d %c",&i,&k);
if(s[i]==k)//相同就不改
continue;
//改变该点,可能改变三个字符串,一个它为第一个时,一个它为第二个时,一个它为第三个时
if(i>1&&i<n&&s[i-2]=='w'&&s[i-1]=='b'&&s[i]=='w')
{
insert(0,i,1);
num[i]=0;
}
if(i>1&&i<n&&s[i-2]=='w'&&s[i-1]=='b'&&k=='w')
{
insert(1,i,1);
num[i]=1;
}
if(i>0&&i<n-1&&s[i-1]=='w'&&s[i]=='b'&&s[i+1]=='w')
{
insert(0,i+1,1);
num[i+1]=0;
}
if(i>0&&i<n-1&&s[i-1]=='w'&&k=='b'&&s[i+1]=='w')
{
insert(1,i+1,1);
num[i+1]=1;
}
if(i>=0&&i<n-1&&s[i]=='w'&&s[i+1]=='b'&&s[i+2]=='w')
{
insert(0,i+2,1);
num[i+2]=0;
}
if(i>=0&&i<n-1&&k=='w'&&s[i+1]=='b'&&s[i+2]=='w')
{
insert(1,i+2,1);
num[i+2]=1;
}
s[i]=k;//字符记得改变
}
}
}
return 0;
}