【题目链接】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. 【BZOJ1598】牛跑步 [A*搜索]

    牛跑步 Time Limit: 10 Sec  Memory Limit: 162 MB[Submit][Status][Discuss] Description BESSIE准备用从牛棚跑到池塘的方 ...

  2. springcloud(一):大话Spring Cloud(山东数漫江湖)

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  3. MongoDB 数据库(2)

    db.collectionName 集合对象 获取集合对象 db.getCollection('collection_name') e.g. db.getCollection("class0 ...

  4. C#读取txt文件时中文乱码

    解决办法 使用GB2312中文字符集 StreamReader reader = new StreamReader(txtUrl, Encoding.GetEncoding("gb2312& ...

  5. Spring的BeanFactory体系结构(一)

    本文使用的代码是: Spring 3.0 接 触Spring也有很长一段时间了.但是,每次都是直接使用Spring直接提供的API,时间久了,自然也会想探索Spring里面的奥秘.今天上 午,整理出了 ...

  6. 网络应用框架Netty快速入门

    一 初遇Netty Netty是什么? Netty 是一个提供 asynchronous event-driven (异步事件驱动)的网络应用框架,是一个用以快速开发高性能.可扩展协议的服务器和客户端 ...

  7. 2017-2018-1 20179205《Linux内核原理与设计》第六周作业

    <Linux内核原理与设计> 视频学习及操作 给MenuOS增加time和time-asm命令的方法: 1.更新menu代码到最新版 rm menu -rf //强制删除menu, rm ...

  8. 有向有权图的最短路径算法--Dijkstra算法

    Dijkstra算法 1.定义概览 Dijkstra(迪杰斯特拉)算法是典型的单源最短路径算法,用于计算一个节点到其他所有节点的最短路径.主要特点是以起始点为中心向外层层扩展,直到扩展到终点为止.Di ...

  9. Linux System.map文件【转】

    转自:http://blog.csdn.net/ysbj123/article/details/51233618 当运行GNU链接器gld(ld)时若使用了"-M"选项,或者使用n ...

  10. jython

    # -*- coding: utf-8 -*- import sys import json sys.path += ["C:/Users/yangbo/Desktop/restassure ...