HZAU 1207 Candies(线段树区间查询 区间修改)
【题目链接】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(线段树区间查询 区间修改)的更多相关文章
- 线段树 区间查询区间修改 poj 3468
#include<cstdio> #include<iostream> #include<algorithm> #include<string.h> u ...
- hiho一下20周 线段树的区间修改
线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了 ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- hihoCode 1078 : 线段树的区间修改
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- hihoCoder #1078 : 线段树的区间修改(线段树区间更新板子题)
#1078 : 线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题 ...
- 培训补坑(day7:线段树的区间修改与运用)(day6是测试,测试题解以后补坑QAQ)
补坑咯~ 今天围绕的是一个神奇的数据结构:线段树.(感觉叫做区间树也挺科学的.) 线段树,顾名思义就是用来查找一段区间内的最大值,最小值,区间和等等元素. 那么这个线段树有什么优势呢? 比如我们要多次 ...
- hiho一下21周 线段树的区间修改 离散化
离散化 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在回国之后,重新过起了朝7晚5的学生生活,当然了,他们还是在一直学习着各种算法~ 这天小Hi和小Ho ...
- UVa 11992 Fast Matrix Operations (线段树,区间修改)
题意:给出一个row*col的全0矩阵,有三种操作 1 x1 y1 x2 y2 v:将x1 <= row <= x2, y1 <= col <= y2里面的点全部增加v: 2 ...
- hihoCoder week20 线段树的区间修改
区间修改 区间查询 最后一场比赛前的无可救药的热身 #include <bits/stdc++.h> using namespace std; #define mid ((l+r)/2) ...
随机推荐
- 【C++对象模型】第六章 执行期语意学
执行期语意学,即在程序执行时,编译器产生额外的指令调用,确保对象的构造,内存的释放,以及类型转换与临时对象的生成的安全进行. 1.对象的构造和析构 对于类对象的构造,一般在定义之后则开始内部的构造过程 ...
- 元类编程-- 实现orm,以django Model为例
# 需求 import numbers class Field: pass class IntField(Field): # 数据描述符 def __init__(self, db_column, m ...
- PACM Team(牛客第三场多校赛+dp+卡内存+打印路径)
题目链接(貌似未报名的不能进去):https://www.nowcoder.com/acm/contest/141/A 题目: 题意:背包题意,并打印路径. 思路:正常背包思路,不过五维的dp很容易爆 ...
- 调试应用程序(Debugging Applications)
调试应用程序(Debugging Applications)¶ Phalcon中提供了提供了几种调试级别即通知,错误和异常. 异常类 Exception class 提供了错误发生时的一些常用的调试信 ...
- Linux信号函数
1. signal函数: #include <signal.h> void (*signal(int signo, void (*func)(int)))(int); ret-成功返回信号 ...
- PHP 接入(第三方登录)QQ 登录 OAuth2.0 过程中遇到的坑
前言 绝大多数网站都集成了第三方登录,降低了注册门槛,增强了用户体验.最近看了看 QQ 互联上 QQ 登录的接口文档.接入 QQ 登录的一般流程是这样的:先申请开发者 -> 然后创建应用(拿到一 ...
- java===java基础学习(9)---方法参数
方法参数注意三要点: 一个方法不能修改一个基本数据类型的参数(数值型或者布尔型). 一个方法可以改变一个对象参数的状态. 一个方法不能让对象参数引用一个新的对象. package testbotoo; ...
- 安全测试===sqlmap(肆)转载
十八.杂项 1.使用简写 参数:-z 有些参数组合是被经常用到的,如“--batch --random-agent --ignore-proxy --technique=BEU”,这样写一大串很不好看 ...
- monkey测试===Monkey测试结果分析(系列三)转
Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...
- web.py输出中文字符串乱码问题的解决
web.py中文字符串网页显示乱码的解决 #!/usr/bin/env python # encoding: utf-8 import weburls = ( '/', 'index')class i ...