题意:给你2e5个矮人,编号1~N。有2e5个操作:操作1 读取x,y,交换编号为x,y的矮人。操作2 读取AB 判断编号为A,A+1····B的矮人是否连续(不必有序)。

题解:首先用pos[i]保存矮人i的位置,交换就用swap(pos[l],pos[r])来模拟。然后发现条件等价于(pos[l],pos[r])的区间满足最大值为r,最小值为l且区间内人数等于r-l+1即可。所以直接维护区间最大最小值。用change(1,p,x)来更新p处的矮人编号,并pushup。

坑:最开始ask忘写return。想用一个pair<int,int> query 一次性返回大于小于号,会tle。最坑的是忘记判端pos[l]pos[r]的大小,导致RE。

ac代码:

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxnn = ;
int m, n, pos[];
pair<int, int> ans;
struct node {
int l, r, maxn, minn;
}tree[];
inline void push_up(int x) {
tree[x].maxn = max(tree[x << ].maxn, tree[x << | ].maxn);
tree[x].minn = min(tree[x << ].minn, tree[x << | ].minn);
}; inline void build(int x, int l, int r) {
tree[x].l = l; tree[x].r = r;
//tree[x].maxn = l; tree[x].minn = maxnn;
if (l == r) { tree[x].maxn = tree[x].minn = l; return; } int mid = (l+ r) >> ;
build(x << , l, mid);
build(x << | , mid + , r);
push_up(x); };
inline void change(int x, int p,int d) {
int L=tree[x].l,R=tree[x].r;
if (L == R) {
tree[x].maxn = tree[x].minn = d; return;
} int mid = (L + R) >> ;
if (p <= mid)change(x << ,p, d);
else change(x << | ,p, d);
push_up(x); } inline int askmx(int x,int l,int r) {
int L = tree[x].l, R = tree[x].r;
if (L==l&&r == R) return tree[x].maxn; int mid = (L + R) >> ;
if (r <= mid)return askmx(x << , l, r);
else if (l > mid)return askmx(x << | , l, r);
else return max(askmx(x << , l, mid), askmx(x << | , mid + , r));
}
inline int askmn(int x, int l, int r) {
int L = tree[x].l, R = tree[x].r;
if (L == l&&r == R) return tree[x].minn; int mid = L + R >> ;
if (r <= mid)return askmn(x << , l, r);
else if (l > mid)return askmn(x << | , l, r);
else return min(askmn(x << , l, mid), askmn(x << | , mid + , r)); }
int main() {
int n, q;
cin >> n >> q;
build(, , n);
for (int i = ; i <= n; i++)pos[i] = i; for (int i = ; i <= q; i++) {
int x; int l; int r;
scanf("%d", &x);
scanf("%d%d", &l, &r); if (x == ) {
swap(pos[l], pos[r]);
change(, pos[l], l);
change(, pos[r], r);
}
else { //ans= query(1, pos[l], pos[r]);
if (l > r)swap(l, r);
int rr = pos[r];
int ll = pos[l];
if (ll > rr)swap(ll, rr);
if (askmn(, ll, rr)==l&& askmx(, ll, rr)==r&&rr-ll == r-l)puts("YES");
else puts("NO"); }
}
}

