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. Java日期获取需求大全

    刚进公司,作为熟悉技术,为公司做了一个小的点餐系统的网站,其中大量用到了时间日期作为唯一标示或是显示设置.特总结了一下和大家分享. package com.lucis.ordering.Utils; ...

  2. resin远程调试配置,connection refused问题解决

    http://john521.iteye.com/blog/1879805 这篇博客中讲的很好. 我在配置的过程中遇到了几个问题,记录一下: 1. 调试服务器监听的端口号,百度的开发机都限制了端口在某 ...

  3. warning: in-class initialization of non-static data member is a C++11 extension [-Wc++11-extensions]

    struct test{int a=1111111; }test; vincentdeMacBook-Air:datasturte vincent$ g++ struct.cpp -o structp ...

  4. 在centOS7.2安装配置zabbix监控

    zabbix由两部分组成,zabbix-server与可选的zabbix-agent.zabbix-server可以通过SNMP,ZABBIX-AGENT,PING,端口监视等方法提供对远程服务器/网 ...

  5. c/c++程序员的技术栈

    在当今的互联网时代, java,  安卓, ios, 大行其道,而c/c++却显得越来越落寞. 其实这并不是c/c++程序员本身的问题,而是这是一个产品快速响应市场的年代.用过c/c++的人都知道, ...

  6. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  7. 原生js轮播图

    //用原生js实现了一个简单的轮播图效果 <!DOCTYPE html><html> <head> <meta charset="UTF-8&quo ...

  8. 在SrollView中嵌套GridView或ListView(转)

    原文链接:http://blog.csdn.net/gaojinshan/article/details/17055511 我想在同一个界面中,使用两个GridView,两个GridView一起上下滚 ...

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

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

  10. 使用Python-iGraph绘制贴吧/微博好友关系图

    想通过图形化的方式显示社交网络特定用户的好友关系,上网找了一下这方面的图形库有networkx.graphviz等,找了好久我选择了iGraph这个图形库. igraph在Windows下的安装稍微有 ...