poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
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 (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
Hint
An illustration of the sample input:
OOOOOOO
D 3 OOXOOOO
D 6 OOXOOXO
D 5 OOXOXXO
R OOXOOXO
R OOXOOOO
Source
#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(线段树单点更新、区间合并)的更多相关文章
- POJ 2892 Tunnel Warfare(线段树单点更新区间合并)
Tunnel Warfare Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 7876 Accepted: 3259 D ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- hdu1540之线段树单点更新+区间合并
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- HDU 3308 LCIS(线段树单点更新区间合并)
LCIS Given n integers. You have two operations: U A B: replace the Ath number by B. (index counting ...
- hdu 5316 Magician(2015多校第三场第1题)线段树单点更新+区间合并
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5316 题意:给你n个点,m个操作,每次操作有3个整数t,a,b,t表示操作类型,当t=1时讲a点的值改 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)
POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...
- HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对)
HDU.1394 Minimum Inversion Number (线段树 单点更新 区间求和 逆序对) 题意分析 给出n个数的序列,a1,a2,a3--an,ai∈[0,n-1],求环序列中逆序对 ...
- hdu 1166线段树 单点更新 区间求和
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- hdu1166(线段树单点更新&区间求和模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 题意:中文题诶- 思路:线段树单点更新,区间求和模板 代码: #include <iost ...
随机推荐
- POJ2185(KMP)
Milking Grid Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7896 Accepted: 3408 Desc ...
- [JavaScript] 学习笔记-JavaScript基础教程
1.JavaScript介绍 1)JavaScript是互联网上最流行的脚本语言,这门语言可用于Web和HTML,更可广泛用于服务器.pc端.移动端.JavaScript是一种轻量级的编程语言,插入H ...
- We Chall-Training: Get Sourced-Writeup
MarkdownPad Document html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,ab ...
- java_JDBC字段对应
地址: http://otndnld.oracle.co.jp/document/products/oracle10g/102/doc_cd/java.102/B19275-03/datacc.htm ...
- salesforce 零基础学习(六十四)页面初始化时实现DML操作
有的时候我们往往会遇到此种类似的需求:用户在访问某个详细的记录时,需要记录一下什么时候哪个用户访问过此页面,也就是说进入此页面时,需要插入一条记录到表中,表有用户信息,record id,sObjec ...
- C++学习的心路历程之心理障碍
断断续续的C++学习已经过了1年多了,可是,我还是没有迈出可以自如输出写点什么的那一步.甚至我因为这个老是怀疑自己的智商,我是真心想学懂,因为这个关系到我的就业,直接关系到我的饭碗.我是十分的着急,可 ...
- CentOS系统通过PXE实现批量无人值守安装
通过传统的方式安装和部署计算机时,都需要人工干预的方式完成安装.如果需要部署大量的类似功能的工作站或服务器,则需要耗费大量的时间.同时传统的安装方式,每台计算机都需要光驱设备及安装光盘等介质,会额外增 ...
- matlab中hold指令、figure指令及subplot指令的使用
一.hold指令使用 正常情况下,plot指令显示figure时,以前的数据丢失了.使用hold on指令后,此后添加的一系列plot曲线将叠加在前一个图上当使用hold off后,恢复为默认状况,p ...
- java程序测试之字符流
package filestream; import java.io.FileReader; import java.io.FileWriter; import java.io.FileNotFoun ...
- 安装grub
安装windows后,grub不见了 先安装Neo进入Linux 两条命令搞定. 在root用户下输入: update-grub grub-install /dev/sda