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. Speech Synthesis

    <Window x:Class="Synthesizer.MainWindow" xmlns="http://schemas.microsoft.com/winfx ...

  2. Linux系统文件压缩与备份(5)

    在 Linux 系统选有相当多的压缩命令可以使用,这些压缩指令可以让我们更方便的从网上下载大型文件,本章第一节内容我们就来谈谈这个 Linux 系统下常用的几种压缩格式吧. 谈完了压缩后,我们接着来说 ...

  3. " XSS易容术---bypass之编码混淆篇+辅助脚本编写"

    一.前言本文原创作者:vk,本文属i春秋原创奖励计划,未经许可禁止转载!很多人对于XSS的了解不深.一提起来就是:“哦,弹窗的”.”哦,偷cookie的.”骚年,你根本不知道什么是力量.虽然我也不知道 ...

  4. C++类和对象(一)&&实现offsetof宏&&this指针

    一.目录 1.对象的相关知识 2.类的定义 3.类的实例化 4.类对象模型 5.模拟实现offsetof宏 6.this指针 二.正文 1.对象的相关知识 C语言是面向过程的,关注的是过程,分析求解问 ...

  5. 10-03 Java 包的概述和讲解

    带包的编译和运行 A:手动式 a:编写一个带包的java文件. b:通过javac命令编译该java文件. c:手动创建包名. d:把b步骤的class文件放到c步骤的最底层包 e:回到和包根目录在同 ...

  6. git提交的问题

    1. Pull is not possible because you have unmerged files.症状:pull的时候$ git pull Pull is not possible be ...

  7. linux 备忘记录

    杂项记录 Ubuntu 通过/etc/network/interfaces修改IP,重启网络服务貌似也不会生效.可以重启电脑使其生效,或执行: ip addr flush dev ens33 & ...

  8. php完美匹配邮箱、链接地址和电话号码

    php完美匹配邮箱.链接地址和电话号码 写了好一会有问题,朋友这边很好功能,借用了.嘎嘎 //31日 更新: 匹配手机以及电话号码 重新修改,可支持18开头的手机号,并修改bug,可匹配出字符串中所有 ...

  9. iOS Round Double value to 2 decimal digits and round 3rd decimal digit up

    I have I double value like this one 17.125. It should be rounded up to 17.13 If I use the simple met ...

  10. vue子组件传参给父组件

    关于父组件传参给子组件,可以看我另一篇文章 教程开始: 我们要实现的效果是:在子组件的Input框输入,父组件中实时更新显示.(也就是把子组件中的数据传给父组件) 一.子组件代码 template部分 ...