首页 > 其他 > 详细

求点所在的最长连续区间

时间:2019-09-29 16:47:14      阅读:92      评论:0      收藏:0      [点我收藏+]

http://acm.hdu.edu.cn/showproblem.php?pid=1540

Tunnel Warfare

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


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
 

 

Recommend
LL
 https://blog.csdn.net/chudongfang2015/article/details/52133243
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdio.h>
#include <queue>
#include <stack>;
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
const int N = 50009;
ll sum ;
int n , m ;
int a[50009];
char str[5];
int ma , mi  ;

struct node
{
    int l , r ;
    int maxn , minn ;
}tree[N<<2];


void build(int l ,int r , int root)
{
    tree[root].l = l , tree[root].r = r ;
    if(l == r)
    {
        tree[root].maxn = 0 ;
        tree[root].minn = n+1 ;
        return ;
    }
    int mid = (l + r)>>1 ;
    build(l , mid , root*2);
    build(mid+1 , r , root*2+1);
    tree[root].maxn = max(tree[root*2].maxn , tree[root*2+1].maxn) ;
    tree[root].minn = min(tree[root*2].minn , tree[root*2+1].minn) ;
}

void update_min(int x , int val , int root)
{
    if(tree[root].l == tree[root].r)
    {
        tree[root].minn = val ;
        return ;
    }
    int mid = (tree[root].r + tree[root].l) >> 1 ;
    if(x <= mid)
    {
        update_min(x , val , root*2);
    }
    else
    {
        update_min(x  , val , root*2+1);
    }
    tree[root].minn = min(tree[root*2].minn , tree[root*2+1].minn);

}

void update_max(int x , int val ,  int root)
{
    if(tree[root].l == tree[root].r)
    {
        tree[root].maxn = val ;
        return ;
    }
    int mid = (tree[root].r + tree[root].l) >> 1 ;
    if(x <= mid)
    {
        update_max(x , val , root*2);
    }
    else
    {
        update_max(x  , val , root*2+1);
    }
    tree[root].maxn = max(tree[root*2].maxn , tree[root*2+1].maxn);
}


void queryl(int l , int r , int root)
{
    if(tree[root].l >= l && tree[root].r <= r)
    {
        ma = max(ma , tree[root].maxn);
        return ;
    }
    int mid = ( tree[root].l + tree[root].r ) >> 1 ;
    if(l <= mid)
        queryl(l , r , root*2);
    if(r > mid)
        queryl(l , r , root*2+1);
}

void queryr(int l , int r , int root)
{
    if(tree[root].l >= l && tree[root].r <= r)
    {
        mi = min(mi , tree[root].minn);
        return ;
    }
    int mid = (tree[root].l + tree[root].r) >> 1 ;
    if(l <= mid)
        queryr(l , r , root*2);
    if(r > mid)
        queryr(l , r , root*2+1);
}

int main()
{

    while(~scanf("%d%d" , &n , &m))
    {
        stack<int>s;
        build(1 , n , 1);
        int l = 0 ;
        for(int i = 0 ; i < m ; i++)
        {
            scanf("%s" , str);
            if(str[0] == D)
            {
                int x ;
                scanf("%d" , &x);
                update_max(x , x , 1);
                update_min(x , x ,  1);
                s.push(x);
            }
            else if(str[0] == Q)
            {
                sum = 0 ;
                int x;
                scanf("%d" , &x);
                mi = INF , ma = -INF;
                queryl(1 , x , 1);
                queryr(x , n , 1);
                if(mi == ma) cout << 0 << endl ;
                else
                    cout << mi - ma - 1 <<endl ;
            }
            else
            {
                int x ;
                x = s.top();
                s.pop();
                update_max(x , 0  , 1);
                update_min(x , n+1 , 1);
            }

        }
    }


    return 0;
}

 

求点所在的最长连续区间

原文:https://www.cnblogs.com/nonames/p/11607997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!