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

#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. HTML的列表标签和表格标签

    网页的列表和表格 列表的分类 无序列表 有序列表 自定义列表 有序列表 <!--有序列表--><ol>    <li>辽宁</li>    <li ...

  2. 5.springboot-------Yaml

    springboot配置文件: Yaml是什么玩意? Yaml的基本语法 编写规则 1.大小写敏感,同json: 2.通过缩进表示层级关系:符号含义 注意事项:字符串默认不需要加上单引号或双引号. # ...

  3. 寒假安卓app开发学习记录(6)

    今天把之前学过的内容复习了一遍,然后用了大概一个小时的时间看了看教学视频. 学到的主要内容是如何调试Android程序,以及Android的应用以及打包过程.   调试: 依次点击Run“-”Atta ...

  4. testng的注解

    今天又学了点testng的新知识.原来在testng执行用例时,同一个class中的各个method按照字母顺序执行.为了实现自定义顺序执行,怎么办呢? 加入注解priority,举例如下: http ...

  5. OSI七层协议详解

    一.简介 开放系统互连参考模型 (Open System Interconnect 简称OSI)是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放式 ...

  6. 洛谷 P1659 [国家集训队]拉拉队排练(Manacher)

    题目链接:https://www.luogu.com.cn/problem/P1659 思路: 首先跑一遍Manacher,用$cnt_i$记录长为$i$的回文串有多少个. 所记录的$cnt$并不是最 ...

  7. 【转载】Java容器的线程安全

    转自:http://blog.csdn.net/huilangeliuxin/article/details/12615507 同步容器类 同步容器类包括Vector和Hashtable(二者是早期J ...

  8. H5实现查看图片和删除图片的效果

    在最近的项目中,H5需要实现查看图片和删除图片的效果,总结如下: 一.查看图片 查看图片使用weui的gallery.首先添加gallery的html,然后隐藏. <div class=&quo ...

  9. 378. 有序矩阵中第K小的元素

    Q: A: //O(NK) class Solution { public: int kthSmallest(vector<vector<int>>& matrix, ...

  10. android关闭屏幕时不锁屏实现

    所需权限: <uses-permission android:name="android.permission.DISABLE_KEYGUARD"/> 关闭系统锁屏服务 ...