The meaning of problems
description
input
analysis
Title, a total of two operations, one is the k-th digit +1000,0000, one is seeking a minimum, is not equal to ai (1 <= i <= r), and is not less than the number k.
Note that this number may not be present in the array
Because the number of different array, therefore, is not equal to ai (1 <= i <= r), is equivalent to this number may appear in the ai (r + 1 <= i <= n).
Why is it possible? Since the first operation because +1000,10000, that number will be making the first operation, in any case will not be repeated and ai (1000,0000 too much big than 51,0000).
Therefore, this problem, we can make the appropriate changes President tree, once for each ai [r + 1, n], no less than the minimum number k.
Because of this problem, array of 1-n of the n digits, so irrespective of the digital de-emphasis (UNIQUE), and the discretization problem.
In addition to the above two cases, there is a situation, we did not take into account, if k = n (XOR operation has been), then the answer is also possible that n + 1.
In summary, the answer is a total of three cases:
- ai [r + 1, n] of a number (which can be solved by the President of the tree)
- A first operation carried out a certain number (which will be digitally placed in a first set of operations)
- n + 1
Three figures, for the minimum to
Code
#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
int const maxn = 530000;
int const inf = 0x3f3f3f3f;
using namespace std;
int a[maxn], b[maxn];
int root[maxn << 5];//第几个版本的根节点编号
int lc[maxn << 5], rc[maxn << 5], sum[maxn << 5];
int sz;//节点个数
int n, m;
void build(int &rt, int l, int r) {
rt = ++sz;
if (l == r) return;
int mid = (l + r) >> 1;
build(lc[rt], l, mid);
build(rc[rt], mid + 1, r);
}
int update(int id, int l, int r, int pos) {
int _id = ++sz;
lc[_id] = lc[id], rc[_id] = rc[id], sum[_id] = sum[id] + 1;
if (l == r) return _id;
int mid = (r + l) >> 1;
if (pos <= mid)
lc[_id] = update(lc[id], l, mid, pos);
else
rc[_id] = update(rc[id], mid + 1, r, pos);
return _id;
}
//查询 不比k大的最小数字
int query(int p, int q, int l, int r, int k) {
if (l == r) return l;
int x1 = sum[lc[q]] - sum[lc[p]];
int x2 = sum[rc[q]] - sum[rc[p]];
int mid = (l + r) >> 1;
int ans = inf;
if (x1 > 0 && mid >= k)
ans = query(lc[p], lc[q], l, mid, k);
//这个if不能写为else,因为第一个if可能无法得到结果,返回inf
if(ans == inf && x2 > 0 && r >= k)
ans = query(rc[p], rc[q], mid + 1, r, k);
return ans;
}
int main() {
int T;
scanf("%d", &T);
while (T--) {
while (~scanf("%d %d", &n, &m)) {
sz = 0;
set<int> s;
int lastAns = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b + 1, b + n + 1);
build(root[0], 1, n);
for (int i = 1; i <= n; i++) {
int pos = lower_bound(b + 1, b + n + 1, a[i]) - b;
root[i] = update(root[i - 1], 1, n, pos);
}
while (m--) {
int l;
scanf("%d", &l);
if (l == 1) {
int pos = 1; // 随意初始化
scanf("%d", &pos);
pos ^= lastAns;
//cout << "pos = " << pos << endl;
s.insert(a[pos]);
}
else {
int l, k;
scanf("%d %d", &l, &k);
l ^= lastAns;
k ^= lastAns;
//cout << "l = " << l << "k = " << k << endl;
int ansPos = query(root[l - 1 + 1], root[n], 1, n, k);
lastAns = (ansPos == inf) ? inf : b[ansPos];
set<int>::iterator it = s.lower_bound(k);
//
if (it != s.end()) lastAns = min(lastAns, *it);
lastAns = min(lastAns, n + 1);
printf("%d\n", lastAns);
}
}
}
}
return 0;