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 ...
随机推荐
- es6笔记6^_^generator
1.简介 Generator函数是一个函数的内部状态的遍历器(也就是说,Generator函数是一个状态机). 形式上,Generator函数是一个普通函数,但是有两个特征. function命令与函 ...
- HDU1173
采矿 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- 吉特仓储管系统(开源)--使用Grunt压缩JS文件
在吉特仓储管理系统开发的过程中大量使用到了JS,随着JS文件的增多我们需要对JS进行有效的管理,同时也要对JS文件进行一些压缩.文本用于记录一下使用grunt压缩JS的操作步骤,便于遗忘之后记录查找, ...
- 源码(07) -- java.util.Iterator<E>
java.util.Iterator<E> 源码分析(JDK1.7) ----------------------------------------------------------- ...
- hashMap_使用
转]Java中HashMap遍历的两种方式原文地址: http://www.javaweb.cc/language/java/032291.shtml 第一种: Map map = new HashM ...
- NAT详解
1.为什么出现了NAT? IP地址只有32位,最多只有42.9亿个地址,还要去掉保留地址.组播地址,能用的地址只有36亿左右,但是当下有数以万亿的主机,没有这么多IP地址怎么办,后面有了IPv6,但是 ...
- asp.net core mvc剖析:启动流程
asp.net core mvc是微软开源的跨平台的mvc框架,首先它跟原有的MVC相比,最大的不同就是跨平台,然后又增加了一些非常实用的新功能,比如taghelper,viewcomponent,D ...
- block之---应用场景:做参数和返回值
1.做参数 什么时候使用Block充当参数? 封装一个功能,这个功能做什么事情由外界决定,但是什么时候调用由内部决定,这时候就需要把Block充当参数去使用. 模拟需求: 封装一个计算器,怎么计算由外 ...
- WPScan初体验
近日在朋友圈看某位dalao在Ubuntu上安装WPScan花了一个小时,于是洒家随手在Kali Linux上输入了wpscan,发现Kali里面已经装好了.于是决定玩两把WPScan. WordPr ...
- 使用JDBC连接数据库(一)
JDBC是由java编程语言编写的类及接口组成,同时它为程序开发人员提供了一组用于实现对数据库访问的JDBC API,并支持SQL语言.利用JDBC可以将JAVA代码连接到oracle.DB2.SQL ...