zoukankan      html  css  js  c++  java
  • HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解

    Tunnel Warfare

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


    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

     

    题意:题目给出一连串点,会破坏指定的点,修复上一个点(依次向上回溯),然后最后求包含X的最长连续区间长度

    思路:设定每个区间都有一个左最大连续子区间长度,右最大连续子区间长度,然后一直维护这两个值。

    代码:

    #include<queue>
    #include<cstring>
    #include<set>
    #include<map>
    #include<stack>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #define ll long long
    const int N = 50000+5;
    const int MOD = 20071027;
    using namespace std;
    struct Node{
    	int l,r;
    	int lmax,rmax;
    }node[N<<2];
    void build(int l,int r,int rt){
    	node[rt].l = l;
    	node[rt].r = r;
    	node[rt].lmax = node[rt].rmax = r - l + 1;
    	if(l == r) return;
    	int m = (l + r) >> 1;
    	build(l,m,rt<<1);
    	build(m+1,r,rt<<1|1);
    }
    void fresh(int rt){    //更新左右最大值
    	node[rt].lmax = node[rt<<1].lmax;
    	if(node[rt<<1].lmax + node[rt<<1].l - 1 == node[rt<<1].r)   
    		node[rt].lmax += node[rt<<1|1].lmax;
    		
    	node[rt].rmax = node[rt<<1|1].rmax;
    	if(node[rt<<1|1].r - node[rt<<1|1].rmax + 1 == node[rt<<1|1].l) 
    		node[rt].rmax += node[rt<<1].rmax;
    }
    void update(int v,int rt,int x){
    	if(node[rt].l == node[rt].r){
    		node[rt].lmax = node[rt].rmax = v;
    		return;
    	}
    	int m = (node[rt].l + node[rt].r) >> 1;
    	if(x <= m) update(v,rt<<1,x);
    	else update(v,rt<<1|1,x);
    	fresh(rt);
    }
    int query(int rt,int x){
    	if(node[rt].l == node[rt].r){
    		return node[rt].lmax;
    	}
    	int m = (node[rt].l + node[rt].r) >> 1;
    	if(x <= m){
    		if(x >= node[rt<<1].r - node[rt<<1].rmax + 1){
    			return node[rt<<1].rmax + node[rt<<1|1].lmax;
    		} 
    		else{
    			return query(rt<<1,x);
    		}
    	}
    	else{
    		if(x <= node[rt<<1|1].lmax + node[rt<<1|1].l - 1){
    			return node[rt<<1|1].lmax + node[rt<<1].rmax;
    		}
    		else{
    			return query(rt<<1|1,x);
    		}
    	}
    }
    
    int main(){
    	int n,q,x,last;
    	char arr[2];
    	while(~scanf("%d%d",&n,&q)){
    		stack<int> reb;
    		build(1,n,1);
    		while(q--){
    			scanf("%s",arr);
    			if(arr[0] == 'D'){
    				scanf("%d",&x);
    				update(0,1,x);
    				reb.push(x);
    			}
    			else if(arr[0] == 'R'){
    				x=reb.top();
    				reb.pop();
    				update(1,1,x);
    			}
    			else{
    				scanf("%d",&x);
    				printf("%d
    ",query(1,x));
    			}
    		}
    	}
        return 0;
    }


  • 相关阅读:
    Linux 下 的 Oracle,如何安装 tnsname
    checkpoint process vs writer process vs wal writer process
    PostgreSQL的checkpoint能否并行
    PostgreSQL参数学习:wal_keep_segments
    PostgreSQL参数学习:vacuum_defer_clean_age
    PostgreSQL参数学习:max_wal_senders
    对PostgreSQL数据库的hstore类型建立GisT索引的实验
    PostgreSQL的hstore初步学习
    乐鑫esp8266基于freeRtos实现私有服务器本地远程OTA升级
    Android 6.0 超级简单的权限申请2 (Permission)
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9408803.html
Copyright © 2011-2022 走看看