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 ...
随机推荐
- cf486C Palindrome Transformation
C. Palindrome Transformation time limit per test 1 second memory limit per test 256 megabytes input ...
- spring bean初始化和销毁
spring bean的创建与消亡由spring容器进行管理,除了使用<bean><property/></bean>进行简单的属性配置之外,spring支持更人性 ...
- Linux权限机制
权限是操作系统用来限制用户.组.进程对操作系统资源(文件.设备等)的访问的机制 权限分为:读.写.执行,一般表示为 r.w.x http://itercast.com/lecture/22 每个文件或 ...
- 安全:加固你的ssh 登录
SSH 是我们控制虚拟主机的一种途径,这个途径可以让我们拥有完全的控制权,如果对于这个控制权没有进行很好的安全处理,那么将会造成很大的安全问题. 我们可以在系统的日志文件 (例如:/var/ ...
- Spring MVC基础
1.Web MVC基础 MVC的本质是表现层模式,我们以视图模型为中心,将视图和控制器分离出来.就如同分层模式一样,我们以业务逻辑为中心,把表现层和数据访问层代码分离出来是一样的方法.框架只能在技术层 ...
- hdu 5288 OO’s Sequence(计数)
Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...
- KVO奥秘
序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣的回调机制,在某个对象注册监听者后,在被监听的对象发生改变时,对象会发送一个通知给 ...
- android 推断Apk是否签名和 签名是否一致
推断Apk是否签名 用命令:jarsigner -verify -verbose -certs <apk文件> 假设有Android Debug字樣就是debug 假设已经签名: [证书的 ...
- 网页http请求的整个过程
这几天看一个讲解一个网页从我们输入地址到显示在我们面前的一个讲解,是我对http又有了一个完整的了解,现在做一下整个流程的记录,虽然不是很详细,但是整个过程是完整的.如果不对,请指正! 打开浏览器,地 ...
- 调试Linq的时候获得相对应的SQL
(query as System.Data.Objects.ObjectQuery).ToTraceString()