Tree line on the reverse (partial order) - cf1187D good question!

/ * 
Exclude all possible cases, the rest is feasible 
number is not the same number 1. 
2. a range for any sort, any equivalent to the reverse order may be exchanged, 
    then from 1 to b n scanning array, determining whether the position where the value of a may be equal to the array b [i] j is switched to the position i, is equivalent to a judgment interval [i, j] if there are several <b [i], and this number is not judged after will be needed again, it is changed to infinity 
    similar problem for a class partial ordering can be established segment tree to maintain the minimum dynamic range 
 * / 
#include <bits / STDC ++ H.> 
#include <Queue>
 the using  namespace STD;
 #define MAXN 300005
 #define INF 0x3f3f3f3f int A [MAXN], B [MAXN], TA [MAXN], TB [MAXN], n-; #define LSON L, m, RT <<. 1
 #define rson. 1 + m, R & lt, RT < <. 1 |. 1
 int Min [MAXN << 2 ];
 void a pushup ( int 
    Min [RT]
 


 RT) {=min(Min[rt<<1],Min[rt<<1|1]);
}
void build(int l,int r,int rt){
    if(l==r){
        Min[rt]=a[l];
        return;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int pos,int val,int l,int r,int rt){
    if(l==r){
        Min[rt]=val;
        return;
    }
    int m=l+r>>1;
    if(pos<=m)update(pos,val,lson);
    else update(pos,val,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l && R>=r)return Min[rt];
    int m=l+r>>1,res=0x3f3f3f3f;
    if(L<=m)res=min(res,query(L,R,lson));
    if(R>m)res=min(res,query(L,R,rson));
    return res;
}

queue<int>q[maxn];

int main(){
    int t;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i],ta[i]=a[i];
            while(q[a[i]].size())q[a[i]].pop();
        }
        for(int i=1;i<=n;i++)cin>>b[i],tb[i]=b[i];
        sort(ta+1,ta+1+n);sort(tb,tb+1+n);
        int flag=0;
        for(int i=1;i<=n;i++)
            if(ta[i]!=tb[i])flag=1;
        if(!flag){
            build(1,n,1);
            for(int i=1;i<=n;i++)
                q[a[i]].push(i);
            for(int i=1;i<=n;i++){
                int pos=q[b[i]].front();
                q[b[i]].pop();
                int tmp=query(1,pos,1,n,1);
                update(pos,inf,1,n,1);
                if(tmp<b[i])flag=1; 
            }
        }
        if(flag)puts("NO");
        else puts("YES");
    }
} 

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11115015.html