• [POJ2892]Tunnel Warfare


    [POJ2892]Tunnel Warfare

    试题描述

    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!

    输入

    The first line of the input contains two positive integers n and m (nm ≤ 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:

    1. D x: The x-th village was destroyed.
    2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
    3. R: The village destroyed last was rebuilt.

    输出

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

    输入示例

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

    输出示例

    1
    0
    2
    4

    数据规模及约定

    见“输入

    题解

    黄学长这题用的 treap,然而我觉得这题是线段树裸题。。。

    对于每个点维护它最左边的未被摧毁的村庄和最右边的未被摧毁的村庄的编号,然后是区间赋值、单点查询操作。

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <algorithm>
    using namespace std;
    
    int read() {
    	int x = 0, f = 1; char c = getchar();
    	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
    	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
    	return x * f;
    }
    
    #define maxn 50010
    int n, lp[maxn<<2], rp[maxn<<2], sl[maxn<<2], sr[maxn<<2];
    void build(int L, int R, int o) {
    	if(L == R) lp[o] = 1, rp[o] = n;
    	else {
    		int M = L + R >> 1, lc = o << 1, rc = lc | 1;
    		build(L, M, lc); build(M+1, R, rc);
    	}
    	return ;
    }
    void pushdown(int o) {
    	int lc = o << 1, rc = lc | 1;
    	if(lc >= (maxn << 2)) lc = 0; if(rc >= (maxn << 2)) rc = 0;
    	if(sl[o]) lp[o] = sl[lc] = sl[rc] = sl[o], sl[o] = 0;
    	if(sr[o]) rp[o] = sr[lc] = sr[rc] = sr[o], sr[o] = 0;
    	sl[0] = sr[0] = 0;
    	return ;
    }
    int ql, qr;
    void update(int L, int R, int o, int tl, int tr) {
    	pushdown(o);
    	if(ql <= L && R <= qr) sl[o] = tl, sr[o] = tr;
    	else {
    		int M = L + R >> 1, lc = o << 1, rc = lc | 1;
    		if(ql <= M) update(L, M, lc, tl, tr);
    		if(qr > M) update(M+1, R, rc, tl, tr);
    	}
    	return ;
    }
    int al, ar;
    void query(int L, int R, int o) {
    	pushdown(o);
    	if(L == R) al = lp[o], ar = rp[o];
    	else {
    		int M = L + R >> 1, lc = o << 1, rc = lc | 1;
    		if(ql <= M) query(L, M, lc);
    		else query(M+1, R, rc);
    	}
    	return ;
    }
    
    int Cmd[maxn], top, has[maxn];
    int main() {
    	n = read();
    	int q = read();
    	
    	build(1, n, 1);
    	while(q--) {
    		char tp[2]; scanf("%s", tp); int p;
    		if(tp[0] == 'D') {
    			p = read(); has[p]++; Cmd[++top] = p;
    			ql = p; query(1, n, 1);
    			if(al <= p - 1) ql = al, qr = p - 1, update(1, n, 1, al, p - 1);
    			if(p + 1 <= ar) ql = p + 1, qr = ar, update(1, n, 1, p + 1, ar);
    			ql = qr = p; update(1, n, 1, p + 1, p - 1);
    		}
    		if(tp[0] == 'Q') ql = read(), query(1, n, 1), printf("%d
    ", !has[ql] ? ar - al + 1 : 0);
    		if(tp[0] == 'R') {
    			if(!top) continue;
    			p = Cmd[top--]; has[p]--;
    			if(has[p]) continue;
    			int l, r;
    			if(p > 1) ql = p - 1, query(1, n, 1), l = al;
    			else l = 1;
    			if(p < n) ql = p + 1, query(1, n, 1), r = ar;
    			else r = n;
    			ql = l; qr = r; update(1, n, 1, l, r);
    		}
    	}
    	
    	return 0;
    }
    

    然而我并不了解用 treap 的做法。。。

  • 相关阅读:
    验证码生成程序
    会话技术Cookie&Session
    jsp基础、el技术、jstl标签、javaEE的开发模式
    js原生的Ajax
    java基础-反射
    hibernate 联合主键 composite-id
    在ubuntu18 安装nginx过程,以及遇到的错误!
    报错解决:error: this statement may fall through [-Werror=implicit-fallthrough=]
    nginx.conf中关于nginx-rtmp-module配置指令详解
    搭建Nginx+nginx-rtmp-module的hls流媒体服务器并用OBS进行推流
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6151056.html
Copyright © 2020-2023  润新知