Manipulate Dwarfs
In a small village beyond seven hills and seven seas, Snow White lives together with N dwarves who spend all their time eating and playing League of Legends. Snow White wants to put an end to this, so she has organized gym classes for them.
At the beginning of each class, the dwarves must stand in line, ordered by their height. For the purposes of this task, assume that the dwarves have heights 1, 2, ..., N (each exactly once) and initially all are standing in sorted order with height from 1 to N. Now Snow White play on them by issuing commands of the form:
• 1 X Y -- dwarves with height X and Y in the line must switch places. She also checks their ordering by issuing queries of the form:
• 2 A B -- do dwarves with heights A, A+1, ..., B (not necessarily in that order) occupy a contiguous subsequence of the current line? Help the doofus dwarves follow Snow White's instructions and respond to her queries.
INPUT
The first line of input contains the two positive integers N and M, the number of dwarves and the number of Snow White's requests, respectively (2 ≤ N ≤ 200 000, 2 ≤ M ≤ 200 000). Each of the following M lines contains a single Snow White's request, of the form "1 X Y" (1 ≤ X, Y ≤ N, X ≠ Y) or “2 A B” (1 ≤ A ≤ B ≤ N), as described in the problem statement.
OUTPUT
The output must contain one line for each request of type 2, containing the reply to the query, either “YES” or “NO”.
Example:
Input :
4 5
2 2 3
2 2 4
1 1 3
2 3 4
1 4 3
Output :
YES
YES
NO
一个1~n的数列,一开始从1到n排好,
支持两个操作,交换数列中两个数a和b的位置,询问数字a,a+1,...b是不是在数列中连续的一段位置
一个很裸的线段树,记一下每个数在里面的位置,第二个查询只要问这段区间的最大值和最小值是不是b和a,区间长度对不对
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdlib> 5 #include<algorithm> 6 #include<cmath> 7 #include<queue> 8 #include<deque> 9 #include<set> 10 #include<map> 11 #include<ctime> 12 #define LL long long 13 #define inf 0x7ffffff 14 #define pa pair<int,int> 15 #define mkp(a,b) make_pair(a,b) 16 #define pi 3.1415926535897932384626433832795028841971 17 using namespace std; 18 inline LL read() 19 { 20 LL x=0,f=1;char ch=getchar(); 21 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 22 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 23 return x*f; 24 } 25 struct segtree{ 26 int l,r,mx,mn; 27 }t[800010]; 28 int pos[200010]; 29 int n,m; 30 inline void update(int k) 31 { 32 t[k].mx=max(t[k<<1].mx,t[k<<1|1].mx); 33 t[k].mn=min(t[k<<1].mn,t[k<<1|1].mn); 34 } 35 inline void buildtree(int now,int l,int r) 36 { 37 t[now].l=l;t[now].r=r; 38 if (l==r) 39 { 40 t[now].mx=t[now].mn=l; 41 return; 42 } 43 int mid=(l+r)>>1; 44 buildtree(now<<1,l,mid); 45 buildtree(now<<1|1,mid+1,r); 46 update(now); 47 } 48 inline void change(int now,int x,int d) 49 { 50 int l=t[now].l,r=t[now].r; 51 if (l==r) 52 { 53 t[now].mx=t[now].mn=d; 54 return; 55 } 56 int mid=(l+r)>>1; 57 if (x<=mid)change(now<<1,x,d); 58 else change(now<<1|1,x,d); 59 update(now); 60 } 61 inline int askmx(int now,int x,int y) 62 { 63 int l=t[now].l,r=t[now].r; 64 if (l==x&&r==y)return t[now].mx; 65 int mid=(l+r)>>1; 66 if(y<=mid)return askmx(now<<1,x,y); 67 else if (x>mid)return askmx(now<<1|1,x,y); 68 else return max(askmx(now<<1,x,mid),askmx(now<<1|1,mid+1,y)); 69 } 70 inline int askmn(int now,int x,int y) 71 { 72 int l=t[now].l,r=t[now].r; 73 if (l==x&&r==y)return t[now].mn; 74 int mid=(l+r)>>1; 75 if(y<=mid)return askmn(now<<1,x,y); 76 else if (x>mid)return askmn(now<<1|1,x,y); 77 else return min(askmn(now<<1,x,mid),askmn(now<<1|1,mid+1,y)); 78 } 79 int main() 80 { 81 n=read();m=read(); 82 buildtree(1,1,n); 83 for (int i=1;i<=n;i++)pos[i]=i; 84 for (int i=1;i<=m;i++) 85 { 86 int op=read(),l=read(),r=read(); 87 if (op==1) 88 { 89 swap(pos[l],pos[r]); 90 change(1,pos[l],l); 91 change(1,pos[r],r); 92 }else 93 { 94 if (l>r)swap(l,r); 95 int ll=pos[l],rr=pos[r]; 96 if (ll>rr)swap(ll,rr); 97 if (askmx(1,ll,rr)==r&&askmn(1,ll,rr)==l&&rr-ll+1==r-l+1)puts("YES");else puts("NO"); 98 } 99 } 100 }