Description in 2050, the robot war broke out, clever Doraemon to help Nobita win this war, from his pocket to take out the robot game, every game can become a warrior, Doraemon decided to give them a whole team, Doraemon found the first position of the warrior number for Ai (obviously a is a permutation). After the calculation, Doraemon found that the first position of the warrior number for Bi, his army can play the maximum combat effectiveness (guaranteed B is also a permutation).
Doraemon can give orders to change the order of the soldiers, and every time, he will quote an integer I (1≤i<n). If the warrior number in position I is greater than the first i+1, then these two warriors will be exchanged in order, otherwise this command will be ignored.
Now Doraemon wants his army to play the most powerful battle, and he wants to know if there is a sequence of instructions, so that the soldiers can be arranged in the way of arranging B.
But because the number of fighters is too much, Doraemon did not see the answer for a time. So he used the time machine to bring you--the most famous folk scientist of the 21st century--to help him figure out the answer to the question. The first line of input data contains a positive integer n (n<=100000).
The next two lines have n positive integers per row, describing arrangement A and permutation B, respectively. Output for each set of data, if there is such a sequence of instructions, outputs "YES", otherwise the output "no" (quotation marks do not output, please note the case). Sample Input
32 3 12 1 332 1 33 1 2
Sample Output
YESNO
Idea: This problem is Yamashina the third school game of H, our team had the last problem, because too weak, think about about one hours before the initial thinking
is to build a line tree of Max, and the arrays A and B record the subscript of the value before and after the interchange, respectively.
Then, from the big to the small loop, each time you determine whether the value is moved to the right (or not moved), if it is established, ask the initial position of the line tree to the right of the element
The position of the leftmost end where the value is moved. The position after the current value is moved must be less than the result of the query.
Each value then updates the segment tree to update the value of the initial position to the position after the interchange.
Then the same reason from small to large cycle, each judge left to move, ask the right side of the location is good
/************************************************author:d evilcreated time:2016/4/26 13:11:52***************** ******************************* */#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<stdlib.h>using namespacestd;Const intn=100005;intn,a[n],b[n],tree[n<<2];voidBuild1 (intNodeintLintR) { if(l==R) {Tree[node]=N; return ; } intM= (l+r) >>1; Build1 (Node<<1, l,m); Build1 (Node<<1|1, m+1, R); Tree[node]=min (tree[node<<1],tree[node<<1|1]);}voidUpdate1 (intNodeintLintRintp) { if(l==R) {Tree[node]=B[p]; return ; } intM= (l+r) >>1; if(a[p]<=m) update1 (node<<1, l,m,p); ElseUpdate1 (node<<1|1, m+1, r,p); Tree[node]=min (tree[node<<1],tree[node<<1|1]);}intQuery1 (intNodeintLintRintp) { if(l>=p)returnTree[node]; intM= (l+r) >>1, ans; Ans=query1 (node<<1|1, m+1, r,p); if(p<=m) Ans=min (Ans,query1 (node<<1, l,m,p)); returnans;}voidBuild2 (intNodeintLintR) { if(l==R) {Tree[node]=0; return ; } intM= (l+r) >>1; BUILD2 (Node<<1, l,m); BUILD2 (Node<<1|1, m+1, R); Tree[node]=max (tree[node<<1],tree[node<<1|1]);}voidUpdate2 (intNodeintLintRintp) { if(l==R) {Tree[node]=B[p]; return ; } intM= (l+r) >>1; if(a[p]<=m) Update2 (node<<1, l,m,p); ElseUpdate2 (node<<1|1, m+1, r,p); Tree[node]=max (tree[node<<1],tree[node<<1|1]);}intQuery2 (intNodeintLintRintp) { if(r<=p)returnTree[node]; intM= (l+r) >>1, ans; Ans=query2 (node<<1, l,m,p); if(p>m) Ans=max (Ans,query2 (node<<1|1, m+1, r,p)); returnans;}intMain () {//freopen ("In.txt", "R", stdin); while(~SCANF ("%d",&N)) {intx,flag=1; for(intI=1; i<=n; i++) {scanf ("%d",&x); A[X]=i; } for(intI=1; i<=n; i++) {scanf ("%d",&x); B[X]=i; } build1 (1,1, N); for(intI=n; I>0; i--) { if(A[i]<=b[i]&&b[i]>query1 (1,1, N,a[i])) {Flag=0; Break; } update1 (1,1, N,i); } if(!flag) {printf ("no\n"); Continue; } build2 (1,1, N); for(intI=1; i<=n; i++) { if(A[i]>=b[i]&&b[i]<query2 (1,1, N,a[i])) {Flag=0; Break; } update2 (1,1, N,i); } if(!flag) printf ("no\n"); Elseprintf"yes\n"); } return 0;}
Sdustoj 1796 Doraemon Army (line segment tree maintenance prefix position)