Spoj-DWARFLOG Manipulate Dwarfs

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,区间长度对不对

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define mkp(a,b) make_pair(a,b)
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct segtree{
int l,r,mx,mn;
}t[];
int pos[];
int n,m;
inline void update(int k)
{
t[k].mx=max(t[k<<].mx,t[k<<|].mx);
t[k].mn=min(t[k<<].mn,t[k<<|].mn);
}
inline void buildtree(int now,int l,int r)
{
t[now].l=l;t[now].r=r;
if (l==r)
{
t[now].mx=t[now].mn=l;
return;
}
int mid=(l+r)>>;
buildtree(now<<,l,mid);
buildtree(now<<|,mid+,r);
update(now);
}
inline void change(int now,int x,int d)
{
int l=t[now].l,r=t[now].r;
if (l==r)
{
t[now].mx=t[now].mn=d;
return;
}
int mid=(l+r)>>;
if (x<=mid)change(now<<,x,d);
else change(now<<|,x,d);
update(now);
}
inline int askmx(int now,int x,int y)
{
int l=t[now].l,r=t[now].r;
if (l==x&&r==y)return t[now].mx;
int mid=(l+r)>>;
if(y<=mid)return askmx(now<<,x,y);
else if (x>mid)return askmx(now<<|,x,y);
else return max(askmx(now<<,x,mid),askmx(now<<|,mid+,y));
}
inline int askmn(int now,int x,int y)
{
int l=t[now].l,r=t[now].r;
if (l==x&&r==y)return t[now].mn;
int mid=(l+r)>>;
if(y<=mid)return askmn(now<<,x,y);
else if (x>mid)return askmn(now<<|,x,y);
else return min(askmn(now<<,x,mid),askmn(now<<|,mid+,y));
}
int main()
{
n=read();m=read();
buildtree(,,n);
for (int i=;i<=n;i++)pos[i]=i;
for (int i=;i<=m;i++)
{
int op=read(),l=read(),r=read();
if (op==)
{
swap(pos[l],pos[r]);
change(,pos[l],l);
change(,pos[r],r);
}else
{
if (l>r)swap(l,r);
int ll=pos[l],rr=pos[r];
if (ll>rr)swap(ll,rr);
if (askmx(,ll,rr)==r&&askmn(,ll,rr)==l&&rr-ll+==r-l+)puts("YES");else puts("NO");
}
}
}

Spoj DWARFLOG

上一篇:tcpdump 选项及过滤规则


下一篇:「CTS2019」氪金手游