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. java多线程sleep和wait方法的区别

    分别创建了三个类,一个测试类,两个线程类实现Runnable接口. 当有notify()唤醒线程时,执行的结果如下: 当把TestSleepaWait.class.notify();语句注释后,即没有 ...

  2. GeoServer初识与安装

    学习过程中发现官网上的东西足够基础了,所以在这只做一下索引和补充. 官方网址:http://live.osgeo.org/zh/overview/geoserver_overview.html 安装: ...

  3. C++中关于重载默认构造函数与默认全部参数的构造函数的使用注意

    # include<iostream>using namespace std;class Time{public:                            //公用成员函数  ...

  4. 深入剖析ConcurrentHashMap 一

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt201 ConcurrentHashMap是Java5中新增加的一个线程安全的 ...

  5. poj2914无向图的最小割模板

    题意:给出无向图的点,边,权值.求最小割. 思路:根据题目规模,最大流算法会超时. 网上参考的模板代码. 代码: /*最小割集◎Stoer-Wagner算法:一个无向连通网络,去掉一个边集可以使其变成 ...

  6. JavaScript学习日志(四):BOM

    BOM的核心对象就是window,这一章没什么好说的,总结一些比较常用的: 1,a未定义,a; //报错window.a; //undefined 不能用delete删除全局变量 2,html5不支持 ...

  7. YYHS-NOIP2017SummerTraining0914-问题 A: 组合数问题

    题目描述 组合数C(n,m)表示的是从n个物品中选出m个物品的方案数.举个例子,从(1, 2, 3)三个物品中选择两个物品可以有(1, 2),(1, 3),(2, 3)这三种选择方法.根据组合数的定义 ...

  8. UTF-8笔记170330

    unicode 为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本转换.处理的 UTF-8使用可变长度字节来储存 Unicode字符,例如ASCII字母继续使用1字节储 ...

  9. 团队作业8----第二次项目冲刺(Beta阶段) 第一天

    BETA阶段冲刺第一天 1.开了个小会议 2.每个人的工作 (1) 昨天已完成的工作: 今天是第一天,所以是新的开始. (2) 今天计划完成的工作: (3) 工作中遇到的困难: 由于有新的成员加入,默 ...

  10. 201521123113 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. -继承设计的技巧 1.将公共操作和属性放在父类 2.不要使用protected修 ...