D - 秋实大哥与战争

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
Submit Status

男儿何不带吴钩,收取关山五十州。

征战天下是秋实大哥一生的梦想,所以今天他又在练习一个对战游戏。

秋实大哥命令所有士兵从左到右排成了一行来抵挡敌人的攻击。

敌方每一次会攻击一个士兵,这个士兵就会阵亡,整个阵列就会从这个位置断开;同时有的时候已阵亡的士兵会受人赢气息感染而复活。

秋实大哥想知道某一时刻某一个士兵所在的阵列的长度是多少。

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
2 2
0 3
2 2
5
2

解题报告

首先说点题外话。。这题因为数据比较水,因此暴力是可以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>的更多相关文章

  1. UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>

    N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  2. UESTC_秋实大哥打游戏 2015 UESTC Training for Data Structures<Problem H>

    H - 秋实大哥打游戏 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  3. UESTC_秋实大哥去打工 2015 UESTC Training for Data Structures<Problem G>

    G - 秋实大哥去打工 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  4. UESTC_秋实大哥与家 2015 UESTC Training for Data Structures<Problem E>

    E - 秋实大哥与家 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. UESTC_秋实大哥与快餐店 2015 UESTC Training for Data Structures<Problem C>

    C - 秋实大哥与快餐店 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  6. UESTC_秋实大哥与花 2015 UESTC Training for Data Structures<Problem B>

    B - 秋实大哥与花 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  7. UESTC_秋实大哥与小朋友 2015 UESTC Training for Data Structures<Problem A>

    A - 秋实大哥与小朋友 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  8. UESTC_秋实大哥掰手指 2015 UESTC Training for Dynamic Programming<Problem B>

    B - 秋实大哥掰手指 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 2048/1024KB (Java/Others) Submit ...

  9. UESTC_秋实大哥与线段树 2015 UESTC Training for Data Structures<Problem M>

    M - 秋实大哥与线段树 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

随机推荐

  1. visual studio 中GIT的用法

    http://msdn.microsoft.com/zh-cn/library/vstudio/hh850445    Git 使用最新版:Git-1.8.4-preview20130916http: ...

  2. JAX-WS 学习一:基于java的最简单的WebService服务

    JAVA 1.6 之后,自带的JAX-WS API,这使得我们可以很方便的开发一个基于Java的WebService服务. 基于JAVA的WebService 服务 1.创建服务端WebService ...

  3. LeetCode——Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  4. Android系统匿名共享内存Ashmem(Anonymous Shared Memory)在进程间共享的原理分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6666491 在前面一篇文章Android系统匿 ...

  5. Oracle优化技术

    1.基本原理 Oracle的日志:Oracle中为了提高硬盘写的效率,採用内存中数据缓冲区来保存数据,等到一定量或一定时间后才写到磁盘(DBWR). 这个时候假如断电之类的故障发生,数据缓冲区的数据将 ...

  6. [HeadFirst-JSPServlet学习笔记][第三章:实战MVC]

    第三章 实战MVC J2EE如何集成一切 Java2企业版(Java 2 Enterprise Editon,J2EE)是一种超级规范.规定了servlets2.4,JSP2.0,EJB2.1(Ent ...

  7. Linux 编译安装 apache 2.4

    在安装apache之前需要准备一些必要的依赖包 gcc安装: #yum install -y gcc gcc-c++安装: #yum install gcc-c++  apr安装: 下载包:apr-1 ...

  8. NET中级课--文件,流,序列化2

    1.流的类型体系: 基础流    装饰器流    包装器类    帮助类 2.               stream file~     memory~     network~ stream是个 ...

  9. ORACLE/MYSQL/DB2等不同数据库取前几条记录

    选取数据库中记录的操作是最基础最频繁的,但往往实际应用中不会这么简单,会在选取记录的时候加上一些条件,比如取前几条记录,下面就总结了如何在ORACLE/MYSQL/DB2等一些热门数据库中执行取前几条 ...

  10. CSS 设计彻底研究(一)(X)HTML与CSS核心基础

    第1章 (X)HTML与CSS核心基础 这一章重点介绍了4个方面的问题.先介绍了 HTML和XHTML的发展历程以及需要注意的问题,然后介绍了如何将CSS引入HTML,接着讲解了CSS的各种选择器,及 ...