有一种绝望叫做无限PE hzau 1207 Candies (华中农业大学第五届程序设计大赛网络同步赛 I题)

1207: Candies

Time Limit: 2 Sec   Memory Limit: 1280 MB
Submit: 161   Solved: 12
[ 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. 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. 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

题意:

给你一数组,数组的值只有0(A糖果)和1(B糖果)两种,对这个数组有两种操作:(1)把区间L到R的值更改为v, (2)询问从区间L到R最多有多少个1是连续的。

很明显这是个线段树区间更新与及区间合并的问题,对于线段树上的每个区间L到R只要维护三个值:lb(从区间左边开始有多少个1是连续的),rb(从区间右边开始到有多少个1是连续的),mb(区间L,R中最多1连续的个数)。

但是不知为何,博主我PE了4发,并不能找到问题的所在,只能GG了。下面是PE的代码!!!

#include <bits/stdc++.h>
using namespace std;

const int maxn = 1e6 + 7;
struct node{
    int l, r, lazy, len;
    int lb, mb, rb;
    void Set_B(int key){ // 对区间内的 lb, mb, rb 进行更改
        if(key == 1) lb = mb = rb = 0;
        else lb = mb = rb = len;
    }
} tree[maxn<<2];
char z[maxn];

int Find_max(int s1, int s2, int s3){
    if(s2 > s1) s1 = s2;
    if(s3 > s1) s1 = s3;
    return s1;
}

void Push_up(int key){ // 向上更新, 区间合并
    int lson = key<<1, rson = key<<1|1;
    tree[key].lb = tree[lson].lb;
    tree[key].rb = tree[rson].rb;
    if(tree[lson].lb == tree[lson].len) tree[key].lb += tree[rson].lb;
    if(tree[rson].rb == tree[rson].len) tree[key].rb += tree[lson].rb;
    tree[key].mb = max(tree[lson].rb + tree[rson].lb, max(tree[lson].mb, tree[rson].mb));
}

void Push_down(int key){ // 向下更新
    if(tree[key].lazy != 0){
        int lson = key<<1, rson = key<<1|1;
        tree[lson].lazy = tree[rson].lazy = tree[key].lazy;
        tree[lson].Set_B(tree[key].lazy);
        tree[rson].Set_B(tree[key].lazy);
        tree[key].lazy = 0;
    }
}

void Buildtree(int l, int r, int key){ // 建树
    tree[key].l = l;
    tree[key].r = r;
    tree[key].len = r - l + 1;
    tree[key].lazy = 0;
    if(l == r){
        if(z[l] == 'A') tree[key].Set_B(1);
        else tree[key].Set_B(2);
        return;
    }
    int mid = (l + r) >> 1;
    Buildtree(l, mid, key<<1);
    Buildtree(mid+1, r, key<<1|1);
    Push_up(key);
}

void Update(int l, int r, int key, int w){ // 更新
    if(l <= tree[key].l && tree[key].r <= r){
        tree[key].lazy = w;
        tree[key].Set_B(w);
        return;
    }
    Push_down(key);
    int mid = (tree[key].l + tree[key].r) >> 1;
    if(l <= mid) Update(l, r, key<<1, w);
    if(mid < r) Update(l, r, key<<1|1, w);
    Push_up(key);
}

node Merge(node p, node q){ // 区间合并, 与向上更新相同
    node tmp;
    tmp.len = p.len + q.len;
    tmp.lb = p.lb;
    tmp.rb = q.rb;
    if(p.lb == p.len) tmp.lb += q.lb;
    if(q.rb == q.len) tmp.rb += p.rb;
    tmp.mb = max(p.rb + q.lb, max(p.mb, q.mb));
    return tmp;
}

node Query(int l, int r, int key){ // 询问
    if(l <= tree[key].l && tree[key].r <= r) return tree[key];
    Push_down(key);
    int mid = (tree[key].l + tree[key].r) >> 1;
    if(r <= mid) return Query(l, r, key<<1);
    else if(mid < l) return Query(l, r, key<<1|1);
    else return Merge(Query(l, mid, key<<1), Query(mid+1, r, key<<1|1));
}

int main(){
    int t, n, q, op, u, v, w;
    int num = 0;
    scanf("%d", &t);
    while(t --){
        scanf("%d %d", &n, &q);
        scanf("%s", z+1);
        Buildtree(1, n, 1);
        printf("Case #%d: \n", ++ num); // 这里很坑,如果后面没有空格会WA掉
        while(q --){
            scanf("%d %d %d", &op, &u, &v);
            if(op == 1){
                scanf("%d", &w);
                Update(u, v, 1, w);
            }
            else{
                node gg = Query(u, v, 1);
                printf("%d\n", Find_max(gg.lb, gg.mb, gg.rb));
            }
        }
    }
    return 0;
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值