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

Spoj-DWARFLOG Manipulate Dwarfs的更多相关文章

  1. SPOJ - DWARFLOG Manipulate Dwarfs 线段树+想法题;

    题意:给你2e5个矮人,编号1~N.有2e5个操作:操作1 读取x,y,交换编号为x,y的矮人.操作2 读取AB 判断编号为A,A+1····B的矮人是否连续(不必有序). 题解:首先用pos[i]保 ...

  2. BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]

    2588: Spoj 10628. Count on a tree Time Limit: 12 Sec  Memory Limit: 128 MBSubmit: 5217  Solved: 1233 ...

  3. SPOJ DQUERY D-query(主席树)

    题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...

  4. SPOJ GSS3 Can you answer these queries III[线段树]

    SPOJ - GSS3 Can you answer these queries III Description You are given a sequence A of N (N <= 50 ...

  5. 【填坑向】spoj COT/bzoj2588 Count on a tree

    这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...

  6. SPOJ bsubstr

    题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...

  7. 【SPOJ 7258】Lexicographical Substring Search

    http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...

  8. 【SPOJ 1812】Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...

  9. 【SPOJ 8222】Substrings

    http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...

随机推荐

  1. 将从SQL2008 r2里备份的数据库还原到SQL2008中

    从标题可以看出这是未解决上一篇遗留问题写的,现在我也不知道这个可不可以成功,方法似乎查到了一种,具体怎样还不清楚:而且,我想说的是“我踩雷了”. 这篇的主角是“Database Publishing ...

  2. IOS7 Text View 截断的问题解决

    - (void)textViewDidChange:(UITextView *)textView { CGRect line = [textView caretRectForPosition: tex ...

  3. Python 中函数(Function)的用法

    函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.Python提供了许多内建函数,比如print().input(),也可以自己创建函数, ...

  4. python基础一 day13 迭代器

    # 双下方法# print([1].__add__([2]))# print([1]+[2]) # 迭代器# l = [1,2,3]# 索引# 循环 for# for i in l:# i## for ...

  5. Javascript根据指定下标或对象删除数组元素

    删除数组元素在工作中经常会用到,本文讲解一下Javascript根据下标删除数组元素的方法,需要了解的朋友可以参考下 将一下代码放在全局js文件中: Js代码 /** *删除数组指定下标或指定对象 * ...

  6. java script DOM BOM

    onclick        当用户点击某个对象时调用的事件句柄.ondblclick     当用户双击某个对象时调用的事件句柄. onfocus        元素获得焦点.            ...

  7. Bootstrap历练实例:导航中的表单

    Bootstrap历练实例:导航中的表单,它是使用class.navbar-form类,这确保了表单适当的垂直对齐和在较窄的视口中折叠的行为,使用这个对齐方式选项来决定导航栏中的内容放置在哪里. 实例 ...

  8. rz

    Linux系统简单易用的上传下载命令rz和sz sudo yum install lrzsz -y 上传:rz 下载:sz

  9. vue ssr

    https://mp.weixin.qq.com/s/v1c69bJ5PxGcqt-ZU4FVXw https://juejin.im/entry/590ca74b2f301e006c10465f h ...

  10. 【php】 检测 ie ie11 edge浏览器

    来源 php.net 官网评论截取 -- Declan kelly Please note that Internet Explorer 11 no longer contains MSIE in i ...