CF1093E Intersection of Permutations 题解
经典动态二维数点。
考虑令 $f_{a_i}=i$,则询问 $l_a\ r_a\ l_b\ r_b$ 相当于询问 $l_a\le f_{b_i}\le r_a,l_b\le i\le r_b$ 的 $i$ 的个数。
树套树维护 $f_{b_i}$,交换相当于单点修改。
平板电视真的很好写。
#include <cstdio>
#include <algorithm>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
using namespace std;
using namespace __gnu_pbds;
char buf[1 << 23], *p1 = buf, *p2 = buf, obuf[1 << 23], *O = obuf;
inline int R()
{
int r = 0;
char c = getchar();
while (c < '0' || c > '9')
c = getchar();
while (c >= '0' && c <= '9')
r = r * 10 + c - '0', c = getchar();
return r;
}
void P(int x)
{
if (x >= 10)
P(x / 10);
*O++ = x % 10 + '0';
}
int n, m, a[200001], b[200001], k[200001];
struct T
{
tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> c;
inline void C(int x, int k)
{
if (k == 1)
c.insert(x);
else
c.erase(x);
}
inline int Q(int x, int y) { return c.order_of_key(y + 1) - c.order_of_key(x); }
} c[200001];
inline void C(int x, int y, int k)
{
for (; x <= n; x += x & -x)
c[x].C(y, k);
}
inline int Q(int x, int y, int s, int t)
{
int q = 0;
for (--x; y > x; y &= y - 1)
q += c[y].Q(s, t);
for (; x > y; x &= x - 1)
q -= c[x].Q(s, t);
return q;
}
int main()
{
n = R();
m = R();
for (int i = 1; i <= n; ++i)
k[a[i] = R()] = i;
for (int i = 1; i <= n; ++i)
C(i, k[b[i] = R()], 1);
for (int i = 0, o, x, y, p, q; i < m; ++i)
{
o = R();
x = R();
y = R();
if (o & 1)
p = R(), q = R(), P(Q(p, q, x, y)), *O++ = '\n';
else
C(x, k[b[x]], -1), C(y, k[b[y]], -1), swap(b[x], b[y]), C(x, k[b[x]], 1), C(y, k[b[y]], 1);
}
fwrite(obuf, O - obuf, 1, stdout);
return 0;
}