题目链接

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 (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

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

Hint

An illustration of the sample input:

      OOOOOOO

D 3 OOXOOOO

D 6 OOXOOXO

D 5 OOXOXXO

R OOXOOXO

R OOXOOOO

Source

 
题意:有n个村庄编号为1,2,3...n 它们按照序号一一相连,现在有m次操作,有以下几种操作:
        1、D  x  表示将x号村庄摧毁。
        2、Q  x  表示查询x村庄能到达的村庄数(包括x村庄)。
        3、R      表示修复最近一个被摧毁的村庄。
        每次查询输出一个值。
 
思路:线段树单点更新、区间合并,用栈存储被摧毁的村庄号。
 
代码如下:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
const int maxn=;
stack<int> s;
struct Node{
int l,r,m;
}tr[*maxn]; void build(int i,int l,int r)
{
tr[i].l=tr[i].r=tr[i].m=r-l+;
if(l==r) return;
int mid=(l+r)/;
build(*i,l,mid);
build(*i+,mid+,r);
} void update(int i,int l,int r,int x,int y)
{
if(l==r)
{
tr[i].l=tr[i].r=tr[i].m=y;
return;
}
int mid=(l+r)/;
if(x<=mid) update(*i,l,mid,x,y);
else update(*i+,mid+,r,x,y); if(tr[*i].m==mid-l+) tr[i].l=tr[*i].m+tr[*i+].l;
else tr[i].l=tr[*i].l;
if(tr[*i+].m==r-mid) tr[i].r=tr[*i+].m+tr[*i].r;
else tr[i].r=tr[*i+].r;
tr[i].m=max(max(tr[*i].m,tr[*i+].m),tr[*i].r+tr[*i+].l);
} int query(int i,int l,int r,int x)
{
int sum=;
if(l==r) return tr[i].m;
if(r-l+==tr[i].m) return tr[i].m;
int mid=(l+r)/;
if(x<=mid){
if(mid-tr[*i].r+<=x)
return tr[*i].r+tr[*i+].l;
else return query(*i,l,mid,x);
}
else {
if(tr[*i+].l+mid>=x)
return tr[*i].r+tr[*i+].l;
else return query(*i+,mid+,r,x);
}
} int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
scanf("%d",&m);
build(,,n);
int x;
char str[];
while(!s.empty()) s.pop();
while(m--)
{
scanf("%s",str);
if(str[]=='D')
{
scanf("%d",&x);
s.push(x);
update(,,n,x,);
}
else if(str[]=='Q')
{
scanf("%d",&x);
printf("%d\n",query(,,n,x));
}
else
{
update(,,n,s.top(),);
s.pop();
}
}
}
return ;
}

poj 2892---Tunnel Warfare(线段树单点更新、区间合并)的更多相关文章

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

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

  2. hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

    Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  3. hdu1540之线段树单点更新+区间合并

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

  4. HDU 3308 LCIS(线段树单点更新区间合并)

    LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...

  5. hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...

  6. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)

    HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...

  9. hdu 1166线段树 单点更新 区间求和

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. hdu1166(线段树单点更新&区间求和模板)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...

随机推荐

  1. 测试员浅谈App测试的重点

    近年来,手机app也时持续大热.基于安卓和ios的手机app,更是受到众多投资者的青睐.而手机软件测试行业也是如此. 现在听的最多的是web测试和App测试,但实际上两者本质上没有什么区别,性质都一样 ...

  2. 解决Ubuntu 16.04 软件中心闪退

    就是上面这个Ubuntu软件中心,类似如应用市场,今天不知怎么回事竟然抽风了,打开之后几秒就闪退了,导致我安装sublime一致失败,百度之后才知道这是16.04版本的一个毛病,按照我的性格,手机软件 ...

  3. (求租仓库)navigationController .navigationBar 的属性设置

    需要做成的效果如下图的

  4. 依赖lean cloud的注册与登录

    前言 实现登录注册的基本功能,没有添加手机验证和邮箱验证的功能,有相应的方法,如果需要,可以自己加上其相应的方法 github的网址: 效果图: 正文 1.导入leancloud相应的第三方,这个等我 ...

  5. stop总结

    <!DOCTYPE html><html><head><meta charset="utf-8"><script src=&q ...

  6. Unity中的万能对象池

    本文为博主原创文章,欢迎转载.请保留博主链接http://blog.csdn.net/andrewfan Unity编程标准导引-3.4 Unity中的万能对象池 本节通过一个简单的射击子弹的示例来介 ...

  7. 在Express的页面模板中的变量的定义与使用总结

    前言 最近在使用Express框架中的ejs页面模板趟了些许坑,仅以本文记录总结. 本文简述的均为ejs页面模板. 创建ejs变量的各种方法 1. 在Nodejs定义的ejs变量 ejs由是在node ...

  8. [java多线程] - 锁机制&同步代码块&信号量

    在美眉图片下载demo中,我们可以看到多个线程在公用一些变量,这个时候难免会发生冲突.冲突并不可怕,可怕的是当多线程的情况下,你没法控制冲突.按照我的理解在java中实现同步的方式分为三种,分别是:同 ...

  9. Javascript面对对象. 第二篇

    但是还有一个问题,就是识别的问题,因为根本无法搞清楚他们到底是哪个对象的实例. 1.构造函数 function CreateObject(name,age){ //创建一个对象,使用构造函数的对象都是 ...

  10. Winfrom 提示消息框公共类

    1.Winfrom项目经常会使用到消息提示,一般都使用MessageBox.Show方法,但是像错误提示,询问提示,警告提示写起来就有点复杂了,并且后面几种提示都带有图标,但是MessageBox.S ...