HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少
思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定
所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间。
注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间。反之同理。
查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询。
查询的时候还要注意,当前查询点在左儿子的最大右连续区间内时,最大连续区间还要加上右儿子的最大连续区间。反之同理;
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdlib>
// #include <conio.h>
using namespace std;
#define clc(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int N = ;
const int MOD = 1e9+;
#define LL long long
#define mi() (l+r)>>1
double const pi = acos(-);
void fre() {
freopen("in.txt","r",stdin);
}
// inline int r() {
// int x=0,f=1;char ch=getchar();
// while(ch>'9'||ch<'0') {if(ch=='-') f=-1;ch=getchar();}
// while(ch>='0'&&ch<='9') { x=x*10+ch-'0';ch=getchar();}return x*f;
// }
int s[N];
struct node {
int l,r;
int ls,rs,ms;
} e[N<<]; void pushup(int rt) { }
void build(int l,int r,int rt) {
e[rt].l=l;
e[rt].r=r;
e[rt].ls=e[rt].rs=e[rt].ms=r-l+;
if(l!=r) {
int mid=mi();
build(lson);
build(rson);
}
} void update(int rt,int c,int x) {
if(e[rt].l==e[rt].r) {
if(x>) {
e[rt].ls=e[rt].rs=e[rt].ms=;
} else
e[rt].ls=e[rt].rs=e[rt].ms=;
return;
}
int mid=(e[rt].l+e[rt].r)>>;
if(c<=mid) {
update(rt<<,c,x);
} else
update(rt<<|,c,x);
e[rt].ls=e[rt<<].ls;
e[rt].rs=e[rt<<|].rs;
e[rt].ms=max(max(e[rt<<].ms,e[rt<<|].ms),e[rt<<].rs+e[rt<<|].ls);
if(e[rt<<].ls==e[rt<<].r-e[rt<<].l+) {
e[rt].ls+=e[rt<<|].ls;
}
if(e[rt<<|].rs==e[rt<<|].r-e[rt<<|].l+) {
e[rt].rs+=e[rt<<].rs;
}
} int query(int rt,int c) {
if(e[rt].l==e[rt].r||e[rt].ms==||e[rt].ms==e[rt].r-e[rt].l+) {
return e[rt].ms;
}
int mid=(e[rt].l+e[rt].r)>>;
if(c<=mid) {
if(c>=e[rt<<].r-e[rt<<].rs+) {
return query(rt<<,c)+query(rt<<|,mid+);
} else
return query(rt<<,c);
} else {
if(c<=e[rt<<|].l+e[rt<<|].ls-) {
return query(rt<<|,c)+query(rt<<,mid);
} else
return query(rt<<|,c);
}
}
int main() {
// fre();
int n,q,top;
while(~scanf("%d%d",&n,&q)) {
build(,n,);
top=;
while(q--) {
char c;
int x;
cin>>c;
if(c=='D') {
cin>>x;
s[top++]=x;
update(,x,);
} else if(c=='Q') {
cin>>x;
printf("%d\n",query(,x));
} else {
x=s[--top];
update(,x,);
}
}
}
return ;
}
HDU 1540 Tunnel Warfare 线段树区间合并的更多相关文章
- hdu 1540 Tunnel Warfare 线段树 区间合并
题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...
- hdu 1540 Tunnel Warfare(线段树区间统计)
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- Tunnel Warfare 线段树 区间合并|最大最小值
B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...
- HDU 1540 Tunnel Warfare (线段树)
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
- HDU 1540 Tunnel Warfare (线段树)
题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...
- HDU 1540 Tunnel Warfare (线段树或set水过)
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举
HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...
随机推荐
- 一个简单的aJax——后台用servlet技术
示例:webDemo 一.客户端 <%-- Created by IntelliJ IDEA. User: Administrator Date: 15-12-2 Time: 上午5:41 To ...
- json分别算出元素的个数和最多的元素
个数: var str = 'aaafsdsaaasasasasaa'; var json = {}; for (var i = 0; i < str.length; i++) { if(!js ...
- hdu 1176
简单DP 类似于在一个矩形中求最长路径 /************************************************************************* > ...
- [Akka]发送一条消息的内部流程
本想通过了解一下Akka-actor工程中主要的类的概念,来看下Akka内部运作的机制.无奈里边的类的确太多,注释中对每个类的功能也没有足够的解释.所以还是通过debug的方式,找个入手点,看一下互相 ...
- Android Spannable
ApiDemo 源码至 com.example.android.apis.text.Link 类. 首先,看一下其运行效果: 要给 TextView 加上效果,方式主要有几种: 第一种,自动应用效果, ...
- JS中访问对象的属性
方式一: 对象名.属性名; 方式二: 对象名["属性名"]; ★注意:方式二中,属性名以字符串的形式出现在方括号中,这意味着通过方式二访问属性的话,可以实现“动态访问对象的 ...
- 【转】奇异值分解(We Recommend a Singular Value Decomposition)
文章转自:奇异值分解(We Recommend a Singular Value Decomposition) 文章写的浅显易懂,很有意思.但是没找到转载方式,所以复制了过来.一个是备忘,一个是分享给 ...
- BIG5编码表
Big5 (Traditional Chinese) character code table code +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +A +B +C +D +E +F ...
- GPS(1)核心API及3种列出LocationProvider的方法
GPS的常用API Android SDK为GPS提供了很多API,其中LocationManager类是这些API的核心.所有与GPS相关的操作都由LocationManager对象及其派生的对象完 ...
- [ffmpeg 扩展第三方库编译系列] 关于 mingw32 下编译libcaca
在编译前最好先看一下帮助 ./configure --help 开始编译 ./configure --disable-shared --disable-cxx \ --disable-csharp ...