poj2892 treap

博客围绕抗战时期华北平原隧道战场景展开,介绍了村庄隧道连接情况及入侵者攻击、八路军查询连接状态等事件。输入包含村庄和事件数量,有村庄破坏、查询连接村庄数、重建村庄三种事件。解题使用treap算法,初始化空树,按不同事件操作树。

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 7205 Accepted: 2957

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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

此题为了练习treap,初始化空树,对每次D X,将x插入平衡树,对Q X,则寻找距离x最近和最远的值,R则删除最近插入的数。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#define Maxn 50010
using namespace std;

int maxx,minn;
struct treap{
    int l,r,num,w,rnd;
}tree[Maxn];
int sz; //当前已用节点数
int root;
void rturn(int &t){ //右旋
    int k=tree[t].l;
    tree[t].l=tree[k].r;
    tree[k].r=t;
    t=k;
}
void lturn(int &t){ //左旋
    int k=tree[t].r;
    tree[t].r=tree[k].l;
    tree[k].l=t;
    t=k;
}
//第一次插入调用insert(root,0),root=0
//调用结束后root=1,之后可以正常插入
void insert(int &t,int x){ //t传引用
    if(t==0){ //到叶子节点,新建节点
        t=++sz;
        tree[t].num=x;
        tree[t].w=1;
        tree[t].rnd=rand();
        return;
    }
    if(x==tree[t].num){ //已存在该元素
        //tree[t].w++; //权值加1
        return;
    }
    else if(x<tree[t].num){ //往左子树插入
        insert(tree[t].l,x);
        if(tree[tree[t].l].rnd<tree[t].rnd)
            rturn(t); //t会改变
    }
    else{
        insert(tree[t].r,x);
        if(tree[tree[t].r].rnd<tree[t].rnd)
            lturn(t); //t会改变
    }
}
void del(int &t,int x){
    if(t==0) return;
    if(tree[t].num==x){
        /*
        if(tree[t].w>1){
            tree[t].w--;
            return;
        }*/
        if(tree[t].l*tree[t].r==0) //到叶节点
            t=tree[t].l+tree[t].r;
        else if(tree[tree[t].l].rnd>=tree[tree[t].r].rnd){
            lturn(t);
            del(tree[t].l,x);
        }
        else{
            rturn(t);
            del(tree[t].r,x);
        }
    }
    else if(tree[t].num<x)
        del(tree[t].r,x);
    else del(tree[t].l,x);
}
void find(int &t,int x){
    if(t==0) return;
    if(tree[t].num>=x) maxx=min(maxx,tree[t].num);
    if(tree[t].num<=x) minn=max(minn,tree[t].num);
    if(tree[t].num<x) find(tree[t].r,x);
    else find(tree[t].l,x);
}
char s[2];
int st[Maxn],top;
int vis[Maxn];
int main()
{
    int n,m,a;
    //srand((unsigned)time(NULL));
    while(~scanf("%d%d",&n,&m)){
        memset(tree,0,sizeof tree);
        memset(vis,0,sizeof vis);
        sz=root=top=0;
        for(int i=0;i<m;i++){
            scanf("%s",s);
            if(s[0]=='D'){
                scanf("%d",&a);
                insert(root,a);
                vis[a]=1;
                st[top++]=a;
            }
            else if(s[0]=='R'){
                top--;
                if(vis[st[top]]){
                    del(root,st[top]);
                    vis[st[top]]=0;
                }
            }
            else{
                scanf("%d",&a);
                minn=0,maxx=n+1;
                find(root,a);
                if(minn==maxx) puts("0");
                else printf("%d\n",maxx-minn-1);
            }
        }
    }
	return 0;
}

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值