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 ...
随机推荐
- c#后台弹出提示
Page.ClientScript.RegisterClientScriptBlock(typeof(string), "", @"<script>alert ...
- smartforms换页,
smartforms换页,可以把最后的窗口设置成最终窗口, 这样页码有可以用系统变量控制: 总页码/页码 :&SFSY-FORMPAGES&
- 在 SSIS package 中使用FTP
在ssis 包中使用FTP 实际上很简单, 直接拿一个FTP控制流(FTP 任务) ,配置一下FTP 服务器就可以了, 但是当我想在SQL Server Job 中使用这个功能时却报了个错(如下), ...
- cocos2d-x学习日志(10) --射击游戏(喵星战争)
转载请标明:转载自[小枫栏目],博文链接:http://blog.csdn.net/rexuefengye/article/details/10553487 一.纵版射击游戏的特点 纵版射击游戏是一种 ...
- 工信部表态支持Linux,可是Linux又是什么呢?
近日,工信部高层官员出面表态:工信部大力支持发展国产Linux操作系统,可是,Linux又是什么呢?假设依照工信部的说法,发展所谓"国产Linux".恐怕要给国家带来麻烦. 大家知 ...
- [Java][Android][Process] ProcessBuilder与Runtime差别
在Android中想要进行Ping,在不Root机器的情况下似乎还仅仅能进行底层命调用才干实现. 由于在Java中要进行ICMP包发送须要Root权限. 于是仅仅能通过创建进程来攻克了.创建进程在Ja ...
- 【从cocos2d-x学习设计模式】第一阶段:辛格尔顿
设计模式,它总结了前辈在许多方案重用代码.它是一个想法. 因为我们爱cocos2d-x,然后我们从去cocos2d-x在设计模式中,右一起学习!本篇解释未来辛格尔顿. 提cocos2d-x中间Dire ...
- [iOS]简单的APP引导页的实现 (Swift)
在第一次打开APP或者APP更新后通常用引导页来展示产品特性 我们用NSUserDefaults类来判断程序是不是第一次启动或是否更新,在AppDelegate.swift中加入以下代码: func ...
- SVN的项目管理
基于SVN的项目管理——集中与分散 我们在此处不讨论 GIT 比 SVN 好多少,也不讨论 Maven 和 Gradle 哪个好用,基于现有的开发环境,大多数公司还是采用 SVN + Maven ...
- 每天一个JavaScript实例-动态省份选择城市
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...