hdu 1540(线段树区间合并)
题目链接:传送门
参考文章:传送门
题意:n个数字初始连在一条线上,有三种操作,
D x表示x号被摧毁;
R 表示恢复剩下的通路
Q表示查询标号为x所在的串的最长长度。
思路:线段树的区间合并。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
struct Node{
int l,r;
int ls,rs,ms;
}cur[maxn<<];
int ss[maxn],m,n;
void build(int x,int l,int r) //初始化建树
{
cur[x].l=l;cur[x].r=r;
cur[x].ls=cur[x].rs=cur[x].ms=r-l+;
if(l==r) return ;
int mid=(l+r)>>;
build(x*,l,mid);
build(x*+,mid+,r);
}
int MAX(int x,int y)
{
return x>y?x:y;
}
void update(int x,int pos,int fg)
{
if(cur[x].l==cur[x].r)
{
if(fg==) cur[x].ls=cur[x].rs=cur[x].ms=; //删除
else cur[x].ls=cur[x].rs=cur[x].ms=; //更新
return ;
}
int mid=(cur[x].l+cur[x].r)>>;
if(pos<=mid) update(x*,pos,fg);
else update(x*+,pos,fg);
//pushup操作,更新左子区间,右子区间的最长长度
cur[x].ls=cur[x*].ls;
cur[x].rs=cur[x*+].rs;
cur[x].ms=MAX(MAX(cur[x*].ms,cur[x*+].ms),cur[x*].rs+cur[x*+].ls);
if(cur[x*].ls==cur[x*].r-cur[x*].l+) cur[x].ls+=cur[x*+].ls; //如果区间的左子树的左区间已经满了,就加上右子树的左子区间
if(cur[x*+].rs==cur[x*+].r-cur[x*+].l+) cur[x].rs+=cur[x*].rs; // 如果右子树的右子区间已经满了,就加上左子树的右子区间
}
int query(int x,int pos)
{
if(cur[x].ms==||cur[x].l==cur[x].r||cur[x].ms==cur[x].r-cur[x].l+) return cur[x].ms;
int mid=(cur[x].l+cur[x].r)>>;
if(pos<=mid)
{
if(pos>=cur[x*].r-cur[x*].rs+) return query(x*,pos)+query(x*+,mid+);//如果在左子区间的右边界,就遍历两个区间
return query(x*,pos);
}
else
{
if(pos<=cur[x*+].l+cur[x*+].ls-) return query(x*+,pos)+query(x*,mid); //如果在右子区间的左边界,就遍历两个区间
return query(x*+,pos);
}
}
int main(void)
{
while(~scanf("%d%d",&n,&m))
{
build(,,n);
char op[];
int x,top=;
while(m--)
{
scanf("%s",op);
if(op[]=='D')
{
scanf("%d",&x);
ss[top++]=x;
update(,x,);
}
else if(op[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(,x));
}
else if(op[]=='R')
{
x=ss[--top];
update(,x,);
}
}
}
return ;
}
hdu 1540(线段树区间合并)的更多相关文章
- HDU 3911 线段树区间合并、异或取反操作
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...
- HDU - 1540 线段树的合并
这个题题意我大概解释一下,就是一开始一条直线,上面的点全是联通的,有三种操作 1.操作D把从左往右第x个村庄摧毁,然后断开两边的联通. 2.询问Q节点相联通的最长长度 3.把最后破坏的村庄重建. 这个 ...
- hdu 3308(线段树区间合并)
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 3911 线段树区间合并
北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...
- hdu 1806(线段树区间合并)
Frequent values Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- hdu 3308 线段树 区间合并+单点更新+区间查询
LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
- hdu 3911 Black And White (线段树 区间合并)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...
- HDU 3308 (线段树区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意: 两个操作 : 1 修改 单点 a 处的值. 2 求出 区间[a,b]内的最长上升子序列. 做法 ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
随机推荐
- APIcloud制作APP 微信支付与支付宝支付
首先要在云端绑定相应模块如alipay和wxpay其次编写代码. 配置区域 var cfg = { webName:'',//APP名字 payDebug:true, isUseWxPay:true, ...
- supervisor安装和简单使用
安装 pip install superviosr echo_supervisord_conf > /etc/supervisord.conf 编辑配置文件: [unix_http_server ...
- TZOJ 2722 Matrix(树状数组区间取反单点查询)
描述 Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row ...
- f5 SNMP配置
1.选择监控终端 2.配置团体名称:
- 联机分析处理ROLAP、MOLAP和HOLAP区别(转)
OLAP(on-Line Analysis Processing)是使分析人员.管理人员或执行人员能够从多角度对信息进行快速.一致.交互地存取,从而获得对数据的更深入了解的一类软件技术.OLAP的目标 ...
- C++ 智能指针shared_ptr的实现
#include <memory> #include <iostream> using namespace std; template<typename T> cl ...
- c#按照回车换行符分割字符串
string str="aaa\r\nbbscccjdddseee"; string[] sArray=str.Split(new char[2] {'\r','\n'}); 和用 ...
- Informatica_(2)第一个例子
PowerCenter Repository Manager1.启动客户端程序连接服务器打开客户端(PowerCenter Repository Manager)PCRM;存储库--配置域--添加新域 ...
- angular2.0学习笔记4.npm常用指令记录及angular语法
以下命令,都需要在命令行窗口中,先切入到项目文件夹目录,再执行 1.npm start 这个命令会在“监听”模式下运行TypeScript编译器,当代码变化时,它会自动重新编译. 同时,该命令还会在浏 ...
- L1-033 出生年(15)(STL-set代码)
L1-033 出生年(15 分) 以上是新浪微博中一奇葩贴:"我出生于1988年,直到25岁才遇到4个数字都不相同的年份."也就是说,直到2013年才达到"4个数字都不相 ...