HDU 1540 Tunnel Warfare

该题与http://poj.org/problem?id=3667是同一类型的题目,这里就不多说了,因为HOTEL注释的比较详细。

#include<cstdio>
#include<iostream>
#include<vector>
using namespace std;
class Node
{
public:
int l,r;
int l_value,r_value;
int cover;
};
Node tree[250024];
class Tree
{
public:
void Maketree( int l,int r, int cnt );
void Updata( int l,int r, int cnt,int num );
void fun( int cnt );
int Query( int cnt, int num );
};
void Tree::Maketree( int l, int r, int cnt )
{
tree[cnt].l = l;
tree[cnt].r = r;
tree[cnt].l_value = tree[cnt].r_value = r - l + 1;
tree[cnt].cover = 0;
if( l == r ) return ;
else
{
int mid = ( l + r )>>1;
Maketree( l ,mid , cnt*2 );
Maketree( mid + 1 , r ,cnt*2+1 );
}
}
void Tree::fun( int cnt )
{
if( tree[cnt].cover==0 )
tree[cnt].l_value=tree[cnt].r_value=tree[cnt].r -tree[cnt].l + 1;
else tree[cnt].l_value = tree[cnt].r_value = 0;
}
void Tree::Updata( int l, int r, int cnt , int num )
{
if( r<tree[cnt].l||l>tree[cnt].r )
return ;
if( l<=tree[cnt].l&&r>=tree[cnt].r )
{
tree[cnt].cover = num;
fun( cnt );
return ;
}
else
{
if( tree[cnt].cover != -1 )
{
tree[cnt*2].cover = tree[cnt*2+1].cover = tree[cnt].cover;
fun( cnt*2 );
fun( cnt*2 +1 );
tree[cnt].cover = -1;
}
Updata( l , r, cnt*2 , num );
Updata( l , r , cnt*2+1 , num );
if( tree[cnt*2].cover==tree[cnt*2+1].cover )
tree[cnt].cover = tree[cnt*2].cover;
else tree[cnt].cover = -1;
tree[cnt].l_value = tree[cnt*2].l_value + (tree[cnt*2].cover==0?tree[cnt*2+1].l_value:0);
tree[cnt].r_value = tree[cnt*2+1].r_value + ( tree[cnt*2+1].cover == 0? tree[cnt*2].r_value:0 );
}
}
int Tree::Query( int cnt , int num )
{
if( num >= tree[cnt].l&&tree[cnt].l+tree[cnt].l_value-1>=num )
return tree[cnt].l_value;
if( num<=tree[cnt].r&&tree[cnt].r - tree[cnt].r_value+1 <= num )
return tree[cnt].r_value;
else
{
if( tree[cnt*2].r - tree[cnt*2].r_value+1 <= num&&tree[cnt*2+1].l+tree[cnt*2+1].l_value-1>=num )
return tree[cnt*2+1].l_value + tree[cnt*2].r_value;
else
{
if( tree[cnt].l<tree[cnt].r )
{
int mid = ( tree[cnt].l+tree[cnt].r )>>1;
if( num<=mid ) return Query( cnt*2 , num );
else return Query( cnt*2+1 , num );
}
}
}
return 0;
}
int main( )
{
int n,m,num;
char c[2];

while( scanf( "%d%d",&n,&m )==2 )
{
vector<int>S;
Tree e;
e.Maketree( 1 ,n ,1 );
for( int i=0; i< m; i++ )
{
scanf( "%s",c );
if( c[0]=='D' )
{
scanf( "%d",&num );
S.push_back( num );
e.Updata( num , num ,1 ,1 );
}
else
{
if( c[0]=='Q' )
{
scanf( "%d",&num );
printf( "%d\n",e.Query(1 ,num ) );
}
else
{
num = S.back();
S.pop_back();
e.Updata( num , num ,1, 0 );
}
}
}
}
return 0;
}

 

 

你可能感兴趣的:(HDU)