Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2396    Accepted Submission(s): 886

Problem Description
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

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!

 
Input
The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

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.

 
Output
Output the answer to each of the Army commanders’ request in order on a separate line.
 
Sample Input
7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4
 
Sample Output
1 0 2 4

题意:题目给出一连串点,会破坏指定的点,修复上一个点(依次向上回溯),然后最后求包含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(线段树区间维护&求最长连续区间)题解的更多相关文章

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

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

  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 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...

  4. hdu1540 Tunnel Warfare 线段树/树状数组

    During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...

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

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

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

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

  7. hdu 1556 Color the ball(线段树区间维护+单点求值)

    传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/3276 ...

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

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

  9. HDU 1540 Tunnel Warfare (线段树)

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

随机推荐

  1. LoadRunner-关联(自动关联和手动关联)

    使用LR录制脚本,新建课程:每次新建课程都会产生一个新的courseid和partid,直接使用录制的脚本执行是不行的.所以用到了关联. 注:关联分手动和自动关联,自动关联搜索出一些不必要关联的数据, ...

  2. bokeyuan_python文章爬去入mongodb读取--LOWBIPROGRAMMER

    # -*- coding: utf-8 -*- import requests,os from lxml import etree from pymongo import * class Boke(o ...

  3. AppFog使用

    cd ‘你的应用目录’ af login(之后输入你的邮箱和密码) af update ‘你的应用名’ 相关文档:https://docs.appfog.com/getting-started/af- ...

  4. 【Pyton】【小甲鱼】类和对象

    一.类 定义一个类,例子如下: class Turtle: #定义一个名为Turtle的类,Python中类型约定以大写字母开头 #属性 color='green' weight=10 legs=4 ...

  5. 数据结构线性表的动态分配顺序存储结构算法c语言具体实现和算法时间复杂度分析

    #include<stdio.h>#include<stdlib.h>//线性表的动态分配顺序存储结构#define LIST_INIT_SIZE 100//线性表存储空间的初 ...

  6. VMware coding Challenge:Date of Weekday

    这道题再次证明了这种细节的题目,画个图容易搞清楚 import java.util.Scanner; public class Solution2 { static int DateOfWeekday ...

  7. 【调优】Nginx性能调优

    一.Nginx优化配置 1.主配置文件优化:# vi /usr/local/nginx/conf/nginx.conf----------------------------------------- ...

  8. linux locate

    locate命令查找文件比find速度快很多,locate是在linux下实现快速查找文件的工具.相应的windows下有everything功能也很强大. [root@wuzhigang lib]# ...

  9. 【译】在Asp.Net中操作PDF - iTextSharp - 利用列进行排版(转)

    [译]在Asp.Net中操作PDF - iTextSharp - 利用列进行排版   在使用iTextSharp通过ASP.Net生成PDF的系列文章中,前面的文章已经讲述了iTextSharp所涵盖 ...

  10. Python: 用shell通配符匹配字符串,fnmatch/fnmatchcase

    问题:想使用Unix Shell 中常用的通配符(比如*.py , Dat[0-9]*.csv 等) 去匹配文本字符串 解决方案: 1. fnmatch 模块提供了两个函数—— fnmatch() 和 ...