SPOJ - DWARFLOG Manipulate Dwarfs 线段树+想法题;的更多相关文章

  1. ZJOI2017 day2 T2 线段树 想法题

    考完D2发现自己简直zz了...花式扔基本分 首先这道题有个显然的套路:树上一些点到一个定点的距离和=这些点深度和+点数*定点深度和-2*lca深度和 ——上一次见这个套路是LNOI2014,上次做的 ...

  2. hdu-1540线段树刷题

    title: hdu-1540线段树刷题 date: 2018-10-18 19:55:21 tags: acm 刷题 categories: ACM-线段树 概述 哇,,,这道线段树的题可以说是到目 ...

  3. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  4. POJ 3468 线段树裸题

    这些天一直在看线段树,因为临近期末,所以看得断断续续,弄得有些知识点没能理解得很透切,但我也知道不能钻牛角尖,所以配合着刷题来加深理解. 然后,这是线段树裸题,而且是最简单的区间增加与查询,我参考了A ...

  5. hdu-5023线段树刷题

    title: hdu-5023线段树刷题 date: 2018-10-18 13:32:13 tags: acm 刷题 categories: ACM-线段树 概述 这道题和上次做的那道染色问题一样, ...

  6. poj-2777线段树刷题

    title: poj-2777线段树刷题 date: 2018-10-16 20:01:07 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道线段树的染色问题,,, ...

  7. zoj-1610线段树刷题

    title: zoj-1610线段树刷题 date: 2018-10-16 16:49:47 tags: acm 刷题 categories: ACM-线段树 概述 这道题是一道简单的线段树区间染色问 ...

  8. Codeforces Round #393 (Div. 2) (8VC Venture Cup 2017 - Final Round Div. 2 Edition) E - Nikita and stack 线段树好题

    http://codeforces.com/contest/760/problem/E 题目大意:现在对栈有m个操作,但是顺序是乱的,现在每输入一个操作要求你输出当前的栈顶, 注意,已有操作要按它们的 ...

  9. hdu 1754 I Hate It 线段树基础题

    Problem Description 很多学校流行一种比较的习惯.老师们很喜欢询问,从某某到某某当中,分数最高的是多少. 这让很多学生很反感. 不管你喜不喜欢,现在需要你做的是,就是按照老师的要求, ...

随机推荐

  1. 树莓派(Raspberry Pi)USB无线网卡自动连接,二代B

    Raspberry Pi 使用USB无线网卡的时候不会因为路由重启而掉线. #!/bin/bash while true ; do if ifconfig wlan0 | grep -q " ...

  2. [AX]AX2012 R2 HR Jobs, Positions, Department和Workers

    部门.作业(Job的官方翻译)和位置(Position的官方翻译)是AX人力资源管理的基本组织元素,Job和Position在AX有的地方又称作工作和职位,其实这个翻译更为恰当. Job定义的是一个工 ...

  3. 个人成长|荣获CNVD年度最有价值漏洞奖

    本文共750+字,预计阅读2-3分钟. 前几天,很荣幸受主办方邀请,还拿了CNVD的一个“年度最有价值漏洞奖”,说一说,这几天的故事吧. 11月20号,意外收到一个会议邀请,当时还比较诧异,印象中我在 ...

  4. SpringBoot 常见问题记录

    问题一 Error starting ApplicationContext. To display the auto-configuration report re-run your applicat ...

  5. S3C6410裸奔之旅——RVDS2.2编译、仿真、调试过程 LED流水灯---转的

    S3C6410裸奔之旅——RVDS2.2编译.仿真.调试过程 LED流水灯 (2012-10-13 23:56:30) 转载▼ 标签: s3c6410裸奔 ok6410 rvds2.2 rvds2.2 ...

  6. List 集合的N层遍历

    package com.j1.cms.model; import java.io.Serializable; import java.util.List; /** * Created by wangc ...

  7. DataSet转化为实体类【转】

    分别转化单个类和集合两种方法. /// <summary> /// DataSet转换为实体类 /// </summary> /// <typeparam name=&q ...

  8. PHP curl登录 跳过验证码

    <?php switch($_GET['do']){ case 'vc': $cookieFile = "./test.tmp"; $url = 'http://localh ...

  9. Writing Reentrant and Thread-Safe Code(译:编写可重入和线程安全的代码)

    Writing Reentrant and Thread-Safe Code 编写可重入和线程安全的代码 (http://www.ualberta.ca/dept/chemeng/AIX-43/sha ...

  10. 日记整理---->2017-05-14

    学习一下知识吧,好久没有写博客了.如果他总为别人撑伞,你又何苦非为他等在雨中. 学习的知识内容 一.关于base64的图片问题 byte[] decode = Base64.base64ToByteA ...