hdu1540 区间操作,合并,模板题

时间:2022-09-25 19:53:35
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&lpar;线段树区间更新,模板题,求区间和&rpar;

    #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优化-四边形不等式&lpar;模板题:合并石子&rpar;

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

  6. BZOJ 3223&colon; Tyvj 1729 文艺平衡树-Splay树&lpar;区间翻转&rpar;模板题

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

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

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

  8. CSU 1592 石子合并 &lpar;经典题&rpar;【区间DP】

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

  9. Zeratul的完美区间&lpar;线段树&vert;&vert;RMQ模板题&rpar;

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

随机推荐

  1. DB设计原则(一)字段名定义避免二义性。

    字段名定义避免二义性.如:主数据中有库存信息表 KCDDID,KCDDMC.若要在业务表中存储库存地点ID的话,不要定义为KCDD,要定义为KCDDID.这样标明存储的就是ID,而不是名称.同样,单位 ...

  2. javascript中关于坐标 大小 的描述

    window对象 有效桌面的大小,除去桌面下面的任务栏的高度 window.screen.availHeight : window.screen.availWidth :   浏览器窗口的左上角相对于 ...

  3. Android的AndroidManifest&period;xml文件的详解

    一.关于AndroidManifest.xml AndroidManifest.xml 是每个android程序中必须的文件.它位于整个项目的根目录,描述了package中暴露的组件(activiti ...

  4. Spring Boot-------项目搭建及注解

    Spring Boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需 ...

  5. python-装饰器简述

    装饰器是什么 用来修饰别的函数的函数就可以称之为装饰器 这种函数的参数一般就是另外一个函数 也就是说,调用这种函数,需要给这种函数传参,且参数是函数 @语法糖 @语法糖一般用来表示装饰器函数 不用@也 ...

  6. Installation of CarbonData 1&period;1&period;0 with Spark 1&period;6&period;2

    关键词:carbondata spark thrift 数据仓库 [Install thrift 0.9.3] 注意 要装thrift-java必须先装ant . 有人说要装boost,我在cento ...

  7. Python学习之路&mdash&semi;&mdash&semi;&mdash&semi;&mdash&semi;&mdash&semi;day04

    今日内容: 1. 循环语句 1.1 if判断 1.2 while循环 1.3 for循环 一.if判断 语法一: if 条件 代码块1 代码块2 代码块3 # 例: sex='female' age= ...

  8. php 4种传值方式

    我们定义page01.php和page02.php两个php文件,将page01中的内容想办法传递到page02,然后供我们继续使用. 第一种:     使用客户端浏览器的cookie.cookie很 ...

  9. rm 命令&lpar;转&rpar;

    原文:http://www.cnblogs.com/peida/archive/2012/10/26/2740521.html rm命令.rm是常用的命令,该命令的功能为删除一个目录中的一个或多个文件 ...

  10. Qt中将QString转换为char &ast;或者相反

    1.将QString转换为std::string,可以通过QString的成员函数toStdString() QString Qstr="123";std::string str= ...