array HDU - 6703 主席树 + set

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair P;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
struct SegmentTree
{
	int lc, rc;
	int dat;
}t[N * 30];
int tot, root[N], n, a[N], m;
set s;
int update(int now, int l, int r, int x)
{
	int p = ++tot;
	t[p] = t[now];
	if (l == r)
	{
		t[p].dat++;
		return p;
	}
	int mid = (l + r) >> 1;
	if (x <= mid)
		t[p].lc = update(t[now].lc, l, mid, x);
	else
		t[p].rc = update(t[now].rc, mid + 1, r, x);
	t[p].dat = t[t[p].lc].dat + t[t[p].rc].dat;
	return p;
}
int build(int l, int r)
{
	//cout << l << " " << r << endl;
	int p = ++tot;
	t[p].dat = 0;
	if (l == r)
	{
		return p;
	}
	int mid = (l + r) >> 1;
	t[p].lc = build(l, mid);
	t[p].rc = build(mid + 1, r);
	return p;
}
int ask(int p, int q, int l, int r, int L, int R)
{
	if (t[p].dat - t[q].dat == 0)
		return -1;
	if (l == r)
	{
		if (t[p].dat - t[q].dat)
			return l;
		return -1;
	}
	int mid = (l + r) >> 1;
	int ret = -1;
	if (L <= mid)
		ret = ask(t[p].lc, t[q].lc, l, mid, L, R);
	if (ret == -1 && R > mid)
		ret = ask(t[p].rc, t[q].rc, mid + 1, r, L, R);
	return ret;
}
void init()
{
	tot = 0;
	root[0] = build(1, n);
}
int main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(0), cout.tie(0);
	//freopen("./1.txt", "w", stdout);
	int t;
	//cin >> t;
	scanf("%d", &t);
	while (t--)
	{
		
		s.clear();
		//cin >> n >> m;
		scanf("%d%d", &n, &m);
		init();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
			//cin >> a[i];
			root[i] = update(root[i - 1], 1, n, a[i]);
		}
		int lastans = 0;
		int op, x, y;
		s.insert(n + 1);
		for (int i = 1; i <= m; i++)
		{
			//cin >> op;
			scanf("%d", &op);
			if (op == 1)
			{
				scanf("%d", &x);
				//cin >> x;
				x ^= lastans;
				s.insert(a[x]);
			}
			else 
			{
				scanf("%d%d", &x, &y);
				//cin >> x >> y;
				int r = x ^ lastans, k = y ^ lastans;
				lastans = ask(root[n], root[r], 1, n, k, n);
				int temp = *s.lower_bound(k);
				if (lastans != -1)
					lastans = min(lastans, temp);
				else
					lastans = temp;
				//cout << lastans << endl;
				printf("%d\n", lastans);
			}
		}
	}
	return 0;
}

 

你可能感兴趣的:(线段树&&主席树)