[原创]华中农业大学第五届程序设计大赛 I Candies [线段树]【数据结构】
[原创]华中农业大学第五届程序设计大赛 I Candies [线段树]【数据结构】
2017-05-26 22:03:13 Tabris_ 阅读数:309
博客爬取于2020-06-14 22:40:30
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/72773996
题目链接:http://acm.hzau.edu.cn/problem.php?id=1207
——————————————————————————————————————
1207: Candies
Time Limit: 2 Sec Memory Limit: 1280 MB
Submit: 249 Solved: 39
[Submit][Status][Web Board]
Description
Xiao Ming likes those N candies he collects very much. There are two kinds of candies, A and B. One day, Xiao Ming puts his candies in a row and plays games. And he can replace the Lth candy to the Rth candy with the same kind of candies. Now, he wonder that if he eats the Lth candy to Rth candy, he can eat how many B candy continuously at most. For each Xiao Ming’s query, give the number of the B candy he can eat continuously at most.
Input
In the first line, there is an integer T, indicates the number of test cases.
For each case, there are two integers N and M in the first line, indicate the number of candies and the time of Xiao Ming’s operations.
The second line is a N-length string consist of character A and B, indicates the row of candies Xiao Ming put.
The next M line is Xiao Ming’s operations. There are two kind of operations:
1 L R v, indicate Xiao Ming replaces the Lth candy to the Rth candy with A candies (v==1) or B candies ( v == 2 ).
2 L R, indicate Xiao Ming wonder that there are how many continuous B candies between the Lth candy to the Rth candy most.
Output
In each case, the first line is “Case #k: “, k indicates the case number.
For each query, output the number of the most continuous B candies.
Sample Input
1
5 3
ABABB
2 1 3
1 2 3 2
2 1 3
Sample Output
Case #1:
1
2
————————————————————————————————————————————
题目大意:
给你一个序列,分别代表AB两种果实,
有两种操作,
1 将区间l,r 全变为(A或B)中操作
2 问你区间l,r 中连续的B最长是多长
解题思路:
线段树维护就好了
因为要有维护两个区间并的情况,所以要维护
sum 当前区间内连续的B的长度
ls 当前左边界开始向右连续B的长度
rs 当前右边界开始向左连续B的长度
lazy 懒标记
然后注意下维护的细节就好
Ps: 本来自己写了份代码,但是写到最后query的时候发现,合并时需要的信息得需要一个结构体来存诸,然而我写的时数组的。。。。。心态爆炸。。 最后厚颜无耻的贴了代码。。。
附本题代码
————————————————————————————————
1 | # include <bits/stdc++.h> |