hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3168 Accepted Submission(s): 1216
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!
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.
{
int l,r; //左右
int ll,ml,rl; //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
}node[N<<2];
{
if(node[p].l==node[p].r||node[p].ml==0||node[p].ml==node[p].r-node[p].l+1)
{
return node[p].ml;
}
int mid = (node[p].l+node[p].r)>>1;
if(id<=mid)
{
if(id>=node[lson].r-node[lson].rl+1) return query(id,lson)+query(mid+1,rson); //1
else query(id,lson);
}
else
{
if(id<=node[rson].l+node[rson].ll-1) return query(mid,lson)+query(id,rson); //2
else query(id,rson);
}
}

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stack>
#define N 50005
#define lson p<<1
#define rson p<<1|1 using namespace std; struct Nod
{
int l,r; //左右
int ll,ml,rl; //左边开始连续的最大长度、中间的最大长度、右边开始最大的连续长度(对没摧毁的村庄来说)
}node[N<<]; void building(int l,int r,int p) //建树
{
node[p].l = l;
node[p].r = r;
node[p].ll=node[p].ml=node[p].rl=r-l+;
if(l==r) return;
int mid = (l+r)>>;
building(l,mid,lson);
building(mid+,r,rson);
} void update(int id,int p,int ok)
{
if(node[p].l==node[p].r)
{
if(ok==) node[p].ll=node[p].ml=node[p].rl=;
else node[p].ll=node[p].ml=node[p].rl=;
return ;
}
int mid = (node[p].l+node[p].r)>>;
if(id<=mid) update(id,lson,ok);
else update(id,rson,ok); node[p].ml = max(node[lson].ml,node[rson].ml);
node[p].ml = max(node[p].ml,node[lson].rl+node[rson].ll); //最大中间值是 左右孩子的中间值 与 左边右最大+右边左最大 三者的最大值 node[p].ll = node[lson].ll; //左孩子的左最大
node[p].rl = node[rson].rl; //右孩子的右最大 if(node[lson].ll==node[lson].r-node[lson].l+) node[p].ll+=node[rson].ll;
if(node[rson].rl==node[rson].r-node[rson].l+) node[p].rl+=node[lson].rl;
} int query(int id,int p)
{
if(node[p].l==node[p].r||node[p].ml==||node[p].ml==node[p].r-node[p].l+)
{
return node[p].ml;
}
int mid = (node[p].l+node[p].r)>>;
if(id<=mid)
{
if(id>=node[lson].r-node[lson].rl+) return query(id,lson)+query(mid+,rson); //
else query(id,lson);
}
else
{
if(id<=node[rson].l+node[rson].ll-) return query(mid,lson)+query(id,rson); //
else query(id,rson);
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
char op[];
int id;
stack<int> stk;
building(,n,);
while(m--)
{
scanf("%s",op);
if(op[]=='D')
{
scanf("%d",&id);
stk.push(id);
update(id,,);
}
else if(op[]=='Q')
{
scanf("%d",&id);
printf("%d\n",query(id,));
}
else if(op[]=='R')
{
id = stk.top();
stk.pop();
update(id,,);
}
}
while(!stk.empty()){stk.pop();}
}
return ;
}
hdu 1540 Tunnel Warfare (区间线段树(模板))的更多相关文章
- HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Lim ...
- HDU - 1540 Tunnel Warfare(线段树区间合并)
https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...
- hdu 1540 Tunnel Warfare (线段树 区间合并)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1540 Tunnel Warfare(线段树)
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. ...
- hdu 1540 Tunnel Warfare (线段树,维护当前最大连续区间)
Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively i ...
- HDU 1540 Tunnel Warfare(线段树+区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...
- HDU 1540 Tunnel Warfare(最长连续区间 基础)
校赛,还有什么途径可以申请加入ACM校队? Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/ ...
- HDU--1540 Tunnel Warfare(线段树区间更新)
题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- 多线程NSOperation
NSOperation(经常使用): 1.为什么会有NSOperation?弥补gcd的一些问题:1)下载为例子:如果gcd放到队列中的block操作面对网络有问题,block之外无法取消bloc ...
- ios webview 图片自适应屏幕宽度
//这个知识点主要是自己最近在尝试写后台接口 在移动端展示的时候需要用到这个知识点,在webViewDidFinishLoad方法里面执行一段js代码 拿到各个图片 判断其宽度是否大于当前手机屏幕尺寸 ...
- 深入理解计算机系统第二版习题解答CSAPP 2.17
假设w=4,我们能给每个可能的十六进制数字赋予一个数值,假设用一个无符号或者补码表示.完成下表: x 无符号(B2U(x)) 补码(B2T(x)) 十六进制 二进制 0xE 1110 14 -2 0x ...
- (转)战斗bug技巧全攻略
原文地址:http://www.cnblogs.com/manuosex/p/3736077.html 程序员不是有一幅这样的对联吗 上联:一个项目两部电脑三餐盒饭只为四千工资搞得五脏俱损六神无主仍然 ...
- RabbitMQ 原文译1.2--"Hello Word"
本系列文章均来自官网原文,属于个人翻译,如有雷同,权当个人归档,忽喷. .NET/C# RabbitMQ 客户端下载地址:https://github.com/rabbitmq/rabbitmq-do ...
- ASP缓存类收集
木鸟写的 '********************************************** ' vbs Cache类 ' ' 属性valid,是否可用,取值前判断 ' 属性name,ca ...
- apache ambari web页面无法访问解决办法
ambari-server启动成功,但是页面无法访问 作者:Bo liang链接:http://www.zhihu.com/question/34405898/answer/115001510来源:知 ...
- Oracle查询慢, 特别是更新慢问题
近期, 客户发现查询比较慢, 特别是更新更慢. 原来是oracle listerner log太大导致. (C:\app\Administrator\diag\tnslsnr\ServerName\ ...
- JEditorPane中html文档中文乱码解决
Culturally dependent information in some documents is handled through a mechanism called character e ...
- ###《Machine Learning in Action》 - KNN
初学Python:理解机器学习. 算法是需要实现的,纸上得来终觉浅. // @author: gr // @date: 2015-01-16 // @email: forgerui@gmail.com ...