【题目链接】http://acm.hzau.edu.cn/problem.php?id=1207

【题意】给你一个字符串,然后两种操作:1,将区间L,R更新为A或者B,2,询问区间L,R最长的连续的B为多长。

【分析】典型线段树,每个节点维护该区间左边连续B的长度,右边连续B的长度,最长的连续B的长度,还有lazy标记。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int mod=1e9+;
const int N=1e6+;
int n,m,k,L,R,V;
char s[N];
int lazy[N<<];
struct Tree{
int l,r,maxn,len;
}a[N<<];
void pushdwn(int pos,int len){
if(lazy[pos]!=-){
lazy[pos<<]=lazy[pos<<|]=lazy[pos];
a[pos<<].l=a[pos<<].r=a[pos<<].maxn=lazy[pos]?len-(len>>):;
a[pos<<|].l=a[pos<<|].r=a[pos<<|].maxn=lazy[pos]?(len>>):;
lazy[pos]=-;
}
}
void pushup(int pos,int len){
a[pos].l=a[pos<<].l;
a[pos].r=a[pos<<|].r;
if(a[pos].l==len-(len>>))a[pos].l+=a[pos<<|].l;
if(a[pos].r==(len>>))a[pos].r+=a[pos<<].r;
a[pos].maxn=max(a[pos<<].r+a[pos<<|].l,max(a[pos<<].maxn,a[pos<<|].maxn));
}
void build(int pos,int l,int r){
lazy[pos]=-;
if(l==r){
a[pos].l=a[pos].r=a[pos].maxn=(s[l]=='B')?:;
return;
}
int mid=(l+r)>>;
build(pos<<,l,mid);
build(pos<<|,mid+,r);
pushup(pos,r-l+);
}
void update(int pos,int l,int r){
if(L<=l&&r<=R){
a[pos].l=a[pos].r=a[pos].maxn=V?r-l+:;
lazy[pos]=V;
return;
}
pushdwn(pos,r-l+);
int mid=(l+r)>>;
if(L<=mid)update(pos<<,l,mid);
if(R>mid)update(pos<<|,mid+,r);
pushup(pos,r-l+);
}
Tree merg(Tree t1,Tree t2){
Tree ret;
ret.len=t1.len+t2.len;
ret.l=t1.l;
ret.r=t2.r;
if(t1.l==t1.len) ret.l+=t2.l;
if(t2.r==t2.len) ret.r+=t1.r;
ret.maxn=max(t1.r+t2.l,max(t1.maxn,t2.maxn));
return ret;
}
Tree query(int pos,int l,int r){
if(L<=l&&r<=R){
a[pos].len=r-l+;
return a[pos];
}
pushdwn(pos,r-l+);
int mid=(l+r)>>;
if(mid>=R)return query(pos<<,l,mid);
if(mid<L)return query(pos<<|,mid+,r);
Tree t1=query(pos<<,l,mid),t2=query(pos<<|,mid+,r);
return merg(t1,t2);
}
int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++)
{
memset(a,,sizeof a);
printf("Case #%d:\n",cas);
scanf("%d%d%s",&n,&m,s+);
build(,,n);
int p;
while(m--)
{
scanf("%d%d%d",&p,&L,&R);
if(p==)
{
scanf("%d",&V);
V--;
update(,,n);
}
else printf("%d\n",query(,,n).maxn);
}
}
return ;
}

HZAU 1207 Candies(线段树区间查询 区间修改)的更多相关文章

  1. 线段树 区间查询区间修改 poj 3468

    #include<cstdio> #include<iostream> #include<algorithm> #include<string.h> u ...

  2. hiho一下20周 线段树的区间修改

    线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...

  3. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  4. hihoCode 1078 : 线段树的区间修改

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  5. hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)

    #1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...

  6. 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)

    补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...

  7. hiho一下21周 线段树的区间修改 离散化

    离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...

  8. UVa 11992 Fast Matrix Operations (线段树,区间修改)

    题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...

  9. hihoCoder week20 线段树的区间修改

    区间修改 区间查询 最后一场比赛前的无可救药的热身 #include <bits/stdc++.h> using namespace std; #define mid ((l+r)/2) ...

随机推荐

  1. HDU5875 Function

    题意:给定序列,有m个区间的询问,求每个询问a[l]%a[l+1]...%a[r]后的值.(N<=10^5) 思路:这题如果使用线段树,可能会由于姿势和卡常数原因TLE,由于数据好像比较奇怪(? ...

  2. [Luogu 3224] HNOI2012 永无乡

    [Luogu 3224] HNOI2012 永无乡 特别水一个平衡树题. 不认真的代价是调试时间指数增长. 我写的 SBT,因为 Treap 的 rand() 实在写够了. 用并查集维护这些点的关系, ...

  3. 记一次rsync日志报错directory has vanished

    中午两点的时候邮件告知rsync同部svn源库失败,看rsync日志报错显示如上,当时还在上课,没在公司,怀疑是不是有人动了svn的版本库,后来询问同事并通过vpn登录服务器上查看版本库是正常的,也没 ...

  4. mybatis 插入语句name no find

    1.可参考连接:https://www.cnblogs.com/thomas12112406/p/6217211.html 2.dao层的配置 void addUser(@Param("un ...

  5. js/jq 键盘上下左右回车按键

    js判断上下左右回车按键: document.onkeydown=function(e){ e=window.event||e; switch(e.keyCode){ case 37: //左键 co ...

  6. epoll内核源码分析

    转载:https://www.nowcoder.com/discuss/26226?type=0&order=0&pos=27&page=1 /*  *  fs/eventpo ...

  7. rtems-os-source

    http://blog.csdn.net/xpx3216/article/details/5776941 http://tech.hqew.com/fangan_421204 https://gith ...

  8. python爬虫模块之调度模块

    调度模块也就是对之前所以的模块的一个调度,作为一个流水的入口. 下面的代码的获取数据部分暂时没有写,细节部分在实际开发中,要根据要求再定义,这里说的是使用方法 from savedb import D ...

  9. 【UOJ#9】vfk的数据

    我感觉这题可以出给新高一玩2333 #include<bits/stdc++.h> #define N 10005 using namespace std; string s[N]; in ...

  10. C高级 跨平台协程库

    1.0 协程库引言 协程对于上层语言还是比较常见的. 例如C# 中 yield retrun, lua 中 coroutine.yield 等来构建同步并发的程序. 本文就是探讨如何从底层实现开发级别 ...