BZOJ 3339&3585 Persistent Segment Tree

https://www.lydsy.com/JudgeOnline/problem.php?id=3339

https://www.lydsy.com/JudgeOnline/problem.php?id=3585

An amazing algorithm

You should record the rightest appear in the arrow

And using the Segment Tree to record the Minimum imformation

and binary search

but not really binary search

just on the tree

compared with the l

code of AC:

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10,M=1e8+5;
struct ContinueTree{
    int l,r,Min;
    #define l(x) Tree[x].l
    #define r(x) Tree[x].r
    #define Min(x) Tree[x].Min
}Tree[30*N];
int tot=0;
int A[N],R[N];
void Insert(int &x,int l,int r,int p,int v){
    Tree[++tot]=Tree[x];
    x=tot;
    if(l==r){
        Min(x)=v;
        return;
    }
    int mid=(l+r)/2;
    if(p<=mid) Insert(l(x),l,mid,p,v);
    else Insert(r(x),mid+1,r,p,v);
    Min(x)=min(Min(l(x)),Min(r(x)));
}
int Query(int x,int l,int r,int v){
    if(l==r) return r;
   	int mid=(l+r)/2; 
    if(Min(l(x))<v) return Query(l(x),l,mid,v);
    else return Query(r(x),mid+1,r,v);
}
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;++i){
        scanf("%d",&A[i]);
        R[i]=R[i-1];
        Insert(R[i],0,M,A[i],i);
    }
    while(m--){
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",Query(R[r],0,M,l));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值