hdu1540 Tunnel Warfare【线段树】
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
区间合并
用Ltree 和Rtree分别存【L,R】区间中从L开始到右侧最长的连续区间长度 和从R开始到左侧最长的连续区间长度
pushup的时候需要比较左孩子的Ltree能不能和右孩子的相连 右孩子的Rtree能不能和左孩子的相连
destroy 和rebuilt 都是对点进行更新
query的时候,我们和单点查询一样,直到找到当前点的叶子节点一路查询即可。
这里重点在于区间合并的操作,首先我们讨论当前区间点的问题:
如果当前区间点从左到右能够覆盖查询点,那么ans=max(ans,Lsum【now】);
同理,如果当前区间从右到左能够覆盖查询点,那么ans=max(ans,Rsum【now】);
然后我们讨论当前区间和兄弟节点的合并查询问题:
如果当前区间点是左儿子,那么对应我们查询左儿子的Rsum和右儿子的Lsum是否能够连接并且覆盖查询点,如果可以,那么对应ans=max(ans,Rsum【now】+Lsum【now+1】)
如果当前区间点是右儿子,那么对应我们查询右儿子的Lsum和左儿子的Rsum是否能够连接并且覆盖查询点,如果可以,那么对应ans=max(ans,Lsum【now】+Rsum【now-1】);
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
#define inf 1e18
using namespace std;
int m, n;
const int maxn = 50005;
int Ltree[maxn << 2], Rtree[maxn<<2];
void pushup(int l, int r, int rt)//更新
{
    Ltree[rt] = Ltree[rt * 2];
    Rtree[rt] = Rtree[rt * 2 + 1];
    int m = (l + r) / 2;
    if(Ltree[rt * 2] == (m - l + 1))Ltree[rt] += Ltree[rt * 2 + 1];
    if(Rtree[rt * 2 + 1] == (r - m))Rtree[rt] += Rtree[rt * 2];
}
void build(int l, int r, int rt)
{
    if(l == r){
        Ltree[rt] = Rtree[rt] = l - r + 1;
        return;
    }
    int m = (l + r) >> 1;
    build(l, m, rt << 1);
    build(m + 1, r, rt << 1 | 1);
    pushup(l, r, rt);
}
void update(int L, int C, int l, int r, int rt)
{
    if(l == r){
        if(C == -1){
            Ltree[rt] = Rtree[rt] = 0;
        }
        else{
            Ltree[rt] = Rtree[rt] = 1;
        }
        return;
    }
    int m = (l + r) >>1;
    if(L <= m) update(L, C, l, m, rt << 1);
    else update(L, C, m + 1, r, rt << 1 | 1);
    pushup(l, r, rt);
}
/*void update(int L, int R, int l, int r, int rt)
{
    if(tree[rt] == r - l + 1){//本区间完全在操作区间内
        return;
    }
    if(l == r){
        tree[rt] = sqrt(tree[rt]);
        return;
    }
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, l, m, rt << 1);
    if(R > m) update(L, R, m + 1, r, rt << 1 | 1);
    pushup(rt);
}
*/
int query(int L, int l, int r, int rt)
{
    if(l == r){
        return max(Ltree[rt], Rtree[rt]);
    }
    pushup(l, r, rt);
    int m = (l + r) >> 1;
    //pushdown(rt, m - l + 1, r - m);
    int ans = 0;
    if(L >= l && L <= r){
        if(Ltree[rt] >= L - l + 1)
            ans = max(ans, Ltree[rt]);
        if(Rtree[rt] >= r - L + 1)
            ans = max(ans, Rtree[rt]);
    }
    if(rt % 2 == 0){
        if(Rtree[rt] >= r - L + 1)
            ans = max(ans, Rtree[rt] + Ltree[rt + 1]);
    }
    else{
        if(Ltree[rt] >= L - l + 1)
            ans = max(ans, Ltree[rt] + Rtree[rt - 1]);
    }
    if(L <= m)
        ans = max(ans, query(L, l, m, rt << 1));
    if(L > m)
        ans = max(ans, query(L, m + 1, r, rt << 1|1));
    pushup(l, r, rt);
    return ans;
}
int main()
{
    while(scanf("%d%d", &n, &m) != EOF){
        stack<int>des;
        build(1, n, 1);
        for(int i = 0; i < m; i++){
            getchar();
            char ch;
            int a;
            scanf("%c", &ch);
            if(ch == 'R'){
                if(des.size() == 0)continue;
                a = des.top();des.pop();
                update(a, 1, 1, n, 1);
            }
            else{
                scanf("%d", &a);
                if(ch == 'D'){
                    des.push(a);
                    update(a, -1, 1, n, 1);
                }
                else{
                    printf("%d\n", query(a, 1, n, 1));
                }
            }
        }
    }
    return 0;
}
												
											hdu1540 Tunnel Warfare【线段树】的更多相关文章
