题目连接: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. 【Bootstrap3.0建站笔记二】button可下拉弹出层

    1.button可下拉弹出层: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hpbmFwbGFu/font/5a6L5L2T/fontsize/400 ...

  2. grails一对一关联关系

    一对一关联关系开发中用的没有一对多那么广泛.可是我认为掌握以下还是有必要的.一对一关联关系有一张表存在外键,引用的通常是主表的主键.grails也对一对一关联关系提供了非常好的支持.配置也是简单的不得 ...

  3. web攻击方式和防御方法

    在http请求报文中载入攻击代码,就能发起对web应用的攻击.通过url查询字段或者表单.http首部.cookie等途径吧攻击代码传入,若这时web应用存在安全漏洞,那内部信息就会遭到窃取! 对we ...

  4. ubuntu 13.10 Rhythmbox不能播放mp3 和中文乱码的问题

    1.ubuntu 13.10 Rhythmbox不能播放mp3的解决方法 软件中心搜索(ubuntu额外的版权受限软件)不带括号 2.中文乱码问题解决方法: 终端顺序操作 : 1.  sudo ged ...

  5. sublime搜索和替换--正则

    Search and Replace Sublime Text features two main types of search: Search - Single File Search - Mul ...

  6. Java总结之容器

    [容器的概念] 容器:Java API所提供的一系列的实例,用于在程序中存放对象. [容器 API] J2SDK所提供的容器API位于java.util包内. {Collection[Set(Hash ...

  7. .Net 配置文件——继承ConfigurationSection实现自己定义处理类处理自己定义配置节点

    除了使用继承IConfigurationSectionHandler的方法定义处理自己定义节点的类.还能够通过继承ConfigurationSection类实现相同效果. 首先说下.Net配置文件里一 ...

  8. 简单的RPC java实现

    RPC的名声大噪之时是在2003年,那一个“冲击波”病毒(Blaster Worm virus)袭卷全球的一年.而“冲击波”正是用着RPC这把刀来敲开了远程电脑的大门.当然RPC 有更多正面的应用,比 ...

  9. cURL安装和使用笔记

    0.前言     cURL是一个利用URL语法在命令行下工作的文件传输工具.它支持文件上传和下载,所以是综合传输工具,但习惯称cURL为下载工具.cURL还包含了用于程序开发的libcurl.cURL ...

  10. 14.3.4 Phantom Rows 幻影行

    14.3.4 Phantom Rows 幻影行 所谓的幻读为发生在一个事务 当相同的查询产生不同的结果集在不同的时间. 比如,如果一个SELECT被执行2次, 但是第2次返回的记录不是第一次返回的记录 ...