UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>
D - 秋实大哥与战争
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
男儿何不带吴钩,收取关山五十州。
征战天下是秋实大哥一生的梦想,所以今天他又在练习一个对战游戏。
秋实大哥命令所有士兵从左到右排成了一行来抵挡敌人的攻击。
敌方每一次会攻击一个士兵,这个士兵就会阵亡,整个阵列就会从这个位置断开;同时有的时候已阵亡的士兵会受人赢气息感染而复活。
秋实大哥想知道某一时刻某一个士兵所在的阵列的长度是多少。
Input
第一行包含两个整数n,m,表示秋实大哥的士兵数目和接下来发生的事件数目。
接下来m行,每一行是以下三种事件之一:
0 x : 表示x位置的士兵受到攻击阵亡
1 x : 表示x位置的士兵受人赢气息感染复活
2 x : 秋实大哥想知道第x个士兵所在阵列的长度
1≤n,m≤100000,1≤x≤n。
Output
对于每一个2 x事件,输出对应的答案占一行。
Sample input and output
| Sample Input | Sample Output |
|---|---|
5 3 |
5 |
解题报告
首先说点题外话。。这题因为数据比较水,因此暴力是可以AC的。。并且跑的还比线段树快。。
当然这题我用的既不是线段树,也不是暴力。。而是set,采用的插入活人线段。。。。跑了500ms+,之后听某哥们说插死人更快。。。瞬间就纠结了

注意更新时情况较多,需一一分析(很麻烦).....
#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 1e5+;
bool live[maxn]; typedef struct data
{
int l,r;
friend bool operator < (const data&x , const data&y)
{
return x.l < y.l;
}
data(const int& l, const int& r)
{
this->l = l , this->r = r;
}
}; set<data>List; int main(int argc,char *argv[])
{
int n,m;
scanf("%d%d",&n,&m);
memset(live,true,sizeof(live));
List.insert(data(,n));
while(m--)
{
int x,y;
scanf("%d%d",&x,&y);
if (!x)
{
if (!live[y]) //已经死亡还死。。。不操作了
continue;
live[y] = false;
set<data>::iterator it = List.upper_bound(data(y,));
it--;
int getl = it->l;
int getr = it->r;
List.erase(it);
if (getl == y && getr == y)
continue;
if (getl == y)
List.insert(data(y+,getr));
else if(getr == y)
List.insert(data(getl,getr-));
else
{
List.insert(data(getl,y-));
List.insert(data(y+,getr));
}
}
else if(x == )
{
if (live[y]) //已经活了没必要再复活。。
continue;
live[y] = true;
if (List.size() == )
{
List.insert(data(y,y));
continue;
}
set<data>::iterator it = List.upper_bound(data(y,));
it--;
if (List.size() == )
{
if (it == List.end())
{
it++;
if(it->l == y+)
{
int r = it->r;
List.erase(it);
List.insert(data(y,r));
}
else
List.insert(data(y,y));
}
else
{
if (it->r+ == y)
{
int l = it->l;
List.erase(it);
List.insert(data(l,y));
}
else
List.insert(data(y,y));
}
}
if (List.size() >= )
{
set<data>::iterator it2 = it;
it2++;
if ( it2 != List.end() && it->r + == y && y+ == it2->l)
{
int l = it->l , r = it2->r ;
List.erase(it);
List.erase(it2);
List.insert(data(l,r));
}
else if( it!= List.end()&& it->r + == y)
{
int l = it->l;
List.erase(it);
List.insert(data(l,y));
}
else if(it2!= List.end() && it2->l == y+)
{
int r = it2->r;
List.erase(it2);
List.insert(data(y,r));
}
else
List.insert(data(y,y));
}
}
else
{
if (!live[y]) //已经死亡
{
cout << << endl;
continue;
}
set<data>::iterator it = List.upper_bound(data(y,));
it--;
cout << it->r - it->l + << endl;
}
}
return ;
}
UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>的更多相关文章
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>
H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>
G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>
E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>
C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>
B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submi ...
- UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>
A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
- UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>
B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 2048/1024KB (Java/Others) Submit ...
- UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>
M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Sub ...
随机推荐
- Highcharts接收后台传来的json对象值无法显示
在highcharts接收后台传来的json对象网上已经有很多的介绍,在此不多做说明,这里想记录一笔的是在接收的json解析后的value值是String类型的,而highcharts里的data数组 ...
- linux下的守护进程及会话、进程组
守护进程.会话.进程组网上有许多不错的资料.我也是网上搜罗了一堆,加上自己的理解.不敢说原创,只是写在这怕自己忘记罢了.才疏学浅,难免有错误,欢迎大家指正.下面这篇写很不错,大家可以去看看:http: ...
- WPF的数据绑定详细介绍
数据绑定:是应用程序 UI 与业务逻辑之间建立连接的过程. 如果绑定正确设置并且数据提供正确通知,则当数据的值发生更改时,绑定到数据的视觉元素会自动反映更改. 数据绑定可能还意味着如果视觉元素中数据的 ...
- iOS 面试常见问题最全梳理
序言 目前形势,参加到iOS队伍的人是越来越多,甚至已经到供过于求了.今年,找过工作人可能会更深刻地体会到今年的就业形势不容乐观,加之,培训机构一火车地向用人单位输送iOS开发人员,打破了生态圈的动态 ...
- Redis + Jedis + Spring 实例(对象的操作)
目录(?)[+] 不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 一.预期 接上 ...
- java与.net比较学习系列(5) 流程控制语句
java中流程控制语句主要分为以下几类,第一,条件语句,主要包括if语句和switch语句.第二,循环语句,主要包括while循环语句,for循环语句.第三,跳转语句,主要包括三种,break跳出语句 ...
- Android OpenGL库加载过程源码分析
Android系统采用OpenGL绘制3D图形,使用skia来绘制二维图形:OpenGL源码位于: frameworks/native/opengl frameworks/base/opengl 本文 ...
- RelativeLayout经常使用属性介绍
以下介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal ...
- htm跨域锚点定位
a.html <div class="title" id="aaaa">保险服务</div> b.html <div class= ...
- 手机版和PC版识别
1.C#通过User-Agent 处理 //判断 是否来自手机终端 public bool checkMoble() { string userAgent = Request.Headers[&qu ...