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 ...
随机推荐
- Koa2+MySQL+VUE+ElementIUI搭建简单的后台管理小系统
如题,前端入坑许久,还是写个小东西出来吧 想要搭建自己的一个后台管理,实现简单的增删改查,看起来很简单 其实是真的简单,没有想的那么难,我也就写了一个月吧, 当然是假的,其实也就每天一两个小时,花了大 ...
- XP,32/64位Win7,32/64位Win8,32/64位Win10系统 【春节版】
本系统是10月5日最新完整版本的Windows10 安装版镜像,win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为win10 Edge浏览器中国默认主页和搜索引擎 ...
- System.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' from assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
1.提示错误信息: zipSystem.TypeLoadException: Could not load type 'System.IO.Compression.CompressionLevel' ...
- hdu 4667 Building Fence < 计算几何模板>
//大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <string ...
- 2017-2018-1 20179209《Linux内核原理与分析》第三周作业
一.函数调用堆栈 存储程序.函数调用堆栈(高级语言起点)和中断机制是计算机工作的三大法宝.其中函数调用堆栈是本次学习的重点.先介绍一些基本的知识点: 1.ebp 在C语言中用作记录当前函数调用的基址: ...
- sort()函数到底是怎样进行数字排序的
很多人会用sort(),并不见得知道它具体是怎样给数字排序的.其实不知道也行,会用就可以,感兴趣的可以来看看. var numberArray = [2,4,1,3]; numberArray.sor ...
- 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法
开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...
- long_query_time 设置不生效问题
由于原来的慢查询日志太大了,有1G多,并且其中包含上一次查询优化前的慢sql,所以想收集最近两天的慢查询语句,故 mysql> show global variables like 'slow% ...
- Java for LeetCode 138 Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point t ...
- Java for LeetCode 128 Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...