- hdu1540 Tunnel Warfare 线段树/树状数组
		
During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast a ...
 - 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 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区 ...
 - 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 ...
 - HDU 1540 Tunnel Warfare (线段树)
		
Tunnel Warfare Problem Description During the War of Resistance Against Japan, tunnel warfare was ca ...
 - Tunnel Warfare   线段树 区间合并|最大最小值
		
B - Tunnel WarfareHDU - 1540 这个有两种方法,一个是区间和并,这个我个人感觉异常恶心 第二种方法就是找最大最小值 kuangbin——线段树专题 H - Tunnel Wa ...
 - HDU 1540 Tunnel Warfare (线段树)
		
题目大意: n 个村庄排列在一条直线上,相邻的村庄有地道连接,除首尾两个村庄外,其余村庄都有两个相邻的村庄.其中有 3 中操作 D x :表示摧毁编号为 x 的村庄,Q x:表示求出包含村庄 x 的最 ...
 - HDU 1540 Tunnel Warfare (线段树或set水过)
		
题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少. 析:首先可以用set水过,set用来记录每个被破坏的村庄,然后查找时,只要查找左右两个端点好. 用线段 ...
 - hdu 1540 Tunnel Warfare 线段树 区间合并
		
题意: 三个操作符 D x:摧毁第x个隧道 R x:修复上一个被摧毁的隧道,将摧毁的隧道入栈,修复就出栈 Q x:查询x所在的最长未摧毁隧道的区间长度. 1.如果当前区间全是未摧毁隧道,返回长度 2. ...
 
随机推荐
- Android的WebView控件载入网页显示速度慢的究极解决方案
			
Android的WebView控件载入网页显示速度慢的究极解决方案 [转载来源自http://hi.baidu.com/goldchocobo/] 秒(甚至更多)时间才会显示出来.研究了很久,搜遍了国 ...
 - HDU 5083 Instruction(字符串处理)
			
Problem Description Nowadays, Jim Green has produced a kind of computer called JG. In his computer, ...
 - 浅谈Java的学习
			
Java就是用来做项目的!Java的主要应用领域就是企业级的项目开发!要想从事企业级的项目开发,你必须掌握如下要点: 1.掌握项目开发的基本步骤2.具备极强的面向对象的分析与设计技巧3.掌握用例驱动. ...
 - RESTful状态码说明
			
https://www.zhihu.com/question/58686782/answer/159603453 常用状态码: 200 请求成功并返回所需资源 400 客户端请求有语法错误 401 未 ...
 - 使用 requests 发送 POST 请求
			
POST请求也就是向服务器提交数据,通常我们用来提交表单数据: import requests postdata = { //定义表单数据 "username": "ab ...
 - [Python] NotImplemented 和 NotImplementedError 区别
			
NotImplemented 是一个非异常对象,NotImplementedError 是一个异常对象. >>> NotImplemented NotImplemented > ...
 - [Vim] Vim 常用基本操作
			
1. 导航 1.1. 查看行号 :set number 显示行号 :set number! 隐藏行号 :.= 在底部显示当前行号 := 在底部显示总行号 1.2. 移动光标 0 或 ^ ...
 - iOS富文本组件的实现—DTCoreText源码解析 渲染篇
			
本文转载至 http://blog.cnbang.net/tech/2729/ 上一篇介绍了DTCoreText怎样把HTML+CSS解析转换成NSAttributeString,本篇接着看看怎样把N ...
 - windows下dump文件调试
			
dump调试:在系统中异常或者崩溃的时候,来生成dump文件,然后用调试器来调试.这样就可以在生产环境中的dmp文件,拷贝到自己的开发机器上,调试就可以找到错误的位置,配合程序调试符号pdb文件,直接 ...
 - Android源码中中一种常见的struct使用方法
			
直接看例子: #include<iostream> #include<stdlib.h> using namespace std; struct Base{ int ba; i ...