zoukankan      html  css  js  c++  java
  • HDU

    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

    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MAXN = 50005;
    
    struct D{
    	int l,r,maxn,len;
    }Tree[4*MAXN];
    int S[MAXN];
    int top;
    
    void Up(int temp){
    	Tree[temp].l = Tree[temp<<1].l;
    	if(Tree[temp<<1].l == Tree[temp<<1].len)Tree[temp].l += Tree[temp<<1|1].l;
    	Tree[temp].r = Tree[temp<<1|1].r;
    	if(Tree[temp<<1|1].r == Tree[temp<<1|1].len)Tree[temp].r += Tree[temp<<1].r;
    	Tree[temp].maxn = max(Tree[temp<<1].maxn,Tree[temp<<1|1].maxn);
    	Tree[temp].maxn = max(Tree[temp].maxn,Tree[temp<<1].r+Tree[temp<<1|1].l);
    }
    
    void Build(int temp,int left,int right){
    	Tree[temp].len = right-left+1;
    	Tree[temp].l = Tree[temp].r = Tree[temp].maxn = Tree[temp].len;
    	if(left == right)return;
    	int mid = left + (right-left)/2;
    	Build(temp<<1,left,mid);
    	Build(temp<<1|1,mid+1,right);
    }
    
    void Updata(int temp,int left,int right,int tempt,int value){
    	if(left == right){
    		Tree[temp].l = Tree[temp].r = Tree[temp].maxn = value;
    		return;
    	}
    	int mid = left + (right-left)/2;
    	if(tempt<=mid)Updata(temp<<1,left,mid,tempt,value);
    	else Updata(temp<<1|1,mid+1,right,tempt,value);
    	
    	Up(temp);
    }
    
    int Query(int temp,int left,int right,int tempt){
    	if(Tree[temp].maxn == Tree[temp].len || Tree[temp].maxn == 0 || left == right)return Tree[temp].maxn;
    	int mid = left + (right-left)/2;
    	if(tempt<=mid){
    		if(mid-Tree[temp<<1].r+1<=tempt)return Query(temp<<1,left,mid,tempt)+Query(temp<<1|1,mid+1,right,mid+1);
    		else return Query(temp<<1,left,mid,tempt);
    	}else {
    		if(mid+Tree[temp<<1|1].l>=tempt)return Query(temp<<1,left,mid,mid)+Query(temp<<1|1,mid+1,right,tempt);
    		else return Query(temp<<1|1,mid+1,right,tempt);
    	}
    }
    
    int main(){
    	
    	int N,M;
    	while(scanf("%d %d",&N,&M)!=EOF){
    		Build(1,1,N);
    		char ch;
    		int A;
    		top = 0;
    		while(M--){
    			getchar();
    			scanf("%c",&ch);
    			switch(ch){
    				case 'D':
    					scanf("%d",&A);
    					Updata(1,1,N,A,0);
    					S[top++] = A;
    					break;
    				case 'R':
    					Updata(1,1,N,S[--top],1);
    					break;
    				case 'Q':
    					scanf("%d",&A);
    					printf("%d
    ",Query(1,1,N,A));
    			}
    		}	
    	}
    
    	
    	return 0;
    }



  • 相关阅读:
    [转载]android开发手册
    [转载]windows phone7 学习笔记10——生命周期/墓碑化
    [转载]Windows Phone 系列 本地数据存储
    【转载】windows phone7 学习笔记12——推送通知服务
    【转载】windows phone7 学习笔记15——Bing Maps
    [转载]WP7交互特性浅析及APP设计探究
    【】windows phone7 学习笔记11——启动器与选择器
    [转载]支持的 Windows Phone 媒体编解码器
    【转载】wp7屏幕截图代码
    【转载】windows phone7 学习笔记14——地理位置服务与反应性扩展框架
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514124.html
Copyright © 2011-2022 走看看