#include #include #include "testlib.h" using namespace std; template struct SegTree { const T INF = numeric_limits::max(); int n, m; vector> dat; SegTree(int n_, int m_) : dat(n_ * 4, vector(m_, 0)) { int x = 1; while (n_ > x) { x *= 2; } n = x; m = m_; } void init() { for (int i = 0; i < 2 * n - 1; i++) { for (int j = 0; j < m; j++) { dat[i][j] = j; } } } void update(int i, vector x) { i += n - 1; for (int j = 0; j < m; j++) dat[i][j] = x[j]; while (i > 0) { i = (i - 1) / 2; for (int j = 0; j < m; j++) dat[i][j] = dat[i * 2 + 2][dat[i * 2 + 1][j]]; } } vector query(int a, int b) { return query_sub(a, b, 0, 0, n); } vector query_sub(int a, int b, int k, int l, int r) { vector x(m); if (r <= a || b <= l) { for (int i = 0; i < m; i++) x[i] = i; } else if (a <= l && r <= b) { for (int i = 0; i < m; i++) x[i] = dat[k][i]; } else { vector vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2); vector vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r); for (int i = 0; i < m; i++) x[i] = vr[vl[i]]; } return x; } }; int main() { registerValidation(); long long N = inf.readLong((long long)2, (long long)18, "N"); inf.readSpace(); long long M = inf.readLong((long long)2, (long long)10000, "M"); inf.readSpace(); long long Q = inf.readLong((long long)2, (long long)50000, "Q"); inf.readEoln(); SegTree Seg(M, N); Seg.init(); for (int i = 0; i < Q; i++) { int com = inf.readInt(1, 3); if (com == 1) { long long B = inf.readLong((long long)1, M, "B"); vector P(N); for (int j = 0; j < N; j++) { inf.readSpace(); P[j] = inf.readLong((long long)1, N, "P"); } for (int j = 0; j < N; j++) P[j]--; vector flag(N, false); for (int j = 0; j < N; j++) { if (flag[P[j]]) cout << -1 << endl; else flag[P[j]] = true; } Seg.update(B - 1, P); } if (com == 2) { inf.readSpace(); long long S = inf.readLong((long long)1, M, "S"); auto x = Seg.query(0, S); vector y(N); for (int j = 0; j < N; j++) y[x[j]] = j; cout << y[0] + 1; for (int j = 1; j < N; j++) cout << " " << y[j] + 1; cout << endl; } if (com == 3) { inf.readSpace(); long long a = inf.readLong((long long)1, M, "L"); inf.readSpace(); long long b = inf.readLong(a, M, "R"); auto x = Seg.query(a - 1, b); long long count = 0; for (int j = 0; j < N; j++) count += abs(x[j] - j); cout << count << endl; } inf.readEoln(); } inf.readEof(); }