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 ...
随机推荐
- phpword根据模板导出word
参考网址:http://phpword.readthedocs.io/en/latest/installing.html 在composer.json中添加 { "require" ...
- java调用dll
@参考文章1,@参考文章2 根据上篇博客(参考文章2)java生成的dll测试 1,新建java项目,新建WebContent,子目录建WEB-INF\lib,加进jna-3.4.0.jar 新建ja ...
- java实现rabbitMQ延时队列详解以及spring-rabbit整合教程
在实际的业务中我们会遇见生产者产生的消息,不立即消费,而是延时一段时间在消费.RabbitMQ本身没有直接支持延迟队列功能,但是我们可以根据其特性Per-Queue Message TTL和 Dead ...
- c#发送短信
短息计费平台:http://sms.webchinese.cn/User/?action=key 代码: using System;using System.Collections.Generic;u ...
- 42-字符串到json 的错误 com.alibaba.fastjson.JSONObject cannot be cast to java.lang.String
json: {"updated_at":1551780617,"attr":{"uptime_h":3,"uptime_m&quo ...
- go语言sync包的学习(Mutex、WaitGroup、Cond)
package main; import ( "fmt" "sync" "runtime" "time" ) //加锁, ...
- 【APT】SqlServer游标使用
use [ElephantCredit] go begin transaction tran_bank; print '**脚本开始执行!'; declare @tran_error int , @n ...
- hangfire enqueued but not processing(hangfire 定时任务入队列但不执行)
不生效的方法 //RecurringJob.AddOrUpdate<FamilyAppService>((s) => s.UpdateFamilyLevel(), input.Cro ...
- 访问WebServcie遇到配额不足的时候,请增加配额
常常遇到的报错: 1.错误一: Error in deserializing body of reply message for operation 'GetArticleInfo'.,StackTr ...
- laravel中不使用 remember_token时退出报错,如何解决?
Route::get('auth/logout','Auth\AuthController@getLogout'); 这是laravel自带的退出功能只需要写这一条路由就行了,但是很可能爆出以下错误: ...