https://vjudge.net/contest/66989#problem/I

#include<iostream>
#include<cstdio>
#include<cmath>
#include<stack>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
const int maxn=;
LL value[maxn<<],ll[maxn<<],rl[maxn<<],ml[maxn<<];//ll是rt左边连续为1的个数,rl是右边,ml是左右两边
void btree(int l,int r,int rt)
{
ll[rt]=rl[rt]=ml[rt]=r-l+;//初始化全部为1
if(l==r)return ;
int m=(l+r)>>;
btree(ls);
btree(rs);
}
void update(int k,int flag,int l,int r,int rt)//flag=0代表摧毁,=1代表重建
{
if(l==r)
{
ll[rt]=rl[rt]=ml[rt]=flag;//更新叶子节点的值
return ;
}
int m=(l+r)>>;
if(k<=m)update(k,flag,ls);
else update(k,flag,rs);
if((ll[rt]=ll[rt<<])==m-l+)ll[rt]+=ll[rt<<|];//更新左边
if((rl[rt]=rl[rt<<|])==r-m)rl[rt]+=rl[rt<<];
ml[rt]=max(max(ml[rt<<],ml[rt<<|]),rl[rt<<]+ll[rt<<|]);//更新ml的值
}
LL query(int k,int l,int r,int rt)
{
if(l==r||ml[rt]==r-l+||ml[rt]==)return ml[rt];
int m=(l+r)>>;
if(k<=m)//查询点在左侧
{
if(k>m-rl[rt<<])return query(k,ls)+query(m+,rs);
else return query(k,ls);
}
else
{
if(k<m++ll[rt<<|])return query(m,ls)+query(k,rs);
else return query(k,rs);
}
}
int main()
{
int n,m,k;
while(~scanf("%d%d",&n,&m)){
stack<int>a;
btree(,n,);
while(m--){
char op[];
scanf("%s",&op);
if(op[]!='R')
{
scanf("%d",&k);
if(op[]=='D')
{
a.push(k);
update(k,,,n,);
}
if(op[]=='Q')printf("%lld\n",query(k,,n,));
}
else
{
update(a.top(),,,n,);
a.pop();
}
}
}
return ;
}

hdu1540线段树的更多相关文章

  1. hdu-1540线段树刷题

    title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...

  2. Tunnel Warfare(hdu1540 线段树)

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

  3. Tunnel Warfare(HDU1540+线段树+区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...

  4. hdu1540线段树连续区间

    模板题>.<当初学了一波又忘了 #include<map> #include<set> #include<cmath> #include<queu ...

  5. HDU1540(线段树统计连续长度)

    ---恢复内容开始--- Tunnel Warfare Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d &am ...

  6. hdu1540之线段树单点更新+区间合并

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

  7. hdu1540(线段树)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢 ...

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

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

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

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

随机推荐

  1. PHP语言开发微信公众平台(订阅号)之开启开发者模式

    (1)打开上一篇我们从花生壳官网获得的外网网址就会看到localhost根目录下的文件(这里不再赘述php环境的搭建).注:因为外网网址在能联网时,访问外网网址的任何人都能看到根目录下的所有文件,不仅 ...

  2. [LeetCode] Trapping Rain Water II 题解

    题意 题目 思路 我一开始想的时候只考虑到一个结点周围的边界的情况,并没有考虑到边界的高度其实影响到所有的结点盛水的高度. 我们可以发现,中间是否能够盛水取决于边界是否足够高于里面的高度,所以这必然是 ...

  3. MapReduce简介以及详细配置

    1.MapReduce(一个分布式运算框架)将数据分为数据块,发送到不同的节点,并行方式处理. 2.NodeManager和DataNode在一个节点上,程序与数据在一个节点. 3.内容分为两个部分 ...

  4. 常见排序算法-Python实现

    常见排序算法-Python实现 python 排序 算法 1.二分法     python    32行 right = length-  :  ]   ):  test_list = [,,,,,, ...

  5. 小程序新能力-个人开发者尝鲜微信小程序

    个人开发者的福利 微信小程序,刚听到这个新名词的时候,我就兴冲冲的去找入口,看看自己能不能搞个微信小程序的HelloWorld,毕竟能在微信上把自己写的一些小工具跑起来还是满炫酷的. 没想,网上一查, ...

  6. jQuery对象长度size

    jQuery对象有两个方法获取其长度,一个是length属性,一个是调用size()方法,据说前者的效率比后者的高. 代码如下: var DQNRList=$("a[id^='DQNR']& ...

  7. swfobject.js加载swf,关于是否加加载完成;

    这几个帖子可以参考下 http://bbs.9ria.com/forum.php?mod=viewthread&page=1&tid=38913 http://bbs.9ria.com ...

  8. 【Hexo】Hexo+Github构建个人博客 (五):错误集

    一.报错: ERROR Plugin load failed: hexo-deployer-git 解决方案:执行命令  npm install hexo-deployer-git --save 二. ...

  9. 一步操作配置Word环境

    我们在开始编辑Word文档的时候,会发现有些事情我们总需要预先操作一下,比如100%普通视图.显示隐藏特殊标记.显示标尺.最大化窗口.显示导航窗格啥的,每次去操作,感觉很烦,那么通过下面这个宏代码可以 ...

  10. Spring基础学习(四)—AOP

    一.AOP基础 1.基本需求      需求: 日志功能,在程序执行期间记录发生的活动. ArithmeticCalculate.java public interface ArithmeticCal ...