hdu1540线段树
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线段树的更多相关文章
- hdu-1540线段树刷题
title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...
- Tunnel Warfare(hdu1540 线段树)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Tunnel Warfare(HDU1540+线段树+区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...
- hdu1540线段树连续区间
模板题>.<当初学了一波又忘了 #include<map> #include<set> #include<cmath> #include<queu ...
- HDU1540(线段树统计连续长度)
---恢复内容开始--- Tunnel Warfare Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d &am ...
- hdu1540之线段树单点更新+区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu1540(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢 ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- kuangbin专题七 HDU1540 Tunnel Warfare (前缀后缀线段树)
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
随机推荐
- oracle 11g centos6 安装
选型:32位的内存是个瓶颈,已经是64位的时代了.使用64位的CentOS6 和 64位的Oracle 11g R2在虚拟机器安装,采用hostonly方式设置网络注意:能上网的网卡要设置一下ICS( ...
- [译]Selenium Python文档:五、Waits等待
大多数现代web应用都使用了AJAX技术.当浏览器加载一个页面的时候,该页面内的元素可能在不用的时间间隔内进行加载.这使得元素定位变得比较困难:如果一个元素还没有出现在DOM中,定位函数将会抛出一个E ...
- JavaScript原型模式-理解对象
一:简述 当初学编程一看到什么什么模式就比较头晕,不过本文我们通过简单的示例代码来说一下js 对象这个话题 ,来看下如何理解这个原型模式. 二:理解对象 1.简单对象 js对象中没有java.C#等类 ...
- Java对象的内存模型(一)
前言 新人一枚,刚刚入门编程不久,各方面都在学习当中,博文有什么错误的地方,希望我们可以多多交流! 最近,在开发App后台过程中,需要将项目部署到云服务器上.而云服务器的内存大小却只有1G.要如何做到 ...
- as3 操作图片,获取,设置实际像素,扣除透明区域
private var a:Loader = new Loader(); private function test():void { a.load(new URLRequest("asse ...
- 【转】ActionScript,Flash,Flash/Flex Builder,FlashPlayer,AIR,swf,swc,swz之间的区别
原文链接:http://zengrong.net/post/1295.htm ActionScript ActionScript通常简称为AS,它是Flash平台的语言.AS编写的程序,最终可以编译成 ...
- 在VB6/VBA中使用正则表达式
一.关于起因 最近在Office的QQ群里问如何在一串字符串中提取数值并加总的问题.如果使用正则表达式可以非常迅速的解决这个问题. 那么今天我就探讨一下在VB6/VBA中使用正则表达式的方法及代码,另 ...
- POPTEST 150801 祝大家前途似锦
POPTEST 150801 祝大家前途似锦 PT20150801学员不断在就业,同学们走好,远兵辛苦了!!!
- boostrap---btn
bootstrap入门教程,按钮.按钮风格.下拉菜单等 本教程演示: bootstrap框架的按钮.按钮大小.按钮风格.按钮组.下拉菜单制作等. 使用的版本:Bootstrap v2.3.2 . 下面 ...
- Android 瘦身之道 ---- so文件
Android 瘦身之道 ---- so文件 [TOC] 1. 前言 目前Android 瘦身只有几个方面可以入手,因为apk的结构就已经固定了. res 目录下的资源文件.(通常是压缩图片,比如 矢 ...