给定n个点以及m次操作,操作分三种,D,x是第x个村庄被破坏,Q,x是查询与x左右相连(就是中间没有被破坏的点)共有多少个点,包括自身,R是修复最后一次被破坏的点。
这个是加在线段树专题里面的题目。然后感觉其实就是处理端点的问题,于是试了试set,可以过...
set容器里面存的是所有被破坏的点,那么对于查询的点来说,如果在容器里,那么肯定是0..如果不在容器里,那么答案肯定是比他大且离他最近的被破坏点-比他小且离他最近的被破坏点-1.
so,对于第一种操作,直接加入到set中,第二种操作我们可以用set提供的lower_bound函数,查询比他大或相等且离他最近的一个点,这个函数返回是一个类似指针的迭代器,然后迭代器--就可以知道它前一个点是什么了...对于R来说就是删除点...
主要是最近codeforce的比赛上看到两道类似的题目...
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<iterator>
using namespace std;
#define N 2000000
set<int> ms;
set<int>::iterator it;
stack<int>s;
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)!=EOF)
{
while(!s.empty())
s.pop();
ms.clear();
ms.insert(0);
ms.insert(n+1);
char x;
int y;
for(int i=0; i<m; i++)
{
scanf(" %c",&x);
if(x=='D')
{
scanf("%d",&y);
s.push(y);
ms.insert(y);
}
else if(x=='Q')
{
scanf("%d",&y);
it=ms.lower_bound(y);
int t1=*it;
if(t1==y)
{
printf("0\n");
continue;
}
it--;
int t2=*it;
printf("%d\n",t1-t2-1);
}
else
{
y=s.top();
s.pop();
ms.erase(y);
}
}
}
return 0;
}