题目链接

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. UWP必备知识:App File Explorer

    由来 应用在手机端出问题时如果查看LocalState文件夹的数据库文件与日志文件 如何查看应用在手机端占用带宽与占用CPU内存情况 介绍 [UWP开发之Mvvmlight实践七:如何查找设备(Mob ...

  2. # C#不登录电脑启动程序

    我们知道开机自启动程序如果在用户不登录的情况下是不启动的,但是服务类程序是可以跨过用户登录启动的,例如IIS服务,SQL服务.如果我们已经写好了桌面应用程序,又希望他开机自启动,那就需要借助系统服务在 ...

  3. 在VMWare虚拟机中安装Ubuntu 16.04.1 LTS

    一.需要的准备 安装好VMWare虚拟机(傻瓜式安装,一直next就可以,请支持正版),将Ubuntu的系统镜像下载好,目前最新的LTS版本为16.04.1. 我把虚拟机和Ubuntu镜像传到了百度云 ...

  4. Wireshark网络抓包(二)——过滤器

    一.捕获过滤器 选中捕获选项后,就会弹出下面这个框,在红色输入框中就可以编写过滤规则. 1)捕获单个IP地址 2)捕获IP地址范围 3)捕获广播或多播地址 4)捕获MAC地址 5)捕获所有端口号 6) ...

  5. HDU 3783 ZOJ

    ZOJ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HL-340编译驱动

    make -C /lib/modules/3.4.39/build SUBDIRS=/home/linaro/Desktop/ch341_drv modulesmake: *** /lib/modul ...

  7. 国内不能使用Google解决方案(不断更新与递增中...)

    1.修改hosts方式可以根据对hosts的配置,解决Google搜索.Gmail.谷歌学术.维基百科.GitHub.Twitter.Facebook.Flickr.imgur.Google Serv ...

  8. Unity 碰撞器和触发器的理解

    要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一起,除非两个对 ...

  9. android学习16——library project的使用

    library project和普通的project没有区别.用如下命令新建的一个工程. android create project --target 3 --name MyActivity --p ...

  10. 单线程JavaScript

    最近在阅读<你不知道的JavaScript中卷>,当我看到第二部分介绍异步和回调函数的一些知识时,由于该书在第二部分1.2章对线程.事件循环的概念介绍的并非详细,因此引发了我的一系列思考. ...