[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.

输入示例

D
D
D
Q
Q
R
Q
R
Q

输出示例


数据规模及约定

见“输入

题解

黄学长这题用的 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\n", !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 的做法。。。

[POJ2892]Tunnel Warfare的更多相关文章

  1. hdu1540 Tunnel Warfare

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

    Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Lim ...

  3. POJ 2892 Tunnel Warfare(线段树单点更新区间合并)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7876   Accepted: 3259 D ...

  4. HDU 1540 Tunnel Warfare 线段树区间合并

    Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  5. hdu 1540 Tunnel Warfare (区间线段树(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1540 Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) ...

  6. poj 2892 Tunnel Warfare(线段树)

    Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7499   Accepted: 3096 D ...

  7. HDU-1540          Tunnel Warfare

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. hdu 1540 Tunnel Warfare(线段树区间统计)

    Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. HDU 1540 Tunnel Warfare(最长连续区间 基础)

    校赛,还有什么途径可以申请加入ACM校队?  Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/ ...

随机推荐

  1. git如何放弃所有本地修改?

    问题描述: 本地做了一些修改,我用git rebase说有冲突.我现在想把本地的请求都干掉,可能有的已经commit过了(没有push过),完全同步成远程版本,应该用什么命令? 使用命令: git r ...

  2. mybatis int 类型判断<if>

    如果数据类型是integer或者int,也就是数据类型的,在用<if>标签做动态语句的时候 不用判断是否为"''" <if test="sex != n ...

  3. IP-Address TextBox

    http://www.codeproject.com/Articles/4693/IP-Address-TextBox 可以下载试用效果.个人感觉功能很强大,但输入时让人不太舒服.可以参考. ntro ...

  4. Java数据库——事务处理

    在数据库中执行5条SQL语句,这些SQL语句本身需要保持一致,即要么同时成功,要么同时失败 事务基本操作 //============================================= ...

  5. TCP/IP详解

    第一篇 TCPIP协议详解 第1章 TCPIP协议族 第2章 IP协议详解 第3章 TCP协议详解 第4章 TCP/IP通信案例:访问Internet上的Web服务器 一.TCP/IP协议族 TCP/ ...

  6. JS,JQ点击事件

    1.点击显示,再次点击隐藏 $("#pingfen-click").click(function(){ name = document.getElementById("p ...

  7. ie6下absolute:fixed问题,完美兼容

    普通css代码 .fix_foot{height: 30px; background: #ff0000; position: %; z-index:;} ie6兼容代码 * html,* html b ...

  8. 好看的CSS按钮

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. TMS320C64X+ 中使用EDMA3中断

    关于EDMA3,TI的文档有详细的介绍.园子里的文章 http://www.cnblogs.com/fpga/archive/2009/10/13/1582516.html,http://www.cn ...

  10. 数字格式化函数:Highcharts.numberFormat()

    (转)数字格式化函数:Highcharts.numberFormat() 一.函数说明 该函数用于图表中数值的格式化,常见用途有数值精度控制.小数点符.千位符显示控制等.   二.函数使用   1.函 ...