hdu 4046 Panda [线段树]
Panda
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2565 Accepted Submission(s): 861
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
Source
Recommend
13075023 | 2015-03-09 18:29:04 | Accepted | 4046 | 1466MS | 5532K | 4484 B | G++ | czy |
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #define N 50005 5 6 using namespace std; 7 8 int n,m; 9 int T; 10 int ccnt; 11 char s[N]; 12 13 typedef struct 14 { 15 int num; 16 int sw; 17 int sbw; 18 int ew; 19 int ewb; 20 }PP; 21 22 PP p[4*N]; 23 int mp[4*N]; 24 int ll[4*N],rr[4*N]; 25 26 PP build(int i,int l,int r) 27 { 28 ll[i]=l; 29 rr[i]=r; 30 if(l==r){ 31 mp[l]=i; 32 p[i].num=0; 33 if(s[l]=='b') {p[i].ew=p[i].ewb=p[i].sw=p[i].sbw=0;} 34 else {p[i].sw=1;p[i].sbw=0;p[i].ew=1;p[i].ewb=0;} 35 // printf(" i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb); 36 return p[i]; 37 } 38 PP le,ri; 39 int mid=(l+r)/2; 40 le=build(i*2,l,mid); 41 ri=build(i*2+1,mid+1,r); 42 43 p[i].num=le.num+ri.num; 44 if(le.ew * ri.sbw ==1){ 45 p[i].num++; 46 } 47 if(le.ewb * ri.sw ==1){ 48 p[i].num++; 49 } 50 p[i].sw=le.sw; 51 p[i].ew=ri.ew; 52 if(p[i].sw==0 && s[l+1]=='w'){ 53 p[i].sbw=1; 54 } 55 else{ 56 p[i].sbw=0; 57 } 58 59 if(p[i].ew==0 && s[r-1]=='w'){ 60 p[i].ewb=1; 61 } 62 else{ 63 p[i].ewb=0; 64 } 65 // printf(" i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb); 66 return p[i]; 67 } 68 69 void ini() 70 { 71 scanf("%d%d",&n,&m); 72 scanf("%s",s+1); 73 //printf(" n=%d m=%d s=%s\n",n,m,s+1 ); 74 build(1,1,n); 75 // for(int i=1;i<=9;i++) printf("i=%d num=%d\n",i,p[i].num ); 76 } 77 78 PP query(int i,int l,int r,int L ,int R) 79 { 80 PP te; 81 if(r<L || l>R){ 82 te.num=0;te.sw=te.sbw=te.ew=te.ewb=0; 83 return te; 84 } 85 if(l==r) return p[i]; 86 if(l>=L && r<=R){ 87 //printf(" q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb); 88 return p[i]; 89 } 90 int mid=(l+r)/2; 91 PP le,ri; 92 if(mid>=L){ 93 le=query(i*2,l,mid,L,R); 94 } 95 else{ 96 le.num=0;le.sw=le.sbw=le.ew=le.ewb=-2; 97 } 98 if(mid<R){ 99 ri=query(i*2+1,mid+1,r,L,R); 100 } 101 else{ 102 ri.num=0;ri.sw=ri.sbw=ri.ew=ri.ewb=-2; 103 } 104 te.num=le.num+ri.num; 105 if(le.ew * ri.sbw ==1){ 106 te.num++; 107 } 108 if(le.ewb * ri.sw ==1){ 109 te.num++; 110 } 111 te.sw=le.sw; 112 te.ew=ri.ew; 113 if(l+1<=R && te.sw==0 && s[l+1]=='w'){ 114 te.sbw=1; 115 } 116 else{ 117 te.sbw=0; 118 } 119 120 if(r-1>=L && te.ew==0 && s[r-1]=='w'){ 121 te.ewb=1; 122 } 123 else{ 124 te.ewb=0; 125 } 126 //printf(" le q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,le.num,le.sw,le.sbw,le.ew,le.ewb); 127 //printf(" ri q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,ri.num,ri.sw,ri.sbw,ri.ew,ri.ewb); 128 //printf(" q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,te.num,te.sw,te.sbw,te.ew,te.ewb); 129 return te; 130 } 131 132 void updata(int i) 133 { 134 int l,r; 135 l=ll[i]; 136 r=rr[i]; 137 if(i==0) return; 138 PP le,ri; 139 le=p[i*2];ri=p[i*2+1]; 140 p[i].num=le.num+ri.num; 141 if(le.ew * ri.sbw ==1){ 142 p[i].num++; 143 } 144 if(le.ewb * ri.sw ==1){ 145 p[i].num++; 146 } 147 p[i].sw=le.sw; 148 p[i].ew=ri.ew; 149 if(p[i].sw==0 && s[l+1]=='w'){ 150 p[i].sbw=1; 151 } 152 else{ 153 p[i].sbw=0; 154 } 155 156 if(p[i].ew==0 && s[r-1]=='w'){ 157 p[i].ewb=1; 158 } 159 else{ 160 p[i].ewb=0; 161 } 162 updata(i/2); 163 } 164 165 void solve() 166 { 167 int t,L,R; 168 int k; 169 char ch[3]; 170 int i,j; 171 PP ans; 172 for(j=1;j<=m;j++){ 173 scanf("%d",&t); 174 // printf(" j=%d t=%d\n",j,t ); 175 if(t==0){ 176 scanf("%d%d",&L,&R); 177 // printf(" L=%d R=%d\n",L,R ); 178 ans=query(1,1,n,L+1,R+1); 179 printf("%d\n", ans.num); 180 } 181 else{ 182 scanf("%d%s",&k,ch); 183 // printf(" j=%d t=%d k=%d ch=%s\n", j,t,k,ch); 184 k++; 185 s[k]=ch[0]; 186 i=mp[k]; 187 188 p[i].num=0; 189 if(s[k]=='b') {p[i].ew=p[i].ewb=p[i].sw=p[i].sbw=0;} 190 else {p[i].sw=1;p[i].sbw=0;p[i].ew=1;p[i].ewb=0;} 191 updata(i/2); 192 // for(int i=1;i<=9;i++) printf(" i=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb); 193 } 194 } 195 } 196 197 int main() 198 { 199 scanf("%d",&T); 200 for(ccnt=1;ccnt<=T;ccnt++){ 201 ini(); 202 printf("Case %d:\n",ccnt ); 203 solve(); 204 } 205 }