队里几个大神刷题太快了qwq,但是我还是很想花点时间停下来多做做树状数组题。
搜了百度没看到多少入门的题,树状数组可能综合性比较强,因此基础必须打好。
Panda
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3467 Accepted Submission(s): 1120
Problem Description
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?
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?
Input
An integer T means the number of test cases T<=100
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’)
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’)
Output
For each test case, output the case number first.
The answer of the question.
The answer of the question.
Sample Input
2 5 2 bwbwb 0 0 4 0 1 3 5 5 wbwbw 0 0 4 0 0 2 0 2 4 1 2 b 0 0 4
Sample Output
Case 1: 1 1 Case 2: 2 1 1 0
思路比较简单,主要是更换字母的时候,有点难度。我的想法是只考虑每个对右面的影响,这样考虑也是全面的,而且比较好些。
我想到的就列举下来了。有一个点,如果区间是[x,y]那么应该考虑sum(y)-sum(x+1),理由是我们只考虑对右面的影响,所以区间的第三个小点才会出现爱心值。
但是我以为能一次AC,实际上有没有实现。理由是,x+1很可能比y大,造成错误。实际上区间长度不足3的时候(请注意不是y-x<3而且y-x<2,因为是闭区间。比如[3,5]长度已经是3了),可以直接输出0,从而又加速又保证正确性。
代码啊好长好长,软疼。。列举大功法……
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<vector>
#include<cctype>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<sstream>
#include<iostream>
#include<algorithm>
#define rep(i,a,b) for(i=a;i<=b;i++)
#define rec(i,a) for(i=0;i< a;i++)
#define N 50000 + 10
#define Q 1000 + 10
#define P 100 + 1
char u;
int n,q;
int x1,y1;
int t1,t2,t3;
int t4,t5,t6;
int a[N]={0};
char uc[N]={0};
using namespace std;
int lowbit(int n)
{
return n&(-n);
}
void update(int i,int number)
{
while(i<=n)
{
a[i]+=number;
i+=lowbit(i);
}
}
int sumfunction(int i)
{
int sum=0;
while(i)
{
sum+=a[i];
i-=lowbit(i);
}
return sum;
}
int function(int i)
{
int mark=0;
if(uc[i]=='w'&&uc[i-1]=='b'&&uc[i-2]=='w')mark=1;
if(mark)return 1;
else return 0;
}
void changefunction(int x)
{
t1=function(x+0);
t2=function(x+1);
t3=function(x+2);
uc[x]=u;
t4=function(x+0);
t5=function(x+1);
t6=function(x+2);
if(t1==1&&t4==0) update(x+1,-1);
else if(t1==0&&t4==1)update(x+1, 1);
if(t2==1&&t5==0) update(x+2,-1);
else if(t2==0&&t5==1)update(x+2, 1);
if(t3==1&&t6==0) update(x+3,-1);
else if(t3==0&&t6==1)update(x+3, 1);
}
void changefunction1(int x)
{
t1=function(x+2);
uc[x]=u;
t4=function(x+2);
if(t1==1&&t4==0) update(x+3,-1);
else if(t1==0&&t4==1)update(x+3, 1);
}
void changefunction2(int x)
{
t1=function(x+1);
t2=function(x+2);
uc[x]=u;
t4=function(x+1);
t5=function(x+2);
if(t1==1&&t4==0) update(x+2,-1);
else if(t1==0&&t4==1)update(x+2, 1);
if(t2==1&&t5==0) update(x+3,-1);
else if(t2==0&&t5==1)update(x+3, 1);
}
void changefunction3(int x)
{
t1=function(x+0);
t2=function(x+1);
uc[x]=u;
t4=function(x+0);
t5=function(x+1);
if(t1==1&&t4==0) update(x+1,-1);
else if(t1==0&&t4==1)update(x+1, 1);
if(t2==1&&t5==0) update(x+2,-1);
else if(t2==0&&t5==1)update(x+2, 1);
}
void changefunction4(int x)
{
t1=function(x+0);
uc[x]=u;
t4=function(x+0);
if(t1==1&&t4==0) update(x+1,-1);
else if(t1==0&&t4==1)update(x+1, 1);
}
int main()
{
int T;
int i;
int L1;
int position;
scanf("%d",&T);
for(int cnt=1;cnt<=T;cnt++)
{
printf("Case %d:\n",cnt);
memset(a,0,sizeof a);
memset(uc,0,sizeof uc);
scanf("%d%d",&n,&q);
scanf("%s",uc);
L1=strlen(uc);
for(i=2;i<L1;i++)
{
if(function(i))
{
update(i+1,1);
}
}
for(i=1;i<=q;i++)
{
scanf("%d",&position);
if(position==0)
{
scanf("%d%d",&x1,&y1);++x1,++y1;
if(y1-x1<2)printf("0\n");
printf("%d\n",sumfunction(y1)-sumfunction(x1+1));
}
else if(position==1)
{
scanf("%d",&x1);getchar();
scanf("%c",&u);
if(x1>=2&&x1<=L1-3)
changefunction(x1);
else if(x1==0)changefunction1(x1);
else if(x1==1)changefunction2(x1);
else if(x1==L1-2)changefunction3(x1);
else if(x1==L1-1)changefunction4(x1);
}
}
memset(a,0,sizeof a);
memset(uc,0,sizeof uc);
}
return 0;
}
运行时间是430MS左右,没有使用读入优化。如果用cin读取字符,不加getchar(),550MS。