Tunnel Warfare(hdu1540 线段树)
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412 Accepted Submission(s): 1323
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.
#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 线段树)的更多相关文章
- HDU--1540 Tunnel Warfare(线段树区间更新)
题目链接:1540 Tunnel Warfare 以为单组输入 这个题多组输入 结构体记录每个区间左边和右边的连续区间 ms记录最大 在查询操作时: 1.这个点即将查询到右区间 看这个点 x 是否存在 ...
- hdu1540 Tunnel Warfare【线段树】
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- hdu 1540 Tunnel Warfare (区间线段树(模板))
http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...
- poj 2892 Tunnel Warfare(线段树)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7499 Accepted: 3096 D ...
- hdu 1540/POJ 2892 Tunnel Warfare 【线段树区间合并】
Tunnel Warfare Time Limit: 4000/2000 MS ...
- 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 Lim ...
- Tunnel Warfare(线段树取连续区间)
emmmmmmmm我菜爆了 思路来自:https://blog.csdn.net/chudongfang2015/article/details/52133243 线段树最难的应该就是要维护什么东西 ...
随机推荐
- 记录一次BUG修复-Entity Framwork SaveChanges()失效
目录 一. 前言 二.问题背景 三.问题描述 四.问题解决步骤 六.总结 一. 前言 这是笔者在参与一个小型项目开发时所遇到的一个BUG,因为项目经验不足对Entity Framwork框架认识不足导 ...
- Android 使用 NYTimes Stores 缓存 network request
NYTimes Stores 是一个缓存库,在 2017年的 AndroidMakers 大会上被介绍过. https://github.com/NYTimes/Store 实现一个 Disk Cac ...
- Flask系列06--(中间件)Flask的特殊装饰器 before_request,after_request, errorhandler
一.使用 Flask中的特殊装饰器(中间件)方法常用的有三个 @app.before_request # 在请求进入视图函数之前 @app.after_request # 在请求结束视图函数之后 响应 ...
- Android------------------系统服务调用的学习
一.ServiceManager的方法: 此方法getService,用于根据名称获取当前的IBinder的代理(并没有直接获取服务), 服务提供的功能是依靠IBinder间接调用的(返回值IBind ...
- ZJOI Round2游记
虽然一试很惨但是二试还是要来玩一下的 Day 0 到余姚了,然后到余姚边上的宾馆住来下来 顺便一问老师们对边上是不是有什么误解-- 吃完晚饭就回宾馆颓了 话说半夜真刺激--住隔壁那一位手突然骨折了,本 ...
- FunDA(17)- 示范:异常处理与事后处理 - Exceptions handling and Finalizers
作为一个能安全运行的工具库,为了保证占用资源的安全性,对异常处理(exception handling)和事后处理(final clean-up)的支持是不可或缺的.FunDA的数据流FDAPipeL ...
- JVM中强引用,弱引用,软引用和幽灵引用的代码
上代码: public class ReferenceTest { public static void main(String[] args) { //test1();//软引用 //test2() ...
- Java8简明指南
Java8简明指南 转载自并发编程网 – ifeve.com本文链接地址: Java8简明指南 欢迎来到Java8简明指南.本教程将一步一步指导你通过所有新语言特性.由短而简单的代码示例,带你了解如何 ...
- 字符、字符串和文本的处理之Char类型
.Net Framework中处理字符和字符串的主要有以下这么几个类: (1).System.Char类 一基础字符串处理类 (2).System.String类 一处理不可变的字符串(一经创建,字符 ...
- 编程珠玑第一章的算法,Java实现,通俗易懂
该算法也就是所谓的位图算法,用一个int表示32位,也就是实际值为1~32的数. 按照书里说的, 该算法只适合内存有限,而磁盘和时间不限,且数字在1~MAX之间不重复的排序. package demo ...