题目连接

参考

题意:

维护各个点的连续的最大连续长度。

思路:

主要是维护一个区间的三个变量ll,f[i].l为起点向右的最大连续
长度,rl:f[i].r为起点向左的最大连续长度,ml:[l,r]区间内的
最大连续长度,便于合并。
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=100000+100;
struct node
{
int l,r;
int ll,rl,ml;
};
int a[maxn];
int sti[maxn];//sickc,数组的话更快,也更方便
int top=0;
node f[maxn*4];
void maketree(int i,int l,int r)
{
f[i].l=l;
f[i].r=r;
f[i].ll=f[i].rl=f[i].ml=r-l+1;//刚开始都连续
if(l==r)
return ;
int mid=(l+r)>>1;
maketree(i<<1, l, mid);
maketree(i<<1|1, mid+1, r);
}
void update(int i,int x,int v)
{
if(f[i].l==f[i].r)//叶子结点
{
f[i].ll=f[i].rl=f[i].ml=v;//v=0||1;
return ;
}
int mid=(f[i].l+f[i].r)>>1;
if(x<=mid) update(i<<1, x, v);
else update(i<<1|1, x, v);
//合并操作:!
//ml
f[i].ml=max(f[i<<1].ml,f[i<<1|1].ml);
f[i].ml=max(f[i].ml,f[i<<1].rl+f[i<<1|1].ll);
//ll
f[i].ll=f[i<<1].ll;
if(f[i].ll==f[i<<1].r-f[i<<1].l+1)
f[i].ll+=f[i<<1|1].ll;
//rl
f[i].rl=f[i<<1|1].rl;
if(f[i].rl==f[i<<1|1].r-f[i<<1|1].l+1)
f[i].rl+=f[i<<1].rl;
}
int query(int i,int x)//寻找连续区间的最大值
{
if(f[i].l==f[i].r||f[i].ml==f[i].r-f[i].l+1||f[i].ml==0)
//是叶子节点||都连续||都不连续直接返回ml
return f[i].ml;
int mid=(f[i].l+f[i].r)>>1;
if(x<=mid){
if(x>=mid-f[i<<1].rl+1)//主要看代码
return query(i<<1, x)+query(i<<1|1, mid+1);
else
return query(i<<1, x);
}
else {
if(x<=mid+f[i<<1|1].ll)
return query(i<<1, mid)+query(i<<1|1, x);
else
return query(i<<1|1, x);
}
}
int main ()
{
int n,m;
char s[5];
while(~scanf("%d%d",&n,&m))//WA一次
{
top=0;
maketree(1, 1, n);
for(int i=1;i<=m;i++)
{
scanf("%s",s);
if(s[0]!='R'){
int t;
scanf("%d",&t);
if(s[0]=='D'){
sti[++top]=t;
update(1, t, 0);
}
else if(s[0]=='Q')
printf("%d\n",query(1, t));
}
else
update(1, sti[top--], 1);
}
}
return 0;
}

HDU 1540<线段树,区间并>的更多相关文章

  1. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  2. HDU 1698 线段树 区间更新求和

    一开始这条链子全都是1 #include<stdio.h> #include<string.h> #include<algorithm> #include<m ...

  3. HDU - 1540 线段树的合并

    这个题题意我大概解释一下,就是一开始一条直线,上面的点全是联通的,有三种操作 1.操作D把从左往右第x个村庄摧毁,然后断开两边的联通. 2.询问Q节点相联通的最长长度 3.把最后破坏的村庄重建. 这个 ...

  4. E - Just a Hook HDU - 1698 线段树区间修改区间和模版题

    题意  给出一段初始化全为1的区间  后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...

  5. hdu 1698 线段树 区间更新 区间求和

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. hdu 3308(线段树区间合并)

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. HDU 3911 线段树区间合并

    北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...

  8. HDU - 1698 线段树区间修改,区间查询

    这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...

  9. HDU 5023线段树区间染色,统计区间内颜色个数

    这个也是一个线段树的模板 #include<iostream> #include<string.h> #include<algorithm> #include< ...

  10. hdu 1540 线段树

    这题的意思是现在有一些村庄成一条直线排列,现在有三个操作,D:摧毁一个指定的村庄,Q:询问与指定村庄相连的村庄个数, 就是这个村庄向左和向右数村庄数量,遇到尽头或损坏的村庄为止,这个就是与这个村庄相连 ...

随机推荐

  1. Java 4

    1.继承的问题 子类是父类的一个扩展,子类可以利用父类的属性与行为,这种情况子类会破坏父类的封装 为了保持父类良好的封装性,设计父类有以下规则: 如果要把某类设计为最终类则需要添加final修饰符,或 ...

  2. deb

    1.APT方式 (1)普通安装:apt-get install softname1 softname2 …; (2)修复安装:apt-get -f install softname1 softname ...

  3. thrift安装笔记

    Thrift源于大名鼎鼎的facebook之手,在2007年facebook提交Apache基金会将Thrift作为一个开源项目,对于当时 的facebook来说创造thrift是为了解决facebo ...

  4. 【转】Informix数据表结构分析资料整理之约束查询代码

    原文地址:http://blog.csdn.net/xqf222/article/details/6271219 本文主要整理了Informix数据库相关系统表数据,已分析整个Informix数据表结 ...

  5. Python & virtualenv使用说明

    virtualenv是virtual environment的缩写,可以创建独立的Python环境,用起来比较干净:   安装(已安装pip 或者 easy_install): 如果使用pip: pi ...

  6. WTL消息以及处理函数声明

    MSG_WM_CREATE LRESULT OnCreate(LPCREATESTRUCT lpCreateStruct); MSG_WM_INITDIALOG LRESULT OnInitDialo ...

  7. A - LCM Challenge

    A - LCM Challenge Time Limit: 2000/1000MS (Java/Others)    Memory Limit: 128000/64000KB (Java/Others ...

  8. ssh environment variable

    1 down vote When you run a command as an argument to ssh, the command is run directly by sshd; the s ...

  9. 移动端解决input focus后键盘弹出,高度被挤压的问题

    //解决弹出键盘页面高度变化bug var viewHeight = window.innerHeight; //获取可视区域高度 $("input").focus(functio ...

  10. opencv轮廓处理函数详细

    ApproxChains 用多边形曲线逼近 Freeman 链 CvSeq* cvApproxChains( CvSeq* src_seq, CvMemStorage* storage, int me ...