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

题意:给你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"); }
}
}
上一篇:[CSS] Manipulate Images Using CSS Filter and Blend Modes


下一篇:How to manipulate pixels on a bitmap by scanline property(Ma Xiaoguang and Ma Xiaoming)