1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| #include <bits/stdc++.h> #define ld double #define ull unsigned long long #define ll long long #define pii pair <int, int> #define iiii pair <int, pii > #define mp make_pair #define INF 1000000000 #define rep(i, x) for(int (i) = 0; (i) < (x); (i)++) inline int getint() { int x = 0, p = 1; char c = getchar(); while (c <= 32) c = getchar(); if (c == 45) p = -p, c = getchar(); while (c > 32) x = x * 10 + c - 48, c = getchar(); return x * p; } using namespace std; const int mod = 1e9 + 7; inline void reduce(int &x) { x += x >> 31 & mod; } inline int mul(int x, int y) { return 1ll * x * y % mod; } //ruogu_alter const int N = 7e5 + 5; const int M = 5e5 + 5; const int Q = 2e5 + 5; int dat[N << 2][10], n, len[N], q, a[10][M]; pii pos[N]; int qx[Q], qy[Q], qz[Q], op[Q], tmp[10]; vector<int> id; // void up(int k) { rep(i, n) { dat[k][i] = min(dat[2 * k + 1][i], dat[2 * k + 2][i]); } } void build(int l, int r, int k) { if (r - l == 1) { rep(i, n) dat[k][i] = len[i]; return; } int mid = (l + r) >> 1; build(l, mid, 2 * k + 1); build(mid, r, 2 * k + 2); up(k); } void modify(int l, int r, int p, int k, int x, int v) { if (r - l == 1) { dat[k][x] = v; return; } int mid = (l + r) >> 1; if (p < mid) modify(l, mid, p, 2 * k + 1, x, v); else modify(mid, r, p, 2 * k + 2, x, v); dat[k][x] = min(dat[2 * k + 1][x], dat[2 * k + 2][x]); } int qry(int l, int r, int k, int p) { if (r - l == 1) { rep(i, n) tmp[i] = min(tmp[i], dat[k][i]); return l; } int mid = (l + r) >> 1, sum = 0; rep(i, n) sum += min(tmp[i], dat[2 * k + 2][i]); if (sum > p) { rep(i, n) tmp[i] = min(tmp[i], dat[2 * k + 2][i]); return qry(l, mid, 2 * k + 1, p); } else return qry(mid, r, 2 * k + 2, p); } int main() { n = getint(); q = getint(); rep(i, n) { len[i] = getint(); rep(j, len[i]) { a[i][j] = getint(); id.emplace_back(a[i][j]); } } rep(i, q) { op[i] = getint(); qx[i] = getint() - 1; if (op[i] == 1) { qy[i] = getint() - 1; qz[i] = getint(); id.emplace_back(qz[i]); } } sort(id.begin(), id.end()); id.resize(unique(id.begin(), id.end()) - id.begin()); rep(i, q) if (op[i] == 1) { qz[i] = lower_bound(id.begin(), id.end(), qz[i]) - id.begin(); } int m = id.size(); build(0, m, 0); rep(i, m) pos[i] = mp(-1, -1); rep(i, n) rep(j, len[i]) { a[i][j] = lower_bound(id.begin(), id.end(), a[i][j]) - id.begin(); assert(pos[a[i][j]] == mp(-1, -1)); pos[a[i][j]] = mp(i, j); modify(0, m, a[i][j], 0, i, j); } rep(i, q) { if (op[i] == 1) { modify(0, m, a[qx[i]][qy[i]], 0, qx[i], len[qx[i]]); pos[a[qx[i]][qy[i]]] = mp(-1, -1); assert(pos[qz[i]] == mp(-1, -1)); a[qx[i]][qy[i]] = qz[i]; pos[qz[i]] = mp(qx[i], qy[i]); modify(0, m, a[qx[i]][qy[i]], 0, qx[i], qy[i]); } else { rep(i, n) tmp[i] = len[i]; int k = qx[i], sum = 0, h; pii res = pos[h = qry(0, m, 0, k)]; rep(i, n) sum += tmp[i]; assert(sum <= k); printf("%d\n", id[a[res.first][k - sum + res.second]]); } } return 0; }
|