当前位置: 首页 > 工具软件 > Shining Panda > 使用案例 >

hdu4046 - Panda (线段树-单点更新)

柯星华
2023-12-01

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046

题目原文:

Panda

Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4402    Accepted Submission(s): 1351


 

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

 

 题目大意:

对一段w和b组成的字符串进行两种操作,一是修改某一个字符,二是查询某一区间wbw组合的数量。

解题思路:

利用线段树单点更新,每次修改都会相应三次修改单个字符。同时sumv数组表示的是以下标idx结尾的组合数目。

AC代码:

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;

const int INF = 0x3f3f3f3f;

const int MAXN = int(5e4 + 5);
const int LEN = 131072; // (1 << (int) log2(MAXN) << 2);

#define TREE int l, int r, int rt
#define LSON (rt << 1)
#define RSON (rt << 1 | 1)
#define MIDDLE ((l + r) >> 1)
#define LTREE l, MIDDLE, LSON
#define RTREE MIDDLE + 1, r, RSON

char arr[MAXN];
int sumv[LEN];

void pushUp(int rt)
{
    sumv[rt] = sumv[LSON] + sumv[RSON];
}

void build(TREE)
{
    if (l == r)
    {
        int idx = l;
        scanf(" %1c", &arr[idx]);
        sumv[rt] = 0;
        if (idx > 2 && arr[idx] == 'w' && arr[idx - 1] == 'b' && arr[idx - 2] == 'w')
        {
            sumv[rt] = 1;
        }
        return;
    }
    build(LTREE);
    build(RTREE);
    pushUp(rt);
}

void update(TREE, int pos, char val)
{
    if (l == r)
    {
        arr[pos] = val;
        if (pos > 2)
        {
            if (arr[pos] == 'w' && arr[pos - 1] == 'b' && arr[pos - 2] == 'w') sumv[rt] = 1;
            else sumv[rt] = 0;
            // if (arr[pos + 1] == 'w' && arr[pos] == 'b' && arr[pos - 1] == 'w') sumv[rt + 1] = 1;
            // else sumv[rt] = 0;
            // if (arr[pos + 2] == 'w' && arr[pos + 1] == 'b' && arr[pos] == 'w') sumv[rt + 2] = 1;
            // else sumv[rt] = 0;
        }
        return;
    }
    if (pos <= MIDDLE) update(LTREE, pos, val);
    else update(RTREE, pos, val);
    pushUp(rt);
}

int query(TREE, int left, int right)
{
    if (left > right) return 0;
    if (left <= l && r <= right)
    {
        return sumv[rt];
    }
    int ans = 0;
    if (left <= MIDDLE) ans += query(LTREE, left, right);
    if (right > MIDDLE) ans += query(RTREE, left, right);
    return ans;
}

int main()
{
    int T;
    scanf("%d", &T);
    int I = 0;
    while (T--)
    {
        // memset(arr, '.', sizeof(arr));
        printf("Case %d:\n", ++I);
        int n, m;
        scanf("%d%d", &n, &m);
        int i;
        build(1, n, 1);

        // for (i = 1; i <= n; i++)
        // {
        //     printf("%c(%d) ", arr[i], sumv[i]);
        // }
        // printf("\n");

        int op, l, r, idx;
        char ch;
        for (i = 0; i < m; i++)
        {
            scanf("%d", &op);
            if (op == 0)
            {
                scanf("%d%d", &l, &r);
                ++l, ++r;
                l += 2;
                printf("%d\n", query(1, n, 1, l, r));
            }
            else
            {
                scanf("%d %1c", &idx, &ch);
                ++idx;
                update(1, n, 1, idx, ch);
                if (idx + 1 <= n) update(1, n, 1, idx + 1, arr[idx + 1]);
                if (idx + 2 <= n) update(1, n, 1, idx + 2, arr[idx + 2]);
            }
        }
    }
    return 0;
}





 

 类似资料: