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. win10安装Ubuntu双系统

    1.软碟通做启动盘,不要用easyBCD,比较麻烦 2.windows10中取消选择"启用快速启动(推荐)" 3.压缩出空白卷 4.重启时按F12 5.在bios中将boot pr ...

  2. 整理版jq 复习贴子

    1绝对定位(abs)与相对定位(relative) 区别是相对定位参照自己的位置进行移动(当然需要设置top left这些生效)并且原来的位置保留着 偏移后会把 其它的层遮罩住 绝对定位就是的参照位置 ...

  3. D17——C语言基础学PYTHON

    C语言基础学习PYTHON——基础学习D17 20181014内容纲要: 1.jQuery介绍 2.jQuery功能介绍 (1)jQuery的引入方式 (2)选择器 (3)筛选 (4)文本操作 (5) ...

  4. 修改git全局初始化的用户名

    今天我把文件上传到码云的时候出现 翻译的话就是 用户名不能多个值 这样的错误     所以想要解决这个错误的话非常简单 $ git config --global --replace-all user ...

  5. web socket 入门

    WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议,其优雅地解决了以往web服务器不能向web客户端实时推送消息的问题. 在浏览器js环境中,创建一个websock ...

  6. java中连接各种数据的方法

    1.oraclethin驱动连接字符串:jdbc:oracle:thin:用户名/密码@localhost:1521:cake驱动类:oracle.jdbc.driver.OracleDriver 2 ...

  7. Proxy代理模式(结构型模式)

    1.问题 在面向对象系统中,有些对象由于某种原因(比如创建对象的开销很大,或者某些操作需要安全控制,或者需要进程外的访问等),直接访问会给调用者带来麻烦,那么如何在不损失接口透明性的情况下,解决这些麻 ...

  8. logstash笔记(一)——redis&es

    下载地址: https://www.elastic.co/downloads 版本:logstash-2.2.2 两台linux虚拟机,一台windows宿主机 shipper: 192.168.22 ...

  9. Redis 缓存服务配置与使用

    缓存服务器Couchbase另外一种选择Redis documentation http://redis.io/documentation http://redis.cn/documentation. ...

  10. Android get current Locale, not default

    he default Locale is constructed statically at runtime for your application process from the system ...