hdu1540 区间操作,合并,模板题
Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!
InputThe first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.
There are three different events described in different format shown below:
D x: The x-th village was destroyed.
Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
R: The village destroyed last was rebuilt.
OutputOutput the answer to each of the Army commanders’ request in order on a separate line.
Sample Input
7 9
D 3
D 6
D 5
Q 4
Q 5
R
Q 4
R
Q 4
Sample Output
1
0
2
4 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
题解:线段树的合并,这道题就是需要合并,D的话破坏了这个村庄,那怎么办呢,就相当于中间切开,那么就是
相当于把当前这个点标记为0,然后再去更新它的父亲,这样怎么样会更加好做,好理解呢。
一段区间可以成为两段区间拼凑而成。
然后,我们定义老ls,rs表示左边连续区间,右边连续区间,ms表示最大连续区间,然后开始都为
整段,然后以D来切割,每段ls与rs最大值都不能超过当前这个点表示的区间
更新时方法:
左边就位左儿子的左边,右边就位右儿子的右边,然后最大值,就位左边,右边,和左儿子右边和
右儿子左边拼起来,就可以了。
查询操作:就是如果当前点,包涵在左边的右边,则可以和右子树的左儿子合并,同理,右边也一
样,然后最大值就是判断该段是否都联通了,是的话就直接可以推出返回值了,就
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int MAXN=;
char s[];
int tk[MAXN*]={},x,top,n,m;
struct fzy{
int ls,rs,ms,l,r;
}tree[MAXN*];
void build(int l,int r,int p){
tree[p].ls=tree[p].ms=tree[p].rs=r-l+;//一开始标记为所有
tree[p].l=l;tree[p].r=r;
if (l!=r)
{
int mid=(l+r)>>;
build(l,mid,p*);
build(mid+,r,p*+);
}
}
void change(int p,int t,int x)
{
if (tree[p].l==tree[p].r)
{
if (x==)
{
tree[p].ls=tree[p].ms=tree[p].rs=;
}
else
{
tree[p].ls=tree[p].ms=tree[p].rs=;
}
return;
}
int mid=(tree[p].l+tree[p].r)>>;
if (t<=mid) change(*p,t,x);
else change(*p+,t,x);
tree[p].ls=tree[p*].ls;
tree[p].rs=tree[p*+].rs;
tree[p].ms=max(max(tree[p*].ms,tree[p*+].ms),tree[p*].rs+tree[p*+].ls);
if (tree[p*].ls==tree[p*].r-tree[p*].l+)
tree[p].ls+=tree[p*+].ls;
if (tree[p*+].rs==tree[p*+].r-tree[p*+].l+)
tree[p].rs+=tree[p*].rs;
}
int query(int p,int x)
{
if (tree[p].l==tree[p].r||tree[p].ms==||tree[p].ms==tree[p].r-tree[p].l+) return tree[p].ms;
int mid=(tree[p].l+tree[p].r)>>;
if (x<=mid)
{
if (x>=tree[p*].r-tree[p*].rs+) return query(p*,x)+query(p*+,mid+);//表示还可以和右边连
else return query(p*,x);
}
else
{
if (x<=tree[p*+].l+tree[p*+].ls-) return query(p*+,x)+query(p*,mid);//表示还可以和左边连
else return query(p*+,x);
}
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
top=;
build(,n,);
for (int i=;i<=m;i++)
{
scanf("%s",&s);
if (s[]=='D')
{
scanf("%d",&x);
tk[++top]=x;
change(,x,);
}
else if (s[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(,x));
}
else
{
if (top>)
{
x=tk[top--];
change(,x,);
}
}
}
}
}
没什么了。
hdu1540 区间操作,合并,模板题的更多相关文章
- POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)
#include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...
- poj3468区间延迟更新模板题
#include<stdio.h> #include<string.h> #define N 100000 struct st{ int x,y; __int64 yanc ...
- POJ 3225 Help with Intervals --线段树区间操作
题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...
- hdu1698 Just a Hook 【区间修改】(模板题)
题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...
- dp优化-四边形不等式(模板题:合并石子)
学习博客:https://blog.csdn.net/noiau/article/details/72514812 看了好久,这里整理一下证明 方程形式:dp(i,j)=min(dp(i,k)+dp( ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- POJ - 3264 线段树模板题 询问区间最大最小值
这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...
- CSU 1592 石子合并 (经典题)【区间DP】
<题目链接> 题目大意: 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input ...
- Zeratul的完美区间(线段树||RMQ模板题)
原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...
随机推荐
- python环境搭建和打包
安装: python是有两个版本的一个是2.x,一个是3.x,这两个版本是不兼容的所有请使用前看准版本.下面我们主要说3.5版本. Mac:https://www.python.org/ftp/pyt ...
- ==与equal
@ 对象类型比较:(引用类型) ==和equal都表示对象引用的内存地址是否相同 对象类型继承Object并重写方法equal()用于对象的比较 @ 字符串比较: ==表示String引用的内存地址是 ...
- python正则表达式手记
----------re模块进行正则的使用---------- #result=re.match(正则表达式,要匹配的字符串):使用正则对字符串进行过滤从前面开始匹配#result.group():将 ...
- PHP发送邮件功能--ThinkPHP3.2.3
首先第一步 :在网上down了一个PHPMailer插件,插件地址>https://github.com/PHPMailer/PHPMailer下载解压后,这里我们只需要用到其中两个文件,如 ...
- Python Celery队列
Celery队列简介: Celery 是一个 基于python开发的分布式异步消息任务队列,通过它可以轻松的实现任务的异步处理, 如果你的业务场景中需要用到异步任务,就可以考虑使用celery. 使用 ...
- [转]Java7中的ForkJoin并发框架初探(下)—— ForkJoin的应用
详见: http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp86 前两篇文章已经对Fork Join的设计和JDK中源码的简要分析 ...
- 构建具有用户身份认证的 React + Flux 应用程序
原文:Build a React + Flux App with User Authentication 译者:nzbin 译者的话:这是一篇内容详实的 React + Flux 教程,文章主要介绍了 ...
- JAVA基础第十组(5道题)
46.[程序46] 题目:两个字符串连接程序 package com.niit.homework1; import java.util.Scanner; /** * @author: Annie * ...
- 【Beta】 第五次Daily Scrum Meeting
一.本次会议为第五次meeting会议 二.时间:10:00AM-10:20AM 地点:陆大楼 三.会议站立式照片 四.今日任务安排 成员 昨日任务 今日任务 林晓芳 帮助完善登录界面 对目前完成的模 ...
- Java零碎知识点
█ 举个例子:Iterator iter = map.entrySet().iterator(); xx.yy() ,表示一个xx对象的yy方法 ,xx.yy().zz()中 xx.yy()返回 ...