线段树保存每个区间的左边最大连续长度和右边最大连续长度~

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int maxn=1e6+;
struct node {
int l,r;
int ll,rl,ml;
//左边开始连续的最大长度和右边开始连续的最大长度
//以及这个区间的最大连续长度
}segTree[maxn*];
void build (int i,int l,int r) {
segTree[i].l=l;
segTree[i].r=r;
segTree[i].ll=segTree[i].rl=segTree[i].ml=r-l+;
if (l==r) return;
int mid=(l+r)>>;
build (i<<,l,mid);
build (i<<|,mid+,r);
}
void update (int i,int t,int val) {
if (segTree[i].l==segTree[i].r) {
if (val==) segTree[i].ll=segTree[i].rl=segTree[i].ml=;
else segTree[i].ll=segTree[i].rl=segTree[i].ml=;
return;
}
int mid=(segTree[i].l+segTree[i].r)>>;
if (t<=mid) update (i<<,t,val);
else update (i<<|,t,val);
segTree[i].ll=segTree[i<<].ll;
segTree[i].rl=segTree[i<<|].rl;
segTree[i].ml=max(segTree[i<<].ml,segTree[i<<|].ml);
segTree[i].ml=max(segTree[i].ml,segTree[i<<].rl+segTree[i<<|].ll);
if (segTree[i<<].ll==segTree[i<<].r-segTree[i<<].l+) segTree[i].ll+=segTree[i<<|].ll;
if (segTree[i<<|].rl==segTree[i<<|].r-segTree[i<<|].l+)
segTree[i].rl+=segTree[i<<].rl;
}
int query (int i,int t) {
if (segTree[i].l==segTree[i].r||segTree[i].ml==||segTree[i].ml==segTree[i].r-segTree[i].l+)
return segTree[i].ml;
int mid=(segTree[i].l+segTree[i].r)>>;
if (t<=mid) {
if (t>=segTree[i<<].r-segTree[i<<].rl+)
return query (i<<,t)+query (i<<|,mid+);
else return query (i<<,t);
}
else {
if (t<=segTree[i<<|].l+segTree[i<<|].ll-)
return query (i<<|,t)+query(i<<,mid);
else return query (i<<|,t);
}
}
int a[maxn],top,n,m,x;
string s;
int main () {
while (~scanf ("%d %d",&n,&m)) {
build (,,n);
top=;
while (m--) {
cin>>s;
if (s=="D") {
scanf ("%d",&x);
a[top++]=x;
update (,x,);
}
else if (s=="Q") {
scanf ("%d",&x);
printf ("%d\n",query(,x));
}
else {
if (x>) {
x=a[--top];
update (,x,);
}
}
}
}
return ;
}

HDU1540 Turnal Warfare的更多相关文章

  1. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  2. hdu1540 Tunnel Warfare

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  3. HDU--1540 Tunnel Warfare(线段树区间更新)

    题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...

  4. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  5. hdu1540 Tunnel Warfare【线段树】

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  6. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  7. kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  8. HDU-1540 Tunnel Warfare(区间连续点长度)

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Time Limit: 4000/2000 MS (Java/Others)    Memory Limi ...

  9. HDU1540 Tunnel Warfare 水题

    分析:不需要线段树,set可过,STL大法好 #include <iostream> #include <cstdio> #include <cstring> #i ...

随机推荐

  1. docker启动容器报错:iptables failed

    问题描述: 启动Docker容器的时候 Error response / --dport -j DNAT --to-destination ! -i docker0: iptables: No cha ...

  2. 启动docker报Failed to start Docker Application Container Engine.解决

    [root@docker ~]# systemctl status docker.service● docker.service - Docker Application Container Engi ...

  3. DockerToolbox安装docker - Windows 10

    Wiindows安装docker 在转换成linux模式的时候不能使用,只能退而求其次安装Docker ToolBox在windows10中使用docker. 一.前言 由于本机使用的是win10家庭 ...

  4. ubuntu 切换用户

    app切换root ubuntu: sudo su - app sudo su - root centos : sudo su ############ root 切换app sudo su - ap ...

  5. Saber-图集

    PS:狙击手

  6. 小白艰难的Python图像的绘制

    1.贪吃蛇 代码: import turtle turtle.setup(650,350) turtle.penup() turtle.fd(-250) turtle.pendown() turtle ...

  7. java.lang.OutOfMemoryError: GC overhead limit exceeded异常处理

    今天写程序遇到个之前从没遇到的异常-----java.lang.OutOfMemoryError: GC overhead limit exceeded,下面附上解决方法 异常: 解决方法: 鼠标右击 ...

  8. Ansible - iventory

    概述 简单的 ansible 准备 ansible 安装 免密登录 约定 因为重点是 iventory, 所以只使用最简单的模块 ping 1. host-simple 概述 简单版 hosts 文件 ...

  9. ARM64架构下面安装mysql5.7.22

    MySQL下载地址为: https://obs.cn-north-4.myhuaweicloud.com/obs-mirror-ftp4/database/mysql-5.7.27-aarch64.t ...

  10. Spring Boot Json 之 Jackjson Fastjson

    Json 是目前互联网应用使用最为广泛的信息交换格式之一.Spring Boot 内置了 Jackson .Json 在应用中主要体现在以下功能: 序列化 反序列化 字段格式化 验证自动化 目前长用的 ...