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 ...
随机推荐
- wxWidgets与其他工具库的比较(上)
本文是在wxWidgets Wiki上面找到的一篇,对比了wxWidgets和其他一些界面工具的特点.看到很多朋友在网上询问这些库各自的特点,我想先把这篇文章翻译出来——毕竟这也算是一篇官方的文章,应 ...
- 看病要排队--hdu1873
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1873 运用优先队列写就行了 #include<stdio.h> #include< ...
- ubuntu 下关闭apache服务自动启动
最近在自己的ubuntu安装了apache服务器,每次开机的时候通过: ps -A 命令发现apache服务总是自动启动,如下: 本来自己的电脑内存就小,现在也不用这个服务,所以想让apa ...
- HDU 1068 Girls And Boys 二分图题解
版权声明:本文作者靖心.靖空间地址:http://blog.csdn.net/kenden23/,未经本作者同意不得转载. https://blog.csdn.net/kenden23/article ...
- 一行代码彻底禁用WordPress缩略图自动裁剪功能
记得在博客分享七牛缩略图教程的时候,提到过 WordPress 默认会将上传的图片裁剪成多个,不但占用磁盘空间,也会拖慢网站性能,相当闹心! 当时也提到了解决办法: ①.关闭主题自带缩略图裁剪功能(若 ...
- 飞跃平野(sdut1124)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1124 飞跃原野 Time Limit: 500 ...
- 置换检验(Permutation Test)学习[转载]
转自:https://www.cnblogs.com/bnuvincent/p/6813785.html http://www.bioinfo-scrounger.com/archives/564 1 ...
- [LeetCode] 628. Maximum Product of Three Numbers_Easy
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] 182. Duplicate Emails_Easy tag: SQL
Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...
- 用python写http接口自动化测试框架
本文是转载张元礼的博客 http://blog.csdn.Net/vincetest 一.测试需求描述 对服务后台一系列的http接口功能测试. 输入:根据接口描述构造不同的参数输入值 输出:XML文 ...