HDU - 1540 Tunnel Warfare(线段树区间合并)
https://cn.vjudge.net/problem/HDU-1540
题意
D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少。
分析
线段树的区间内,我们要用三个变量记录左边连续区间,右边连续区间和最大连续区间。
其它看代码,要比较仔细。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ;
struct ND{
int l,r;
int ls,rs,ms;
}tree[maxn<<];
int n,m,top,s[maxn];
void pushup(int rt){
tree[rt].ls=tree[rt<<].ls;
tree[rt].rs=tree[rt<<|].rs;
//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间,左右子树合并的中间区间,三者中最大的区间值
tree[rt].ms=max(max(tree[rt<<].ms,tree[rt<<|].ms),tree[rt<<].rs+tree[rt<<|].ls);
//左子树区间满了的话,父亲左区间要加上右孩子的左区间
if(tree[rt<<].ls==tree[rt<<].r-tree[rt<<].l+)
tree[rt].ls+=tree[rt<<|].ls;
if(tree[rt<<|].rs==tree[rt<<|].r-tree[rt<<|].l+)//同上
tree[rt].rs+=tree[rt<<].rs;
}
void build(int rt,int l,int r){
tree[rt].l=l,tree[rt].r=r;
tree[rt].ls=tree[rt].rs=tree[rt].ms=r-l+;
if(l==r) return;
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
}
void update(int rt,int x,int val){
if(tree[rt].l==tree[rt].r){
tree[rt].ls=tree[rt].rs=tree[rt].ms=val;
return;
}
int mid=(tree[rt].l+tree[rt].r)>>;
if(mid>=x) update(rt<<,x,val);
if(mid<x) update(rt<<|,x,val);
pushup(rt);
} int query(int rt,int x){
//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
if(tree[rt].l==tree[rt].r||tree[rt].ms==||tree[rt].ms==tree[rt].r-tree[rt].l+)
return tree[rt].ms;
int mid=(tree[rt].l+tree[rt].r)>>;
if(x<=mid){//因为x<=mid,看左子树,
///tree[rt<<1].r-tree[rt<<1].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
if(x>=tree[rt<<1].r-tree[rt<<1].rs+1)
return query(rt<<1,x)+query(rt<<1|1,mid+1);
else return query(rt<<1,x);
}else{
if(x<=tree[rt<<|].ls+tree[rt<<|].l-)
return query(rt<<,mid)+query(rt<<|,x);
else return query(rt<<|,x);
}
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t,cas=;
char op[];
// scanf("%d",&t);
while(~scanf("%d%d",&n,&m)){
top=;
build(,,n);
while(m--){
int x;
scanf("%s",op);
if(op[]=='D'){
scanf("%d",&x);
update(,x,);
s[top++]=x;
}else if(op[]=='Q'){
scanf("%d",&x);
printf("%d\n",query(,x));
}else{
if(x>){
x=s[--top];
update(,x,);
}
}
}
}
return ;
}
HDU - 1540 Tunnel Warfare(线段树区间合并)的更多相关文章
- HDU 1540 Tunnel Warfare 线段树区间合并
Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- 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 ...
随机推荐
- 【BZOJ3236】【AHOI2013】作业 线段树 分治 树状数组
题目描述 给你一个长度为\(n\)的数列,还有\(m\)个询问,对于每个询问\((l,r,a,b)\),输出1.区间\([l,r]\)有多少范围在\([a,b]\)的数:2.区间\([l,r]\)有多 ...
- #565. 「LibreOJ Round #10」mathematican 的二进制(期望 + 分治NTT)
题面 戳这里,题意简单易懂. 题解 首先我们发现,操作是可以不考虑顺序的,因为每次操作会加一个 \(1\) ,每次进位会减少一个 \(1\) ,我们就可以考虑最后 \(1\) 的个数(也就是最后的和) ...
- 一点理解之 CmBacktrace: ARM Cortex-M 系列 MCU 错误追踪库
@2019-02-14 [小记] CmBacktrace: ARM Cortex-M 系列 MCU 错误追踪库,用来将单片机故障状态寄存器值翻译出来输出至终端上以便排错 CmBacktrace: AR ...
- 分离式部署LNMP
-------Nginx----------PHP+NFS------------MySql------192.168.56.202 192.168.56.201 192.168.56.200安装My ...
- [SDOI2011]消防
某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超越宇宙的热情,所以这个国家最兴旺的行业是消防业.由 ...
- OpenLayers学习笔记(四)— QML显示html中openlayers地图的坐标
GitHub:八至 作者:狐狸家的鱼 本文链接:实现QML中显示html中地图的坐标 如何QML与HTML通信已经在这篇文章 QML与HTML通信之画图 详细讲述了 1.HTML var coord; ...
- A1141. PAT Ranking of Institutions
After each PAT, the PAT Center will announce the ranking of institutions based on their students' pe ...
- A1072. Gas Station
A gas station has to be built at such a location that the minimum distance between the station and a ...
- Linux命令模拟Http的get或post请求
Http请求指的是客户端向服务器的请求消息,Http请求主要分为get或post两种,在Linux系统下可以用curl和wget命令来模拟Http的请求. get请求: 1.使用curl命令: cur ...
- 利用captcha库绘制验证码
#导包 from captcha.image import ImageCaptcha from PIL import Image import random import time import os ...