Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1323

Problem Description
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!
 
Input
The 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.
 
Output
Output 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
 
 
好久木有写博客了,,,,最近在搞线段树,,,o(︶︿︶)o 唉,表示还是很菜啊!
就本题而言,我就没想到线段树,结果自己的代码就超时了,,,悲剧啊!
附上我的拙作:
             求改进!
#include<stdio.h>
__int64 i,j,m,n;
int main()
{
int b[],t;
while(scanf("%d",&n)!=EOF)
{
m=;
for(i=;i<n;i++)
{
scanf("%d",&b[i]);
m+=b[i];
}
for(i=;i<m;i++)
{
if(m==(+i)*i/2.0)
{
t=;
break;
}
else if(m<(+i)*i/2.0)
{
t=i;
break;
} else
continue;
}
if(m==)
printf("yes\n2\n");
else
printf("yes\n%d\n",t);
}
return ;
}

然后下面是AC代码,线段树

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdlib.h>
using namespace std; const int maxn = +; int n,m;
int s[maxn],top; struct node
{
int l,r;
int ls,rs,ms;
}a[maxn<<]; int max(int a,int b)
{
return a>b?a:b;
} void init(int l,int r,int i)
{
a[i].l = l;
a[i].r = r;
a[i].ls = a[i].rs = a[i].ms = r-l+;
if(l!=r)
{
int mid = (l+r)>>;
init(l,mid,i*);
init(mid+,r,*i+);
}
} void insert(int i,int t,int x)
{
if(a[i].l == a[i].r)
{
if(x==)
a[i].ls = a[i].rs = a[i].ms = ;
else
a[i].ls = a[i].rs = a[i].ms = ;
return ;
}
int mid = (a[i].l+a[i].r)>>;
if(t<=mid)
insert(*i,t,x);
else
insert(*i+,t,x);
a[i].ls = a[*i].ls;
a[i].rs = a[*i+].rs;
a[i].ms = max(max(a[*i].ms,a[*i+].ms),a[*i].rs+a[*i+].ls);
if(a[*i].ls == a[*i].r-a[*i].l+)
a[i].ls += a[*i+].ls;
if(a[*i+].rs == a[*i+].r-a[*i+].l+)
a[i].rs += a[*i].rs;
} int query(int i,int t)
{
if(a[i].l == a[i].r || a[i].ms == || a[i].ms == a[i].r-a[i].l+)
return a[i].ms;
int mid = (a[i].l+a[i].r)>>;
if(t<=mid)
{
if(t>=a[*i].r-a[*i].rs+)
return query(*i,t)+query(*i+,mid+);
else
return query(*i,t);
}
else
{
if(t<=a[*i+].l+a[*i+].ls-)
return query(*i+,t)+query(*i,mid);
else
return query(*i+,t);
}
} int main()
{
int i,j,x;
char ch[];
while(~scanf("%d%d",&n,&m))
{
top = ;
init(,n,);
while(m--)
{
scanf("%s",ch);
if(ch[] == 'D')
{
scanf("%d",&x);
s[top++] = x;
insert(,x,);
}
else if(ch[] == 'Q')
{
scanf("%d",&x);
printf("%d\n",query(,x));
}
else
{
if(x>)
{
x = s[--top];
insert(,x,);
}
}
}
}
return ;
}

Tunnel Warfare(hdu1540 线段树)的更多相关文章

  1. HDU--1540 Tunnel Warfare(线段树区间更新)

    题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...

  2. hdu1540 Tunnel Warfare【线段树】

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

  3. HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  5. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  6. poj 2892 Tunnel Warfare(线段树)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7499   Accepted: 3096 D ...

  7. hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】

    Tunnel Warfare                                                             Time Limit: 4000/2000 MS ...

  8. HDU - 1540 Tunnel Warfare(线段树区间合并)

    https://cn.vjudge.net/problem/HDU-1540 题意 D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 分析 线段树的区间内,我 ...

  9. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

  10. Tunnel Warfare(线段树取连续区间)

    emmmmmmmm我菜爆了 思路来自:https://blog.csdn.net/chudongfang2015/article/details/52133243 线段树最难的应该就是要维护什么东西 ...

随机推荐

  1. EF学习笔记-2 EF之支持复杂类型的实现

    使用过.NET的小伙伴们知道,在我们的实体模型中,除了一些简单模型外,还有一些复杂类型,如几个简单的类型组合而成的类型:而EF除了在实现基本的增删改查之外,也支持复杂类型的实现. 那么如何手动构造复杂 ...

  2. Python2 指定文件编码格式需要注意的地方

    python2 中默认的编码格式是unicode, 开发人员经常需要根据需要,将python文件的编码格式设置为utf-8,我们可以在python文件的第一行进行设置,加入如下代码: # encodi ...

  3. ubuntu下nodejs源码安装

    1.从github选择下载自己要安装的nodejs版本,https://github.com/nodejs/node/releases,我下载的版本是node-9.11.2.tar.gz 2.解压no ...

  4. Testing - 软件测试知识梳理 - 软件可靠性测试

    软件可靠性的基本概念 错误,缺陷,故障和失效 错误:指的是软件在生命周期中各个阶段的状态和行为与人们的期待不一致的偏差,不单单是软件系统本身,中间产品的偏差也算是软件错误 缺陷:指的是软件中一切不好的 ...

  5. iOS-IAP内购的那些事(iOS内购漏单的问题)

    前言 说起内购,其实挺令开发者厌烦的,原因呢,先不说漏单的问题,首先苹果要扣除30%的销售额哦,可恨不?(我觉得可恨),有些想办法先隐藏掉第三方支付(支付宝.微信等),等项目上线了,再跳过内购使用第三 ...

  6. redis哨兵集群环境搭建

    一.哨兵的介绍 哨兵(sentinal)是redis集群架构中非常重要的一个组件,主要功能如下: 集群监控,负责监控redis master和slave进程是否正常工作 消息通知,如果某个redis实 ...

  7. h5 端图片上传-模拟多张上传

    1.由于后端的限制,上传图片到服务器只能的一张一张传2.显示图片预览是本地的图片3.根据服务器返回的结果拿到相应的路径保存到提交评论的接口中4.删除的时候,需要删除对应的路径,不要把删除的提交到评论的 ...

  8. 全网最详细的Sublime Text 3的设置字体及字体大小(图文详解)

    不多说,直接上干货! 前期博客 全网最详细的Windows里下载与安装Sublime Text *(图文详解) 全网最详细的Sublime Text 3的激活(图文详解) 你也许是如下的版本:   点 ...

  9. Liferay7.0与cas单点登录配置

    1.简介     Liferay7.0支持多种登录方式,包括:常规的.opensso.cas.ntlm.ldap.openid.Facebook.Google等. 其中, (1) 常规:则是默认Lif ...

  10. 用Filter作用户授权的例子

    public class LoginFilter implements Filter { private String permitUrls[] = null; private String goto ...