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) 解法一:线段树(模板题) ...
随机推荐
- css入门基础知识
一.CSS常用选择器 /*CSS注释*/ /*CSS修改页面中的所有标签必须借助选择器选中. 选择器中可以写多对CSS属性:每个属性名与属性值之间用:分隔,多对属性之间,必须用;分隔 选择器{ 属性1 ...
- JSP小记
0.配置servlet的模板 1.找Jar文件:\Common\plugins (myeclipse.ini文件中查看) * com.genuitec.eclipse.wizards*.jar 2.找 ...
- Spring mybatis源码篇章-XMLLanguageDriver解析sql包装为SqlSource
前言:通过阅读源码对实现机制进行了解有利于陶冶情操,承接前文Spring mybatis源码篇章-MybatisDAO文件解析(二) 首先了解下sql mapper的动态sql语法 具体的动态sql的 ...
- 8个超震撼的HTML5和纯CSS3动画源码
HTML5和CSS3之所以强大,不仅因为现在大量的浏览器的支持,更是因为它们已经越来越能满足现代开发的需要.Flash在几年之后肯定会消亡,那么HTML5和CSS3将会替代Flash.今天我们要给大家 ...
- 201521123020《Java程序设计》第8周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1.List中指定元素的删除(题目4-1) 1.1 实验总结 运用到了equ ...
- Java程序设计第三周学习总结
1. 本周学习总结 2. 书面作业 Q1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...
- 201521123006 《java程序设计》 第14周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...
- 201521123055 《Java程序设计》第11周学习总结
1. 本章学习总结 2. 书面作业 Q1.互斥访问与同步访问 ** 完成题集4-4(互斥访问)与4-5(同步访问) ** 1.1 除了使用synchronized修饰方法实现互斥同步访问,还有什么办法 ...
- dbda数据库类
<?phpclass DBDA{ public $host="localhost";//服务器地址 public $uid="root";//用户名 pu ...
- Hibernate注解配置与XML配置区别
注解配置的方式与xml很很多类似: 首先是需要加入4个jar包:hibernate-commons-annotations.jar . hibernate-annotations.jar.ejb3-p ...