HDU 1540 Tunnel Warfare (线段树单点更新+思维)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12857    Accepted Submission(s): 5083


 

Problem Description

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

 

Input

The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.

 

Output

Output the answer to each of the Army commanders’ request in order on a separate line.

 

Sample Input

 

7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4

 

Sample Output

 

1 0 2 4

 

Source

POJ Monthly

 

Recommend

LL   |   We have carefully selected several similar problems for you:  1542 1394 1166 1255 1754 

 

#include<iostream>
#include<fstream>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 50005

#define INF 1000000

#define debug puts("YES");
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;

int n,m;
///线段树单点修改
/*
题目大意:给定n个村庄排成一排,
序列形式,然后提供三种操作,一种摧毁村庄操作,一种重建,
一种是查询操作,即查询还有多少连通在一起。

对于这种序列问题,模拟找感觉,
一开始模拟以为是树状数组,结果超时了(。。。)
树状数组的查询花时间主要体现在如何找到最近的那个被摧毁的村庄,
可以用二分优化,但代码结构反而烦了。

再次思考,线段树处理。
单点更新即可,处理两个指标,最大最小值,但最大最小值初始化分别为0和n+1.
这样查询的时候,可以查询左边的最大值,查询右边的最小值。,即可得到区间长度。

比如:第一个样例,
初始化:
0,0,0,0,0,0,0;
8,8,8,8,8,8,8;
然后摧毁3,修改,
0,0,3,0,0,0,0;
8,8,3,8,8,8,8;
摧毁5,
0,0,3,0,5,0,0
8,8,3,8,5,8,8.
不难发现,每次查询,比如查询4,
那么左边的最大值是3,右边的最小值是5.可计算出区间长度。     


*/

int maxv[maxn<<2],minv[maxn<<2];
void pushup(lrt)
{
    maxv[rt]=max(maxv[rt<<1],maxv[rt<<1|1]);
    minv[rt]=min(minv[rt<<1],minv[rt<<1|1]);
}

void Build(int l,int r,int rt)
{
    if(l==r)
    {
        maxv[rt]=0;
        minv[rt]=n+1;
        return ;
    }
    int mid=l+r>>1;
    Build(l,mid,rt<<1);
    Build(mid+1,r,rt<<1|1);
    pushup(l,r,rt);
}

void update(lrt,int pos,int v)///单点修改
{
    if(l==r)
    {
        maxv[rt]=minv[rt]=v;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) update(lson,pos,v);
    if(mid<pos) update(rson,pos,v);
    pushup(l,r,rt);
}

void update(lrt,int pos)///单点修改
{
    if(l==r)
    {
        maxv[rt]=0,minv[rt]=n+1;
        return ;
    }
    int mid=l+r>>1;
    if(pos<=mid) update(lson,pos);
    if(mid<pos) update(rson,pos);
    pushup(l,r,rt);
}

int query_min(lrt,int L,int R)
{
    if(L<=l&&r<=R)  return minv[rt];
    int mid=l+r>>1,ans=n+1;
    if(L<=mid) ans=min(ans,query_min(l,mid,rt<<1,L,R));
    if(mid<R) ans=min(ans,query_min(mid+1,r,rt<<1|1,L,R));
    pushup(l,r,rt);
    return ans;
}

int query_max(lrt,int L,int R)
{
    if(L<=l&&r<=R) return maxv[rt];
    int mid=l+r>>1,ans=0;
    if(L<=mid) ans=max(ans,query_max(l,mid,rt<<1,L,R));
    if(mid<R)  ans=max(ans,query_max(mid+1,r,rt<<1|1,L,R));
    pushup(l,r,rt);
    return ans;
}

int val[maxn],cnt=0,mark[maxn];

int main()
{
  ///  ofstream write;
    ///write.open("out.txt");
   while( scanf("%d%d",&n,&m)!=EOF)
   {
        char c[5];int q;
        Build(1,n,1);

        cnt=0;
        memset(mark,0,sizeof(mark));

        for(int i=0;i<m;i++)
        {
            scanf("%s",c);///getchar();
            if(c[0]=='D')
            {
                scanf("%d",&q);
                update(1,n,1,q,q);
                val[cnt++]=q;
                mark[q]=-1;
            }
            else if(c[0]=='Q')
            {
                scanf("%d",&q); if(mark[q]==-1) {puts("0");continue;}///特判下
                int l=query_max(1,n,1,1,q),r=query_min(1,n,1,q,n);
                printf("%d\n",r-l-1);
             ///   write<<r-l-1<<endl;
            }
            else if(c[0]=='R')
            {
                int index=val[--cnt];
                mark[index]=0;
                 update(1,n,1,index);
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37451344/article/details/81611389