HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解
Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2396 Accepted Submission(s): 886
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!
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.
题意:题目给出一连串点,会破坏指定的点,修复上一个点(依次向上回溯),然后最后求包含X的最长连续区间长度
思路:设定每个区间都有一个左最大连续子区间长度,右最大连续子区间长度,然后一直维护这两个值。
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 50000+5;
const int MOD = 20071027;
using namespace std;
struct Node{
int l,r;
int lmax,rmax;
}node[N<<2];
void build(int l,int r,int rt){
node[rt].l = l;
node[rt].r = r;
node[rt].lmax = node[rt].rmax = r - l + 1;
if(l == r) return;
int m = (l + r) >> 1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
}
void fresh(int rt){ //更新左右最大值
node[rt].lmax = node[rt<<1].lmax;
if(node[rt<<1].lmax + node[rt<<1].l - 1 == node[rt<<1].r)
node[rt].lmax += node[rt<<1|1].lmax;
node[rt].rmax = node[rt<<1|1].rmax;
if(node[rt<<1|1].r - node[rt<<1|1].rmax + 1 == node[rt<<1|1].l)
node[rt].rmax += node[rt<<1].rmax;
}
void update(int v,int rt,int x){
if(node[rt].l == node[rt].r){
node[rt].lmax = node[rt].rmax = v;
return;
}
int m = (node[rt].l + node[rt].r) >> 1;
if(x <= m) update(v,rt<<1,x);
else update(v,rt<<1|1,x);
fresh(rt);
}
int query(int rt,int x){
if(node[rt].l == node[rt].r){
return node[rt].lmax;
}
int m = (node[rt].l + node[rt].r) >> 1;
if(x <= m){
if(x >= node[rt<<1].r - node[rt<<1].rmax + 1){
return node[rt<<1].rmax + node[rt<<1|1].lmax;
}
else{
return query(rt<<1,x);
}
}
else{
if(x <= node[rt<<1|1].lmax + node[rt<<1|1].l - 1){
return node[rt<<1|1].lmax + node[rt<<1].rmax;
}
else{
return query(rt<<1|1,x);
}
}
}
int main(){
int n,q,x,last;
char arr[2];
while(~scanf("%d%d",&n,&q)){
stack<int> reb;
build(1,n,1);
while(q--){
scanf("%s",arr);
if(arr[0] == 'D'){
scanf("%d",&x);
update(0,1,x);
reb.push(x);
}
else if(arr[0] == 'R'){
x=reb.top();
reb.pop();
update(1,1,x);
}
else{
scanf("%d",&x);
printf("%d\n",query(1,x));
}
}
}
return 0;
}
HDU1540 Tunnel Warfare(线段树区间维护&求最长连续区间)题解的更多相关文章
- HDU1540 Tunnel Warfare —— 线段树 区间合并
题目链接:https://vjudge.net/problem/HDU-1540 uring the War of Resistance Against Japan, tunnel warfare w ...
- 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 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
- hdu1540 Tunnel Warfare 线段树/树状数组
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
- Tunnel Warfare 线段树 区间合并|最大最小值
B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...
- hdu 1540 Tunnel Warfare 线段树 区间合并
题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...
- hdu 1556 Color the ball(线段树区间维护+单点求值)
传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并
Tunnel Warfare Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1540 Tunnel Warfare (线段树)
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
随机推荐
- iphone传照片还是用QQ比较好
之前设置相机格式为高效,通过用91助手的苹果助手导出来文件格式为HEIC,window下没法打开.网上介绍用微信传,发现传的是缩略图,不清楚.最后选择通过qq传比较高清.注意,如果从相册中进行选择的话 ...
- 纯代码实现WordPress上传图片自动重命名的方法
在我们使用 WordPress 发布文章时,经常都需要添加图片.多媒体什么的.然而,大家都知道 WordPress 是舶来物,对于中文用户来说,我们都会把图片命名为中文的,由于 WordPress 机 ...
- [javascript]编码&i字符串格式化&nput历史记录&清空模态框
js中编码问题 https://www.haorooms.com/post/js_escape_encodeURIComponent 我在前端js添加时候创建dom时候,有汉字,发现是乱码就研究了下 ...
- QC质量管理七大手法
1.层别法 层别法就是将大量有关某一特定主题的观点.意见或想法按组分类,将收集到的大量的数据或资料按相互关系进行分组,加以层别.层别法一般和柏拉图.直方图等其它七大手法结合使用,也可单独使用. 2.查 ...
- 4.keras实现-->生成式深度学习之DeepDream
DeepDream是一种艺术性的图像修改技术,它用到了卷积神经网络学到的表示,DeepDream由Google于2015年发布.这个算法与卷积神经网络过滤器可视化技术几乎相同,都是反向运行一个卷积神经 ...
- discuz formhash
class.core.php中 $this->var['formhash'] = formhash();define('FORMHASH', $this->var['formhash']) ...
- 1:4 UI标签和通用标签
UI标签:负责用户界面输出的标签. 非标单:例如错误信息提示的标签 fielderror,actionerror,actionmessagr:系统错误消息的自动显示 通 ...
- boostrap小图标
- yii的url写法
Yii 各种url地址写法 echo Url::home(); 生成入口地址/yii2test/frontend/web/index.php: echo Url::base();生成入口文件夹地址: ...
- Understanding Convolutional Neural Networks for NLP
When we hear about Convolutional Neural Network (CNNs), we typically think of Computer Vision. CNNs ...