array

array

权值线段树

查询剪枝

疯狂RE

#include
using namespace std;
#define int long long
#define maxn 100000+500
#define inf n+1
int L[maxn*4],R[maxn*4],V[maxn*4];
int A[maxn],B[maxn];
inline int read()
{
    char ch=getchar();
    int ans=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        ans=ans*10+ch-'0';
        ch=getchar();
    }
    return ans;
}

int n,m,La;
void build(int l,int r,int x)
{
    L[x]=l,R[x]=r;
    if(l==r)
    {
        V[x]=A[l];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,2*x);
    build(mid+1,r,2*x+1);
    V[x]=max(V[2*x],V[2*x+1]);
    return;
}
void update(int x,int d,int w)
{
    if(L[x]==R[x]&&L[x]==d)
    {
        V[x]=w;
        return;
    }else if(L[x]==R[x])return;
    int mid=(L[x]+R[x])/2;
    if(d<=mid)update(2*x,d,w);
    else update(2*x+1,d,w);
    V[x]=max(V[2*x],V[2*x+1]);
}

bool query(int x,int b,int c)
{
  //cout<

    if(L[x]==R[x]&&V[x]>b&&c<=L[x])
    {
        cout<'\n';
        La=L[x];
        return true;
    }
    else if(L[x]==R[x])return false;
    int mid=(L[x]+R[x])/2;
    if(c<=mid&&V[2*x]>b)
        if(query(2*x,b,c))return true;
    if(c<=R[x]&&V[2*x+1]>b)
        if(query(2*x+1,b,c))return true;
    return false;

}
signed main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T,a,b,c;
    T=read();
    while(T--)
    {
        // init();
        n=read(),m=read();
        for(int i=1; i<=n; i++)
        {
            a=read();
            A[a]=i;
            B[i]=a;
        }
        //A[n+1]=n+1;
        build(1,n,1);
        La=0;
        for(int i=1; i<=m; i++)
        {
            a=read();
            if(a==1)
            {
                b=read();
                b^=La;
                update(1,B[b],n+1);
            }
            else
            {
                b=read();
                c=read();
                //cout<
                b^=La;
                c^=La;
                if(!query(1,b,c))
                {
                    cout<1<<'\n';
                    La=(n+1);
                }
            }
        }
    }



}

 

为什么写这种就能过?

#include
using namespace std;
#define int long long
#define maxn 100000+500
#define inf n+1
int L[maxn*4],R[maxn*4],V[maxn*4];
int A[maxn],B[maxn];
inline int read()
{
    char ch=getchar();
    int ans=0;
    while(ch<'0'||ch>'9')ch=getchar();
    while(ch>='0'&&ch<='9')
    {
        ans=ans*10+ch-'0';
        ch=getchar();
    }
    return ans;
}

int n,m,La;
void build(int l,int r,int x)
{

    if(l==r)
    {
        V[x]=A[l];
        return;
    }
    int mid=(l+r)/2;
    build(l,mid,2*x);
    build(mid+1,r,2*x+1);
    V[x]=max(V[2*x],V[2*x+1]);
    return;
}
void update(int x,int l,int r,int d,int w)
{
    if(l==r)
    {
        V[x]=w;
        return;
    }
    int mid=(l+r)/2;
    if(d<=mid)update(2*x,l,mid,d,w);
    else update(2*x+1,mid+1,r,d,w);
    V[x]=max(V[2*x],V[2*x+1]);
}

bool query(int x,int l,int r,int b,int c)
{
  //cout<

    if(l==r&&V[x]>b&&c<=l)
    {
        cout<'\n';
        La=l;
        return true;
    }
    else if(l==r)return false;
    int mid=(l+r)/2;
    if(c<=mid&&V[2*x]>b)
        if(query(2*x,l,mid,b,c))return true;
    if(c<=r&&V[2*x+1]>b)
        if(query(2*x+1,mid+1,r,b,c))return true;
    return false;

}
signed main()
{
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T,a,b,c;
    T=read();
    while(T--)
    {
        // init();
        n=read(),m=read();
        for(int i=1; i<=n; i++)
        {
            a=read();
            A[a]=i;
            B[i]=a;
        }
        //A[n+1]=n+1;
        build(1,n,1);
        La=0;
        for(int i=1; i<=m; i++)
        {
            a=read();
            if(a==1)
            {
                b=read();
                b^=La;
                update(1,1,n,B[b],n+1);
            }
            else
            {
                b=read();
                c=read();
                //cout<
                b^=La;
                c^=La;
                if(!query(1,1,n,b,c))
                {
                    cout<1<<'\n';
                    La=(n+1);
                }
            }
        }
    }



}

 

转载于:https://www.cnblogs.com/liulex/p/11404036.html

你可能感兴趣的:(数据结构与算法)