有\(n\)个宝宝一字排开,每个宝宝衣服前后有一个数字,第i个宝宝的衣服上,正面的数为\(a_i\),反面的数为\(b_i\)。
有些宝宝比较淘气,他会跟其他人换位置。
共有\(m\)次换位置,第\(i\)次,位置\(c_i\)和\(d_i\)位置上的宝宝交换位置。
每次宝宝交换位置之后,你都需要判断,通过翻转任意多个宝宝(向后转,背面向前,但不能改变他们的位置),能否让一排宝宝身上的数从左到右单调不降。
\(30\%\)的数据 ,\(n,m\le 2000\)
\(100\%\)的数据 ,\(n\le 200000,m\le 1000000,0\le a_i ,b_i\le 10000000,1\le c_i,d_i\le n\).
太恶心了,puts胜出?!
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int inf = 1000000001;
int n, m, u, v;
struct Node {
int g, d;
} a[N];
struct Tree {
int l, r, mx, mn;
} tr[N << 2];
void pushup(int rt) {
tr[rt].mx = tr[rt].mn = inf; //如果下面的满足则会更改成不是inf
int w = tr[rt << 1 | 1].l;
// cout<<"w: "<<w<<endl;
if (tr[rt << 1].mx <= a[w].g)
tr[rt].mx = min(tr[rt].mx, tr[rt << 1 | 1].mx);
if (tr[rt << 1].mx <= a[w].d)
tr[rt].mx = min(tr[rt].mx, tr[rt << 1 | 1].mn);
if (tr[rt << 1].mn <= a[w].g)
tr[rt].mn = min(tr[rt].mn, tr[rt << 1 | 1].mx);
if (tr[rt << 1].mn <= a[w].d)
tr[rt].mn = min(tr[rt].mn, tr[rt << 1 | 1].mn);
}
void build(int rt, int l, int r) {
// tr[rt]= (Tree){l,r,inf,inf};
tr[rt].l = l, tr[rt].r = r;
if (l == r) {
tr[rt].mx = a[l].g;
tr[rt].mn = a[l].d;
return;
}
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
pushup(rt);
}
void update(int rt, int l, int r, int x) {
if (l == r) {
tr[rt].mx = a[x].g, tr[rt].mn = a[x].d;
return;
}
int mid = (l + r) >> 1;
if (x <= mid)
update(rt << 1, l, mid, x);
else
update(rt << 1 | 1, mid + 1, r, x);
pushup(rt);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].g >> a[i].d;
if (a[i].d > a[i].g)
swap(a[i].d, a[i].g);
}
build(1, 1, n);
// cout<<tr[1].mn<<" "<<tr[1].mx<<endl;
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> u >> v;
swap(a[u], a[v]);
update(1, 1, n, u);
update(1, 1, n, v);
if (tr[1].mx != inf || tr[1].mn != inf)
puts("1");
else
puts("0");
}
return 0;
}
/*
2
1 4
3 5
out:3 5
*/