#include<iostream>
#include<algorithm>
#include<stack>
using namespace std;
struct node{
int l, r, lcov, rcov, mcov;
bool fcov(){ return r - l + 1 == mcov; }
};
node nodes[200000];
stack<int> sta;
void init(int i, int l, int r)
{
nodes[i].l = l;
nodes[i].r = r;
if (l == r)
return;
int mid = (l + r) / 2;
init(i * 2, l, mid);
init(i * 2 + 1, mid + 1, r);
}
void update(int i, int pos, int type)//1:destory,0:rebuild
{
if (nodes[i].l == nodes[i].r)
{
if (type == 1)
nodes[i].lcov = nodes[i].rcov = nodes[i].mcov = 0;
else
nodes[i].lcov = nodes[i].rcov = nodes[i].mcov = 1;
}
else{
int mid = (nodes[i].l + nodes[i].r) / 2;
if (pos <= mid)
update(i * 2, pos, type);
else
update(i * 2 + 1, pos, type);
nodes[i].lcov = nodes[i * 2].lcov + (nodes[i * 2].fcov() ? nodes[i * 2 + 1].lcov : 0);
nodes[i].rcov = nodes[i * 2 + 1].rcov + (nodes[i * 2 + 1].fcov() ? nodes[i * 2].rcov : 0);
nodes[i].mcov = max(nodes[i * 2].rcov + nodes[i * 2 + 1].lcov, max(nodes[i * 2].mcov, nodes[i * 2 + 1].mcov));
}
}
int query(int i, int pos)
{
if (nodes[i].l == nodes[i].r || nodes[i].fcov() || nodes[i].mcov == 0)
return nodes[i].mcov;
else
{
int mid = (nodes[i].l + nodes[i].r) / 2;
if (pos <= mid)
{
if (pos >= mid - nodes[i * 2].rcov + 1)
return nodes[i * 2].rcov + query(i * 2 + 1, mid + 1);
else
return query(i * 2, pos);
}
else
{
if (pos <= mid+ nodes[i * 2 + 1].lcov)
return nodes[i * 2 + 1].lcov + query(i * 2, mid);
else
return query(i * 2 + 1, pos);
}
}
}
int main()
{
int n, m;
while (cin >> n >> m)
{
while (!sta.empty())
sta.pop();
init(1, 1, n);
for (int i = 1; i <= n; i++)
update(1, i, 0);
for (int i = 0; i < m; i++)
{
char c;
int a;
cin >> c;
if (c == 'D')
{
cin >> a;
sta.push(a);
update(1, a, 1);
}
else if (c == 'Q')
{
cin >> a;
cout << query(1, a) << endl;
}
else
{
if (!sta.empty())
{
a = sta.top();
sta.pop();
update(1, a, 0);
}
}
}
}
return 0;
}
hdoj1540_Tunnel Warfare(线段树)
最新推荐文章于 2020-12-02 14:52:34 发布