Codeforces 1187D:Subarray Sorting

Subarray Sorting

You are given an array a1,a2,…,an and an array b1,b2,…,bn.
For one operation you can sort in non-decreasing order any subarray a[l…r] of the array aa.

For example, if a=[4,2,2,1,3,1] and you choose subbarray a[2…5], then the array turns into [4,1,2,2,3,1].

You are asked to determine whether it is possible to obtain the array b by applying this operation any number of times (possibly zero) to the array aa.

Input
The first line contains one integer t (1≤t≤3⋅105) — the number of queries.

The first line of each query contains one integer n (1≤n≤3⋅10^5).

The second line of each query contains n integers a1,a2,…,an (1≤ai≤n).

The third line of each query contains n integers b1,b2,…,bn (1≤bi≤n).

It is guaranteed that ∑n≤3⋅105 over all queries in a test.

Output
For each query print YES (in any letter case) if it is possible to obtain an array b and NO (in any letter case) otherwise.

Example

Input
4
7
1 7 1 4 4 5 6
1 1 4 4 5 7 6
5
1 1 3 3 5
1 1 3 3 5
2
1 1
1 2
3
1 2 3
3 2 1

Output
YES
YES
NO
NO

题意:就是让你只能通过对a数组的任意区间进行非递减排序,看能不能变成b数组。

思路:线段树问题,对数组a进行建树,然后对b数组遍历,从a数组中找到第一个与b数组值相等的下标k(因为数组比较大,不可能用两个for循环去找,所以要用队列来储存下标。然后只需到对应的队列去取值就行,如果为空,则说明a数组的值不全等于数组,直接break。),如果在区间<1,k>中数组a的最小值小于b当前的值,那么你无论如何变换都无法实现。如果大于或等于,那么就说明当前值为区间最小值,则可以通过变换将其换到b数组对应的位置,再将权值设为inf。

代码如下

#include <bits/stdc++.h>
#define inf 999999

using namespace std;

const int maxn = 3e5+5;
int num[maxn],num2[maxn],Q,n;
queue<int>q[maxn];
int G[maxn*4];

void buildtree(int l,int r,int t)
{
   
    if(l==r){
   
        G[t] = num[l];
        return ;
    }
    int mid  = (int)(l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值