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

最新文章

  1. Oracle 的字符集与乱码
  2. [VB.NET]调用API获取/设置键盘按键状态
  3. [Basic Information Theory] Writen Notes
  4. apache vhost
  5. GMF Q&amp;A(1): 如何让palette支持拖拽(DnD)等10则
  6. Android ActionBar Home按钮返回事件处理的两种方式
  7. Samba nsswitch/pam_winbind.c文件输入验证漏洞
  8. 一个赴美的应届毕业生Kevin,在美国做程序员的访谈
  9. APUE《UNIX 环境高级编程》读后感
  10. springBoot学习
  11. 怎样取json对应的值
  12. Subsequences Summing to Sevens
  13. Java多线程(二) —— 线程安全、线程同步、线程间通信(含面试题集)
  14. 树莓派3B+上运行.Net Core项目
  15. [CERC2017] Intrinsic Interval
  16. 依赖注入框架Ninject
  17. 实力封装:Unity打包AssetBundle(大结局)
  18. PAT 乙级 1042 字符统计(20) C++版
  19. Java进阶之路
  20. doc文件中的cer附件保存到本地

热门文章

  1. npm install -g cnpm --registry=https://registry.npm.taobao.org
  2. Python学习日志9月14日
  3. 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest C Sequence (打表)
  4. 一条SQL语句在MySQL中是如何执行的
  5. WPF知识点全攻略09- 附加属性
  6. 将回车键转换为Tab键
  7. UEditor练习(JSP版)
  8. [BZOJ2938]病毒 (AC自动机+dfs)
  9. maven项目创建(eclipse配置
  10. 企业自颁布服务器证书的有效性验证(C#为例)