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. 在ASP.NET MVC部署AngularJs

    创建一个ASP.NET MVC项目. 打开NuGet管理,安装angularjs: 在App_Start目录下,Bundle刚刚安装的angularjs库: 在Global.asax.cs的Appli ...

  2. CentOS 7 - 配置服务实现开机自启动

    新建系统服务描述文件 cd /etc/systemd/system sudo vim myapp.service 添加以下配置: [Unit] # 这里添加你的服务描述 Description=mya ...

  3. linux 的计划任务 定时任务

    linux的计划任务,也叫做定时任务 https://www.cnblogs.com/mingforyou/p/3930636.html 名字是crond 查看linux本机的定时任务 crontab ...

  4. underscore.js源码研究(2)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  5. Windows上安装tensorflow 详细教程

    原博客转载自:https://www.cnblogs.com/lvsling/p/8672404.html 一, 前言:本次安装tensorflow是基于Python的,安装Python的过程不做说明 ...

  6. 安装ORACLE时 各Linux版本下载地址

    oracle linux :https://edelivery.oracle.com/osdc/faces/SearchSoftware 需要注册oracle账号 redhat官方下载 https:/ ...

  7. 网络Socket编程UDP协议例子

    服务端代码 public class UDPChatServer { //通讯端口 private Integer port=8000; //数据报文的通讯通道对象 private DatagramC ...

  8. c3p0配置文件

    配置文件 名称必须为c3p0-config.xml,否则找不到: 标签名称 <c3p0-config> <default-config > 具体配置内容 </defaul ...

  9. Android入门学习总结

    1.Manifest.xml是程序运行时读取的文件,是核心的配置文件:也是从中读取Activity 2.主要的代码文件存放在MainActivity.java,里面固定会有onCreate函数会通过s ...

  10. Docker概念学习系列之Docker核心概念之镜像Image

    不多说,直接上干货! 说明:   Docker 运行容器之前需要本地存在对应的镜像,如果镜像不存在,Docker 会尝试先从默认镜像仓库下载(默认使用Docker Hub公共注册服务器中的仓库),用户 ...