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 线段树区间合并的更多相关文章

  1. hdu 1540 Tunnel Warfare 线段树 区间合并

    题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...

  2. hdu 1540 Tunnel Warfare(线段树区间统计)

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

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

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

  4. Tunnel Warfare 线段树 区间合并|最大最小值

    B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...

  5. HDU 1540 Tunnel Warfare (线段树)

    Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...

  6. HDU 1540 Tunnel Warfare (线段树)

    题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...

  7. HDU 1540 Tunnel Warfare (线段树或set水过)

    题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...

  8. HDU1540 Tunnel Warfare —— 线段树 区间合并

    题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...

  9. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

随机推荐

  1. Java连接redis的使用示例

    在多线程下使用Jedis 在不同的线程中使用相同的Jedis实例会发生奇怪的错误.但是创建太多的实现也不好因为这意味着会建立很多sokcet连接,也会导致奇怪的错误发生.单一Jedis实例不是线程安全 ...

  2. 团体程序设计天梯赛-练习集L1-002. 打印沙漏

    L1-002. 打印沙漏 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给 ...

  3. Android Activity交互及App交互

    Android交互--------->Intent Activity之间----->Explicit Intent App之间--------->Implicit Intent

  4. iOS socket编程 第三方库 AsyncSocket(GCDAsyncSocket)

    Socket描述了一个IP.端口对.它简化了程序员的操作,知道对方的IP以及PORT就可以给对方发送消息,再由服务器端来处理发送的这些消息.所以,Socket一定包含了通信的双发,即客户端(Clien ...

  5. SaaS 公司如何应对 On-Call 挑战?

    Cloud Insight 集监控.管理.计算.协作.可视化于一身,帮助所有 IT 公司,减少在系统监控上的人力和时间成本投入,让运维工作更加高效.简单.本文系国内 ITOM 行业领军企业 OneAP ...

  6. python:Attempted relative import in non-package

    problem:Attempted relative import in non-package 所谓相对路径其实就是相对于当前module的路径,但如果直接执行脚本,这个module的name就是“ ...

  7. xcode 把cocos2d-x 以源码的形式包含进自己的项目适合, 性能分析问题的错误

    性能分析:出现如下错误: xcode profile  Variable has incomplete type   class “CC_DLL” 解决办法:在 xcode的Build Setting ...

  8. Android studio 下的 NDK 配置方法和注意事项

    http://blog.csdn.net/u013598660/article/details/47341963

  9. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  10. Android:安卓资源引用符号的含义

    @代表引用资源 @*代表引用系统的非public资源,如: @*android:color/white @[package:]type/name引用自定义资源,如: android:text=&quo ...