HDU1540(线段树统计连续长度)
---恢复内容开始---
Tunnel Warfare
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
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!
Input
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.
Output
Sample Input
Sample Output
#include"cstdio"
#include"algorithm"
using namespace std;
const int MAXN=;
struct node{
int l,r;
int ll,rl,ml;
}a[MAXN*];
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].ll=a[rt].rl=a[rt].ml=r-l+;
if(l==r) return ;
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
} void merge(int rt)
{
//若左子树已满,则应与右子树左区间合并
a[rt].ll=a[rt<<].ll;
if(a[rt<<].ll==(a[rt<<].r-a[rt<<].l+))
{
a[rt].ll+=a[(rt<<)|].ll;
} //若右子树已满,则应与左子树的有区间合并
a[rt].rl=a[(rt<<)|].rl;
if(a[(rt<<)|].rl==(a[(rt<<)|].r-a[(rt<<)|].l+))
{
a[rt].rl+=a[rt<<].rl;
}
//该子树的最大连续长度为 左或右子树连续长度的最大者 或者 将左子树右区间与右子树左区间合并的长度
a[rt].ml=max(max(a[rt<<].ml,a[(rt<<)|].ml),a[rt<<].rl+a[(rt<<)|].ll);
} void update(int rt,int pos,int val)
{
if(a[rt].l==a[rt].r)
{
if(val) a[rt].ll=a[rt].rl=a[rt].ml=;
else a[rt].ll=a[rt].rl=a[rt].ml=;
return ;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid) update(rt<<,pos,val);
else update((rt<<)|,pos,val);
merge(rt);
} int query(int rt,int pos)
{
if(a[rt].l==a[rt].r||a[rt].ml==||a[rt].ml==a[rt].r-a[rt].l+)//到达叶子节点或者该节点已满或空,那么不必向下走了
{
return a[rt].ml;
} int mid=(a[rt].l+a[rt].r)>>; if(pos<=mid)//在左子树中
{
if(pos>=a[rt<<].r-a[rt<<].rl+)//若在左子树的右区间
{
return query(rt<<,pos)+query((rt<<)|,mid+);//则需要查询右子树
}
else
{
return query(rt<<,pos);
} }
else
{
if(pos<=a[(rt<<)|].l+a[(rt<<)|].ll-)//若在右子树的左区间
{
return query((rt<<)|,pos)+query(rt<<,mid);//则需要查询左子树
}
else
{
return query((rt<<)|,pos);
}
}
} int stack[MAXN];
int top; int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int pos;
top=;
build(,,n);
while(m--)
{
scanf("%*c");
char op;scanf("%c",&op);
if(op=='D')
{
scanf("%d",&pos);
stack[top++]=pos;
update(,pos,);
}
else if(op=='Q')
{
scanf("%d",&pos);
printf("%d\n",query(,pos));
}
else
{
pos=stack[--top];
update(,pos,);
}
}
}
}
---恢复内容结束---
HDU1540(线段树统计连续长度)的更多相关文章
- hdu-1540线段树刷题
title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...
- ZOJ 2301 / HDU 1199 Color the Ball 离散化+线段树区间连续最大和
题意:给你n个球排成一行,初始都为黑色,现在给一些操作(L,R,color),给[L,R]区间内的求染上颜色color,'w'为白,'b'为黑.问最后最长的白色区间的起点和终点的位置. 解法:先离散化 ...
- HDU 6070 (线段树)(统计颜色)
HDU 6070 Partition Problem : 给一段长度为n的序列,要求找出一段区间,使得这段区间的数字种类除以区间长度最小.输出最后的答案即可.(n <= 60000)(9s时限) ...
- Tunnel Warfare(hdu1540 线段树)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- Tunnel Warfare(HDU1540+线段树+区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目: 题意:总共有n个村庄,有q次操作,每次操作分为摧毁一座村庄,修复一座村庄,和查询与询问的 ...
- hdu1540线段树
https://vjudge.net/contest/66989#problem/I #include<iostream> #include<cstdio> #include& ...
- Tunnel Warfare--- hdu1540 线段树求连续子区间
题目链接 题意:有n个村庄,编号分别为1-n:由于战争会破坏村庄,但是我们也会修复: D x代表村庄x被破坏: Q x是求与x相连的有几个没有被破坏: R 是修复最后一次被破坏的村庄: 接下来有m个操 ...
- [题解](线段树最大连续子段和)POJ_3667_Hotel
题意:1.求一个最靠左的长x的区间全部为0,并修改为1,输出这个区间的左端点 2.修改一个区间为0 实际上是维护最大连续子段和,原来也写过 大概需要维护一个左/右最大子段和,当前这段最大子段长,再维护 ...
- hdu1540线段树连续区间
模板题>.<当初学了一波又忘了 #include<map> #include<set> #include<cmath> #include<queu ...
随机推荐
- WinForm程序打包工具InnoSetup使用说明图文教程
WinForm程序打包工具InnoSetup使用说明图文教程 WinForm程序开发测试好了,如果将Debug/Release里面的文件发给客户使用,会让客户觉得你不够专业,但是使用VS自带的打包工具 ...
- SecureCRT的上传和下载
securtCRT对于后台开发者并不陌生,在windows下是得力的助手.而文件从服务器上上传和下载是很基本.很日常的操作.下面就谈谈关于它的命令及操作: 借助securtCRT,使用linux命令s ...
- 在VMware下安装CentOS系列1:配置VMware
安装环境 VMware Workstation v9.0.0 build-812388 CentOS-6.3-x86_64-minimal.iso minimal,bin-DVD,netinstall ...
- 大数据:Hive常用参数调优
1.limit限制调整 一般情况下,Limit语句还是需要执行整个查询语句,然后再返回部分结果. 有一个配置属性可以开启,避免这种情况---对数据源进行抽样 hive.limit.optimize.e ...
- linux 读取文件信息并且输出
版权为个人所有,欢迎转载如转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5513231.html 以下为读取文件信息做输出操作. 1. ...
- SPOJ SUBLEX - Lexicographical Substring Search 后缀自动机 / 后缀数组
SUBLEX - Lexicographical Substring Search Little Daniel loves to play with strings! He always finds ...
- java的Access restriction错误
问 :import sun.management.ManagementFactory,我在rt包下已经找到sun.management.ManagementFactory,但就是有错,请问怎么回事. ...
- 剑指Offer:对称的二叉树【28】
剑指Offer:对称的二叉树[28] 题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. 题目分析 Java题解 /* publi ...
- PopupWindow 实现 查看所有手机应用弹出框
public class MainActivity extends Activity implements OnItemLongClickListener, OnClickListener { pri ...
- American Heritage usaco
基础题,主要思路是找到根,然后分别递归即可: #include<iostream> #include<cstring> #include<string> #incl ...