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
Spoj-DWARFLOG Manipulate Dwarfs的更多相关文章
- SPOJ - DWARFLOG Manipulate Dwarfs 线段树+想法题;
题意:给你2e5个矮人,编号1~N.有2e5个操作:操作1 读取x,y,交换编号为x,y的矮人.操作2 读取AB 判断编号为A,A+1····B的矮人是否连续(不必有序). 题解:首先用pos[i]保 ...
- 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 ...
- SPOJ DQUERY D-query(主席树)
题目 Source http://www.spoj.com/problems/DQUERY/en/ Description Given a sequence of n numbers a1, a2, ...
- 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 ...
- 【填坑向】spoj COT/bzoj2588 Count on a tree
这题是学主席树的时候就想写的,,, 但是当时没写(懒) 现在来填坑 = =日常调半天lca(考虑以后背板) 主席树还是蛮好写的,但是代码出现重复,不太好,导致调试的时候心里没底(虽然事实证明主席树部分 ...
- SPOJ bsubstr
题目大意:给你一个长度为n的字符串,求出所有不同长度的字符串出现的最大次数. n<=250000 如:abaaa 输出: 4 2 1 1 1 spoj上的时限卡的太严,必须使用O(N)的算法那才 ...
- 【SPOJ 7258】Lexicographical Substring Search
http://www.spoj.com/problems/SUBLEX/ 好难啊. 建出后缀自动机,然后在后缀自动机的每个状态上记录通过这个状态能走到的不同子串的数量.该状态能走到的所有状态的f值的和 ...
- 【SPOJ 1812】Longest Common Substring II
http://www.spoj.com/problems/LCS2/ 这道题想了好久. 做法是对第一个串建后缀自动机,然后用后面的串去匹配它,并在走过的状态上记录走到这个状态时的最长距离.每匹配完一个 ...
- 【SPOJ 8222】Substrings
http://www.spoj.com/problems/NSUBSTR/ clj课件里的例题 用结构体+指针写完模板后发现要访问所有的节点,改成数组会更方便些..于是改成了数组... 这道题重点是求 ...
随机推荐
- UVA1515 Pool construction (最小割模型)
如果不允许转化'#'和'.'的话,那么可以直接在'#'和'.'之间连容量为b的边,把所有'#'和一个源点连接, 所有'.'和一个汇点连接,流量不限,那么割就是建围栏(分割'#'和'.')的花费. 问题 ...
- shell脚本自动部署及监控
一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...
- selenium-介绍和安装
前戏 相信大家对web自动化selenium都不陌生,是一个web自动化框架,我在第一家公司的时候,产品是两个星期一个版本,每一次发布测试都要进行回归测试,也就是大家说的点点点,后来我就想,能不能做成 ...
- Bootstrap历练实例:表单控件状态(禁用)
禁用的输入框 input 如果您想要禁用一个输入框 input,只需要简单地添加 disabled 属性,这不仅会禁用输入框,还会改变输入框的样式以及当鼠标的指针悬停在元素上时鼠标指针的样式. < ...
- javase(8)_集合框架_List、Set、Map
一.集合体系(不包括Queue体系) 二.ArrayList ArrayList的属性 private transient Object[] elementData; //存储元素 private i ...
- javaEE(15)_Servlet过滤器
一.Filter简介 1.Filter也称之为过滤器,它是Servlet技术中最激动人心的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, ...
- JS设置组合快捷键
为提升用户体验,想要在web页面中通过组合快捷键调出用户帮助页面,具体实现思路是监听keyup事件,在相应的处理函数中进行逻辑编写,代码如下 $(document).keyup(function (e ...
- CF-1110 (2019/02/08)
CF-1110 A. Parity 快速幂的思想,考虑最后一位即可 #include <bits/stdc++.h> using namespace std; typedef long l ...
- Bzoj 1088: [SCOI2005]扫雷Mine (DP)
Bzoj 1088: [SCOI2005]扫雷Mine 怒写一发,算不上DP的游戏题 知道了前\(i-1\)项,第\(i\)项会被第二列的第\(i-1\)得知 设\(f[i]\)为第一列的第\(i\) ...
- Flux reference
https://facebook.github.io/flux/docs/dispatcher.html#content 首先安装 npm install --save flux Dispatcher ...