/*Panda Time Limit : 10000/4000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 0 Accepted Submission(s) : 0 Font: Times New Roman | Verdana | Georgia Font Size: ← → 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? 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’) Output For each test case, output the case number first. 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 The 36th ACM/ICPC Asia Regional Beijing Site —— Online Contest */ #include<stdio.h> int segtree[200040], num[50010];//0开始 char s[50010];//0开始 void build(int left, int right, int rt) { segtree[rt] = 0; if(left == right) { segtree[rt] = num[left]; return ; } int mid = (right + left)>>1; build(left, mid, rt<<1); build(mid+1, right, rt<<1|1); segtree[rt] = segtree[rt<<1] + segtree[rt<<1|1]; } void update(int x, int left, int right, int rt) { if(left == right ) { segtree[rt] = num[left]; return; } int mid = (left+right)>>1; if(mid >= x) update(x, left, mid, rt<<1); else update(x, mid+1, right, rt<<1|1); segtree[rt] = segtree[rt<<1] + segtree[rt<<1|1]; } int query(int x, int y, int left, int right, int rt) { if(x <= left && y >= right) { return segtree[rt]; } int sum = 0, mid = (left+right)>>1; if(mid >= x) sum += query(x, y, left, mid, rt<<1); if(mid+1 <= y) sum += query(x, y, mid+1, right, rt<<1|1); return sum; } int main() { int T, i, j, k, cas = 1, n, m; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); getchar(); scanf("%s", s); for(i = 0; i < n-2; ++i) { if(s[i] == 'w' && s[i+1] == 'b' && s[i+2] == 'w') num[i+1] = 1; else num[i+1] = 0; } num[n-1] = 0; num[n] = 0; build(1, n, 1); printf("Case %d:\n", cas++); for(i = 0; i < m; ++i) { int a; scanf("%d", &a); if(a)//change { int b; char c; scanf("%d %c", &b, &c); if(s[b] == c) continue; s[b] = c;//change b+1th character if(b >= 2)//last { if(s[b-2] == 'w' && s[b-1] == 'b' && s[b] == 'w') num[b-1] = 1; else num[b-1] = 0; update(b-1, 1, n, 1); } if(b >= 1 && b <= n-2)//mid { if(s[b-1] == 'w' && s[b] == 'b' && s[b+1] == 'w') num[b] = 1; else num[b] = 0; update(b, 1, n, 1); } if(b <= n-3)//first { if(s[b] == 'w' && s[b+1] == 'b' && s[b+2] == 'w') num[b+1] = 1; else num[b+1] = 0; update(b+1, 1, n, 1); } } else//printf { int b, c; scanf("%d%d", &b, &c); if(c - b < 2) printf("0\n"); else printf("%d\n", query(b+1, c-1, 1, n, 1) ); } } } return 0; }
题意:给出一个字符串,仅由‘w’,‘b’组成,当三个连续的“wbw”一起时为一个love,求给出一个字符串中有多少个love。有2种操作,一是询问一段区间内有多少个love,二是改变字符串中的某个字符。
思路:本题明显可以看出是用线段树,可此题统计时,是以三个字节为一个标准单位,按常理很难用线段树来统计,所以得先将三个字节作为一个小单位存放进数组,再以保存的数组作为基准来建立线段树,至于小单位可以以三个字节的首字节作为判断标准,或是末字节作为判断标准进行整合,我这里是以首字节为标准。即第一个字母即为数组的第一个数,如果第一个字母为首字母的小单位为wbw则数组为1,否则为0。以此表示数组num【i】即以第i个字母为开头的三个连续的字符是否为love,然后就可以建立一个线段树,查询时大大缩短时间,并且实现单点更新。
难点:这道题有很多细节,由于是单个字符改变,但实际是以三个字符作为一个标准单位,牵扯到线段树的三个叶子节点,所以对于线段树来说需要实现三次单点更新,即当改变的字母分别作为首 中 末时的三个标准单位,每种情况都有一个区间限制,所以更新时需要注意,其次是题目中下标是从0开始的,而线段树是从1开始的,查询和更新时也需要注意。