HDU1540 Tunnel Warfare(线段树,区间合并)

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25 Accepted Submission(s): 15
 
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
 
 
Source
POJ Monthly
 
emmm,具体看代码部分吧
#include <iostream>
#include <algorithm>
#include <stack>
#include <cstdio>
using namespace std;
const int maxn = 50010;
int n, m;
struct Node {
    int left, right;
    int ls, ms, rs;//左端最大连续区间,区间内最大连续区间,右端最大连续区间(注意是最大连续区间。这三者可能相等
}tree[maxn << 2];

void init(int node, int left, int right) {
    tree[node].left = left;
    tree[node].right = right;
    tree[node].ls = tree[node].ms = tree[node].rs = right - left + 1;
    
    if (left != right) {
        int mid = (left + right) >> 1;
        init(node << 1, left, mid);
        init(node << 1 | 1, mid + 1, right);
    }
}

void insert(int node, int pos, int val) {
    int l = tree[node].left, r = tree[node].right;
    if (l == r) {
        tree[node].ls = tree[node].rs = tree[node].ms = val;
        return ;
    }
    int mid = (l + r) >> 1;
    
    if (pos <= mid) {
        insert(node << 1, pos, val);
    } else {
        insert(node << 1 | 1, pos, val);
    }
    tree[node].ls = tree[node << 1].ls;//父亲的左连续区间至少等于左二字的左连续区间
    tree[node].rs = tree[node << 1 | 1].rs;//同上
    //区间最大值等于两个儿子中的其中一个最大值,或者左右儿子中间相加的部分
    tree[node].ms = max(max(tree[node << 1].ms, tree[node << 1 | 1].ms), tree[node << 1].rs + tree[node << 1 | 1].ls);
    
    if (tree[node << 1].ls == tree[node << 1].right - tree[node << 1].left + 1)
        tree[node].ls += tree[node << 1 | 1].ls;//左子树的左连续区间值满的话,父节点的左连续区间就加上右子树的
    if (tree[node << 1 | 1].rs == tree[node << 1 | 1].right - tree[node << 1 | 1].left + 1)
        tree[node].rs += tree[node << 1].rs;//同上
    
}

int query(int node, int pos) {
        //符合条件直接返回
    if (tree[node].left == tree[node].right || tree[node].ms == 0 || tree[node].ms == tree[node].right - tree[node].left + 1)
        return tree[node].ms;
    int mid = (tree[node].left + tree[node].right) >> 1;
    
    if (pos <= mid) {//这个地方画个图比较好理解
        if (pos >= tree[node << 1].right - tree[node << 1].rs + 1)
            return query(node << 1, pos) + query(node << 1 | 1, mid + 1);//因为t<=mid,看左子树,a[2*i].r-a[2*i].rs+1代表左子树右边连续区间的左边界值,如果t在左子树的右区间内,则要看右子树的左区间有多长并返回
        else
            return query(node << 1, pos);
    } else {
        if (pos <= tree[node << 1 | 1].ls + tree[node << 1 | 1].left - 1)
            return query(node << 1, mid) + query(node << 1 | 1, pos);
        else
            return query(node << 1 | 1, pos);
    }
}

int main() {
        //freopen("in.txt", "r", stdin);
    char ch[2];
    while (scanf("%d%d", &n, &m) != EOF) {
        stack<int> st;
        init(1, 1, n);
        while (m--) {
            int x;
            scanf("%s", ch);
            if (ch[0] == 'D') {
                scanf("%d", &x);
                st.push(x);
                insert(1, x, 0);
            } else if (ch[0] == 'Q') {
                scanf("%d", &x);
                printf("%d\n", query(1, x));
            } else {
                x = st.top();
                st.pop();
                insert(1, x, 1);
            }
        }
    }
}


通过短时倒谱(Cepstrogram)计算进行时-倒频分析研究(Matlab代码实现)内容概要:本文主要介绍了一项关于短时倒谱(Cepstrogram)计算在时-倒频分析中的研究,并提供了相应的Matlab代码实现。通过短时倒谱分析方法,能够有效提取信号在时间与倒频率域的特征,适用于语音、机械振动、生物医学等领域的信号处理与故障诊断。文中阐述了倒谱分析的基本原理、短时倒谱的计算流程及其在实际工程中的应用价值,展示了如何利用Matlab进行时-倒频图的可视化与分析,帮助研究人员深入理解非平稳信号的周期性成分与谐波结构。; 适合人群:具备一定信号处理基础,熟悉Matlab编程,从事电子信息、机械工程、生物医学或通信等相关领域科研工作的研究生、工程师及科研人员。; 使用场景及目标:①掌握倒谱分析与短时倒谱的基本理论及其与傅里叶变换的关系;②学习如何用Matlab实现Cepstrogram并应用于实际信号的周期性特征提取与故障诊断;③为语音识别、机械设备状态监测、振动信号分析等研究提供技术支持与方法参考; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,先理解倒谱的基本概念再逐步实现短时倒谱分析,注意参数设置如窗长、重叠率等对结果的影响,同时可将该方法与其他时频分析方法(如STFT、小波变换)进行对比,以提升对信号特征的理解能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值