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 ...
随机推荐
- react-native run-android error: unknown host service
D:\rnworkspace\Hello>react-native run-android JS server already running.Running D:\Android\sdk/pl ...
- DOM对象与Jquery对象转换
dom对象的样式是这么加的(js) .style.background = “red”; jquery对象样式是这么加的(jq) .css(“background”,”red”); <div i ...
- data.frame和matrix的一些操作
编写脚本的时候经常会涉及到对data.frame或matrix类型数据的操作,比如取指定列.取指定行.排除指定列或行.根据条件取满足条件的列或行等.在R中,这些操作都是可以通过简单的一条语句就能够实现 ...
- [LeetCode] 232. Implement Queue using Stacks_Easy tag: Design
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Jmeter接口自动化测试 (四)(持续构建)
本文转载至http://www.cnblogs.com/chengtch/p/6145867.html Jmeter是压力测试.接口测试工具,Ant是基于Java的构建工具,具有跨平台的作用,jen ...
- smali注入常用代码
注入代码需要注意寄存器个数.1.插入log信息 const-string v2,"SN" invoke-static {v2,v0}, Landroid/util/Log;-> ...
- php传值,传地址,传引用的区别
传值, 是把实参的值赋值给行参 那么对行参的修改,不会影响实参的值 传地址 是传值的一种特殊方式,只是他传递的是地址,不是普通的如int 那么传地址以后,实参和行参都指向同一个对象 传 ...
- volatile的语义与实现
1.volatile关键字的两层语义 一旦一个共享变量(类的成员变量.类的静态成员变量)被volatile修饰之后,那么就具备了两层语义: 1)保证了不同线程对这个变量进行操作时的可见性,即一个线程修 ...
- webpack 3 & React 的配置 。
今天真是难过的一天
- Verilog篇(三)仿真原理
首先引入一个例子: `timescale 1ns/100ps module TB; ...