【题目链接】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. mysql 高级应用

    1.MySQL like 模糊查询 例如:select * from emp where name like '张%'; 2.1MySQL UNION 操作符 SELECT country FROM ...

  2. Spring Boot 配置定时任务

    package com.zooper.demo; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j. ...

  3. bzoj 4552: [Tjoi2016&Heoi2016]排序——二分+线段树

    Description 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题 ,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这 ...

  4. bzoj 1594: [Usaco2008 Jan]猜数游戏——二分+线段树

    Description 为了提高自己低得可怜的智商,奶牛们设计了一个新的猜数游戏,来锻炼她们的逻辑推理能力. 游戏开始前,一头指定的奶牛会在牛棚后面摆N(1 <= N<= 1,000,00 ...

  5. Spring理论基础-控制反转和依赖注入

    第一次了解到控制反转(Inversion of Control)这个概念,是在学习Spring框架的时候.IOC和AOP作为Spring的两大特征,自然是要去好好学学的.而依赖注入(Dependenc ...

  6. delphi按钮文字换行

    delphi按钮有TButton和TBitButton,而TButton不支持换行,TBitButton支持 拖拽TBitButton按钮以后,按alt+F12进入找到TBitButton的capti ...

  7. ipython notebook环境搭建

    默认已经装好python基本环境,再进行下面步骤: 1. 下载安装IPython:  c:>pip.exe install ipython 系统就会去网上寻找ipython的包, 进行下载及安装 ...

  8. sqlmap参数说明

    --delay 设置每隔几秒测试一次注入 --safe-url 设置sqlmap要访问的正常url --safe-freq 设置每测试多少条注入语句后才去访问safe-url --code 设置能正常 ...

  9. 从零开始PHP攻略(000)——关于WAMPServer集成环境

    Apache.PHP和MySQL都可以用于多种操作系统和Web服务器的组合.本篇介绍在Windows下用WampServer环境包来搭建本地php环境. W:windows A:Apache M:My ...

  10. java===java基础学习(16)---final

    final-----概念 1.当不希望父类的某个方法被子类覆盖(override)时,可以用final关键字修饰. 2.当不希望类的某个变量的值被修改时,可以用final修饰.如果要用final,则必 ...