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

#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. 响应国家号召,AI助力疫情防控!顶象AI防疫方案获得国家人工智能标准化总体组认可

    当前,打赢新型冠状病毒感染的肺炎疫情是最重要的使命任务.而这场疫情的拉锯战,不仅要有全国人民共同努力.医护人员的无私奉献,还要积极运用现代科技的力量,用科学来战胜病魔.工信部也发文倡议:充分发挥人工智 ...

  2. selenium参数化-ddt模块

    DDT介绍: DDT(数据驱动测试)允许您通过使用不同的测试数据运行一个测试用例来使其倍增,并使它显示为多个测试用例.要使用DDT需要安装,安装命令:pip install ddt 使用方法: dd. ...

  3. 如何更改已经pushed的commit的注释信息(How to change the pushed commit message)

    1, 修改最后一次注释(Modify the last comment message) git commit -amend 2,修改之前的注释 1)输入: git rebase -i HEAD~3 ...

  4. scrapy 框架基本使用

    scrapy简介: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架. 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中.其最初是为了页面抓取 (更确切来说, 网络抓取 ...

  5. 解决async 运行多线程时报错RuntimeError: There is no current event loop in thread 'Thread-2'

    原来使用: loop = asyncio.get_event_loop()task = asyncio.ensure_future(do_work(checker))loop.run_until_co ...

  6. 优化 : Oracle数据库Where条件执行顺序 及Where子句的条件顺序对性能的影响

    .Oracle数据库Where条件执行顺序: 由于SQL优化起来比较复杂,并且还会受环境限制,在开发过程中,写SQL必须必须要遵循以下几点的原则: 1.ORACLE采用自下而上的顺序解析WHERE子句 ...

  7. springboot笔记-2-.核心的上下文以及配置扫描解析(上)

    前言 上一节中说明了springboot是如何做到自动发现配置的,那么本节看下spring如何创建上下文并解析这些配置,加载我们注册到容器管理中的类.上节已经成功的创建了SpringApplicati ...

  8. Springboot的多环境配置

    通常应用都会被安装到几个不同的环境(比如开发.测试.生产等),每个环境都会有一些参数是不一样的. Spring Boot对此也提供了支持,配置文件的名称只要满足application-{profile ...

  9. js jquery 页面初始化加载

    一.js 页面加载初始化方法 // 1.在body里面写初始化方法. <body onload='init()'> </body> <script type=" ...

  10. XFire调用CXF参数为Null的问题

    最近,领导分配了一个任务,做接口联调.情况是这样,对方客户升级了接口采用CXF,而我们还是用的XFire1.2.6,首先就遇到了这个问题:XFire调用CXF参数为Null的问题 . 在网上搜了一大堆 ...