【HDU4046】Panda(36届北京赛区网络赛题,线段树)

Categories: 数据结构和算法
Comments: No Comments
Published on: 2011 年 09 月 20 日

When I wrote down this letter, you may have been on the airplane to U.S.
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?

#include<stdio.h>
#include <string.h>
#define MAXLEN 50005
#define LL(X) (X)<<1
#define RR(X) ((X)<<1)+1

struct Node{
	int l,r;
	int num;
}nodes[4*MAXLEN];
char letters[MAXLEN];
int buildTree(int l,int r,int n){
	nodes[n].l=l;
	nodes[n].r=r;
	nodes[n].num=0;
	if(l==r){
		if(l<=2) return nodes[n].num=0;
		if(letters[l]=='w'&&letters[l-1]=='b'&&letters[l-2]=='w')
			return nodes[n].num=1;
		else 
			return nodes[n].num=0;
	}
	int mid=(l+r)>>1;
	return nodes[n].num =buildTree(l,mid,LL(n))+buildTree(mid+1,r,RR(n));
}

int updata(int l,int r,int n,char t){
	if(nodes[n].l==l && nodes[n].r==r){
		letters[l]=t;
		if(l<=2){return 0;}
		if(letters[l]=='w'&&letters[l-1]=='b'&&letters[l-2]=='w')
			return nodes[n].num=1;
		else return nodes[n].num=0;
	}
	if(l>=nodes[RR(n)].l)
		return nodes[n].num =updata(l,r,RR(n),t)+nodes[LL(n)].num;
	else if(r<=nodes[LL(n)].r)
		return nodes[n].num =updata(l,r,LL(n),t)+nodes[RR(n)].num;
	else {
		return nodes[n].num =updata(l,nodes[LL(n)].r,LL(n),t)+updata(nodes[RR(n)].l,r,RR(n),t);
	}
}
int query(int l,int r,int n){
	if(nodes[n].l==l && nodes[n].r==r){
		return nodes[n].num;
	}
	if(l>=nodes[RR(n)].l)
		return query(l,r,RR(n));
	else if(r<=nodes[LL(n)].r)
		return query(l,r,LL(n));
	else {
		return query(l,nodes[LL(n)].r,LL(n))+query(nodes[RR(n)].l,r,RR(n));
	}
}
void deal(int &min,int &max){
	int temp;
	if(min>max) {temp=min;min=max;max=temp;}
}
int main(){
	//freopen("1.txt","r",stdin);
	int n,m,q,loop;
	scanf("%d",&n);
	loop=n;
	while(n--)
	{
		printf("Case %d:\n",loop-n);
		scanf("%d%d",&m,&q);
		letters[0]=0;
		scanf("%s",letters+1);
		memset(nodes,0,sizeof(nodes));
		buildTree(1,m+1,1);
		while(q--)
		{
			int l,r,cmd;
			char t[10];
			scanf("%d",&cmd);
			if(cmd==0){
				scanf("%d%d",&l,&r);
				deal(l,r);
				if(r<2 || r-l<2) printf("0\n");
				else printf("%d\n",query(l+3,r+1,1));
			}else{
				scanf("%d",&l);
				scanf("%s",t);
				updata(l+1,l+1,1,t[0]);
				if(l+1<m)
				updata(l+2,l+2,1,letters[l+2]);
				if(l+2<m)
				updata(l+3,l+3,1,letters[l+3]);
			}
		}
	}
}

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期二, 2017 年 10 月 24 日