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. 【转】ubuntu设置PATH----不错

    原文网址:http://no001.blog.51cto.com/1142339/554927 试了好多遍,多无效.. 最后在/etc/enviroment下设置才有效. 不过让有一些未解问题 我使用 ...

  2. mybatis源代码分析:深入了解mybatis延迟加载机制

    下文从mybatis(3.2.7)延迟加载样例讲起,逐步深入其实现机制. 下面的例子是Student类关联一个Teacher对象,在访问Student对象时,不立即加载其关联的Teacher对象,而是 ...

  3. SQL查询最近三个月的数据(查询最近几天,几年等等)

    定义和用法      :: 天,这样就可以找到付款日期. 我们使用如下 ,OrderDate) AS OrderPayDate FROM Orders 结果: OrderId    OrderPayD ...

  4. MongoDB GUI管理工具Mongo VUE

    一.前言 现在越来越多的公司开始采用非关系数据库了,并且很多公司的面试都要求面试 者有MongoDB的使用经验,至于非关系数据库与关系型数据库之间的区别大家可以自行百度.但是作为程序员的我们,既然大部 ...

  5. Lowest Common Ancestor of a Binary Search Tree 解答

    Question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes ...

  6. apacheOfbiz

    一.ofbiz 用自身数据库安装 1. 由 binary 安装: 由 binary 安装非常简单, 以下是安装方法: 下载ofbiz-2.0-beta1-complete.tar.gz, 注意不是of ...

  7. UGUI 过渡动画插件,模仿NGUI的Tween (转载)

    最近在相亲,后来好朋友跟我说他写了一个好插件,于是我就把女朋友甩了,看看他的插件,可以在UGUI制作简单过渡动画. 我看了下是模仿NGUI的Tween, 我在筱程的基础上稍微改到人性化, 简单支持的让 ...

  8. qt画刷和画笔

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #画刷和画笔:QBrush 定义了 QPainter 的填充模式,具 ...

  9. 禁用UITableViewCell 重用机制

    有时候不想让Cell重用,怎么办勒.接下来介绍两种方法 方法一 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAt ...

  10. Subsequence(两个单调队列)

    Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...