题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1540

题意:是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢复上一次破坏的点。

线段树功能:单点修改,区间求值。

分析: pre数组记录区间左端点开始的最大连续个数,  suf数组记录区间右端点开始往左的最大的连续个数,sum数组表示该区间最大的连续点的个数。

sum[rt]最大值是左区间的最大值或右区间的最大值或左区间的suf+右区间的pre。

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define N 50010
#define FILL(a,b) (memset(a,b,sizeof(a)))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std; int sum[N<<],pre[N<<],suf[N<<];
int a[N];
void Pushup(int rt,int len)
{ pre[rt]=pre[rt<<];
suf[rt]=suf[rt<<|];
if(pre[rt<<]==(len-(len>>)))pre[rt]=pre[rt<<]+pre[rt<<|];
if(suf[rt<<|]==(len>>))suf[rt]=suf[rt<<]+suf[rt<<|];
sum[rt]=max(suf[<<]+pre[<<|],max(sum[rt<<],sum[rt<<|]));
}
void build(int l,int r,int rt)
{
if(l==r)
{
sum[rt]=pre[rt]=suf[rt]=;
return;
}
int m=(l+r)>>;
build(lson);
build(rson);
Pushup(rt,r-l+);
}
void update(int pos,int c,int l,int r,int rt)
{
if(l==r)
{
sum[rt]=suf[rt]=pre[rt]=c;
return;
}
int m=(l+r)>>;
if(pos<=m)update(pos,c,lson);
else update(pos,c,rson);
Pushup(rt,r-l+);
}
int query(int pos,int l,int r,int rt)
{
if(l==r)
return sum[rt];
int m=(l+r)>>;
if(pos<=m)
{
if(pos+suf[rt<<]>m)return suf[rt<<]+pre[rt<<|];
else return query(pos,lson);
}
else
{
if(m+pre[rt<<|]>=pos)return pre[rt<<|]+suf[rt<<];
else return query(pos,rson);
}
}
int main()
{
int n,m,x,tot;
char op[];
while(scanf("%d%d",&n,&m)>)
{
build(,n,);
tot=;
while(m--)
{
scanf("%s",op);
if(op[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(x,,n,));
}
else if(op[]=='D')
{
scanf("%d",&x);
a[++tot]=x;
update(x,,,n,);
}
else
{
x=a[tot--];
update(x,,,n,);
}
}
}
}

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线段树

    https://vjudge.net/contest/66989#problem/I #include<iostream> #include<cstdio> #include& ...

  5. hdu1540线段树连续区间

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

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

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

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

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

  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. perl 继承概述

    <pre name="code" class="html">[root@wx03 test]# cat Horse.pm package Horse ...

  2. Basic4android:多功能的Android应用软件快速开发平台

    Basic4android 是目前最简单.最强大的Android平台快速应用开发工具. ( "Basic4android is the simplest and most powerful ...

  3. 男性在下一100层【第三层】——高仿手机银行client接口

    前言: 从<男性在下一100层>系列博文[二楼]现在出版了整整三个月后,.从上述观点和这么多朋友的意见还是比较喜欢真实类的博文. 毕竟我们都叫"攻城狮".所以要看是否这 ...

  4. WPF程序长时间无人操作

    在软件开发中为了安全性,特别是那些需要用到用户名和密码登录服务端的程序,常常考虑长期无人操作,程序自动跳转到用户登录界面. 判断程序是否长时间无人操作,有两个依据,第一个是鼠标长时间不动,第二个是鼠标 ...

  5. HOOK自绘原理 good

    做“HOOK文件打开/保存对话框”的过程中,我首先研究了界面库的相关知识.界面库一般都是由C/C++这种中低级语言编码,这是因为在Windows下的界面库实现技术大都以直接操作控制Windows的消息 ...

  6. HTTP数据包头解析(简单清楚)

    [转]HTTP请求模型和头信息参考 参考: http://blog.csdn.net/baggio785/archive/2006/04/13/661410.aspx模型: http://blog.c ...

  7. Indy10.2.5的危险做法

    为了排查一个Bug今天无意看了看Indy源码,结果吓了一跳.TIdIOHandler.ReadLongWord函数用于读取通讯数据并转换成LongWord类型返回,它做用了一种危险的做法可能会导致数据 ...

  8. DELPHI 通過窗口句柄或窗口标题得到进程句柄

    DELPHI 通過窗口句柄或窗口标题得到进程句柄2009年05月08日 星期五 10:15procedure TForm1.Button1Click(Sender: TObject);varhWind ...

  9. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  10. 开源免费跨平台opengl opencv webgl gtk blender, opengl贴图程序

    三维图形的这是opengl的强项,大型3D游戏都会把它作为首选.图像处理,是opencv的锁定的目标,大多都是C的api,也有少部分是C++的,工业图像表现,图像识别,都会考虑opencv的.webg ...