hdu1540(线段树)
题目连接: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(线段树)的更多相关文章
- 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线段树
https://vjudge.net/contest/66989#problem/I #include<iostream> #include<cstdio> #include& ...
- 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 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 ...
随机推荐
- 基于visual Studio2013解决面试题之0502字符串左移
题目
- 微软HR泄露的asp.net面试题
1.面向对象的思想主要包括什么? 2.什么是ASP.net中的用户控件? 3.什么叫应用程序域?什么是受管制的代码?什么是强类型系统?什么是装箱和拆箱?什么是重载? 4.列举一下你所了解的XML技术及 ...
- C++学习之路—引用(一)—基础知识
(根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 对一个数据可以建立一个“引用”,它的作用是为一个 ...
- shell基础(转)
shell基础1:文件安全与权限 http://bbs.chinaunix.net/forum/viewtopic.php?t=434579&highlight=wingger 附:Linux ...
- PLSQL数据导入
导入数据 (1) 首先以管理员身份登录plsql; (2) 新建命令窗口 (3) 创建用户,设置帐号,密码 创建语句:create user usernameidenti ...
- HTML5开发桌面应用:选择node-webkit还是有道heX
近几年,移动应用和web2.0大行其道,相比之下.传统桌面应用程序开发显得相对冷清(包含该领域技术人才的后继力量),但在一些场景下,它依旧有其不可替代的优势. 将HTML5和Node.JS的技术优势. ...
- struts2由<s:tree>要么dtree小工具 建立树
一个 .<s:tree>方法: 1.引入新的标签: <%@ taglib prefix="sd" uri="/struts-dojo-tags" ...
- vc 按钮自绘
按钮自绘,将按钮区域分成三部分,左边.右边.中间都由贴图绘制,可用于手动进度条按钮,或者左右选择项按钮 cpp代码部分: // LRSkinButton.cpp : implementation fi ...
- 一劳永逸解决UAC问题(修改QMAKE_LFLAGS_EXE的设置)
如果你的程序跑在一个开启了UAC保护的系统中,而你的程序又没有"盾牌"的话,程序总是会受到各种阻挠的,比如读写文件,写注册表等. 有了"盾牌"的话就不会出现一些 ...
- sqlserver 自学笔记之 常量,变量及函数
(1)全局变量. 输入以下sql语句,根据查询结果,了解sqlserver全局变量的含义 select @@VERSIONselect @@CONNECTIONSselect @@CURSOR_ROW ...