During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

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 区间操作,合并,模板题的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

  2. poj3468区间延迟更新模板题

    #include<stdio.h> #include<string.h> #define N 100000 struct st{  int x,y;  __int64 yanc ...

  3. POJ 3225 Help with Intervals --线段树区间操作

    题意:给你一些区间操作,让你输出最后得出的区间. 解法:区间操作的经典题,借鉴了网上的倍增算法,每次将区间乘以2,然后根据区间开闭情况做微调,这样可以有效处理开闭区间问题. 线段树维护两个值: cov ...

  4. hdu1698 Just a Hook 【区间修改】(模板题)

    题目链接:https://vjudge.net/contest/182746#problem/E 题目大意: 一段线段由n条小线段组成,每次操作把一个区间的小线段变成金银铜之一(金的价值为3,银为2, ...

  5. dp优化-四边形不等式(模板题:合并石子)

    学习博客:https://blog.csdn.net/noiau/article/details/72514812 看了好久,这里整理一下证明 方程形式:dp(i,j)=min(dp(i,k)+dp( ...

  6. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...

  7. POJ - 3264 线段树模板题 询问区间最大最小值

    这是线段树的一个模板题,给出一串数字,然后询问区间的最大最小值. 这个其实很好办,只需把线段树的节点给出两个权值,一个是区间的最小值,一个是区间的最大值,初始化为负无穷和正无穷,然后通过不断地输入节点 ...

  8. CSU 1592 石子合并 (经典题)【区间DP】

    <题目链接> 题目大意: 现在有n堆石子,第i堆有ai个石子.现在要把这些石子合并成一堆,每次只能合并相邻两个,每次合并的代价是两堆石子的总石子数.求合并所有石子的最小代价. Input ...

  9. Zeratul的完美区间(线段树||RMQ模板题)

    原题大意:原题链接 给定元素无重复数组,查询给定区间内元素是否连续 解体思路:由于无重复元素,所以如果区间内元素连续,则该区间内的最大值和最小值之差应该等于区间长度(r-l) 解法一:线段树(模板题) ...

随机推荐

  1. 短视频服务大PK,阿里云、腾讯云、又拍云、七牛云、金山云5强横向对比

    继直播后,短视频又再次爆发,在这个风口,国内的各大云厂商也都相继推出了自己的一站式短视频解决方案.上周由于公司短视频功能开发的需要,对比了阿里云.腾讯云.又拍云.七牛云.金山云5家的短视频服务. 先介 ...

  2. ASP.NET Core的身份认证框架IdentityServer4(8)- 使用密码认证方式控制API访问

    前言 本文及IdentityServer这个系列使用的都是基于.net core 2.0的.上一篇博文在API项目中我使用了icrosoft.AspNetCore.Authentication.Jwt ...

  3. protobuf和thrift对比

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt383 数据类型 protobuf thrift protobuf thrif ...

  4. 自己编写服务启动脚本(一):functions文件详细分析和说明

    本文目录: 1.几个显示函数2.action函数3.is_true和is_false函数4.confirm函数5.pid检测相关函数 5.1 checkpid.__pids_var_run和__pid ...

  5. JAVA设计模式:状态模式

    声明:转载请说明来源:http://www.cnblogs.com/pony1223/p/7518226.html 一.引出状态模式 假设我们现在有一个糖果机项目,那么我们知道正常一般糖果机提供给用户 ...

  6. Linaro系统获取root权限方法

    在Zedboard上根据教程安装Linaro Ubuntu后出现一只无法获取Root权限,导致无法挂载U盘等问题. 具体体现在sudo -s命令之后,出现如sudo:must be setuid ro ...

  7. 第二次项目冲刺(Beta阶段)5.19

    1.提供当天站立式会议照片一张 会议内容: ①新成员加入,熟悉团队. ②制定新一轮的任务计划. 2.每个人的工作 (1)工作安排 队员 今日进展 明日安排 王婧 #42文件分类改为按个人分类 #42文 ...

  8. 201521123023《Java程序设计》第五周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过 ...

  9. 201521123097《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常与多线程相关内容. 2. 书面作业 1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中fin ...

  10. MySQL数据库引擎介绍、区别

    数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...