UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures

时间:2023-03-09 03:54:23
UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

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

解题报告UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

首先说点题外话。。这题因为数据比较水,因此暴力是可以AC的。。并且跑的还比线段树快。。

当然这题我用的既不是线段树,也不是暴力。。而是set,采用的插入活人线段。。。。跑了500ms+,之后听某哥们说插死人更快。。。瞬间就纠结了

UESTC_秋实大哥与战争 2015 UESTC Training for Data Structures<Problem D>

注意更新时情况较多,需一一分析(很麻烦).....

 #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 ;
}