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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
| #include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::vector;
const int MAXN = 80001;
//------Segment Tree
struct Node
{
int value;
Node *lef, *rig;
} _memory[MAXN*250], *_memory_top = _memory;
int _n, _x, _st;
Node* _new_archive(Node* const pre, const int lef, const int rig)
{
Node* const node = _memory_top++;
node->value = pre->value+_x, node->lef = pre->lef, node->rig = pre->rig;
if (lef == rig) return node;
const int mid = (lef+rig)/2;
if (_st <= mid) node->lef = _new_archive(pre->lef, lef, mid);
else node->rig = _new_archive(pre->rig, mid+1, rig);
return node;
}
int _query(Node* const u, Node* const v, Node* const lca, Node* const p, const int lef, const int rig)
{
if (lef == rig) return lef;
const int value = u->lef->value + v->lef->value - lca->lef->value - p->lef->value;
const int mid = (lef+rig)/2;
if (value >= _x) return _query(u->lef, v->lef, lca->lef, p->lef, lef, mid);
_x -= value;
return _query(u->rig, v->rig, lca->rig, p->rig, mid+1, rig);
}
Node* Build(const int lef, const int rig)
{
Node* const node = _memory_top++;
node->lef = node->rig = 0, node->value = 0;
if (lef == rig) return node;
const int mid = (lef+rig)/2;
node->lef = Build(lef, mid);
node->rig = Build(mid+1, rig);
return node;
}
inline Node* NewArchive(Node* const pre, const int pos, const int delta)
{ _st = pos, _x = delta; return _new_archive(pre, 1, _n); }
inline int Query(Node* const u, Node* const v, Node* const lca, Node* const p, const int k)
{ _x = k; return _query(u, v, lca, p, 1, _n); }
//------Doubling Algorithm LCA
int p[MAXN][20], deep[MAXN];
int GetLCA(int u, int v)
{
if (deep[u] < deep[v]) std::swap(u, v);
for (int j = 19; deep[u] != deep[v]; u = p[u][j])
while (j > 0 && deep[p[u][j]] < deep[v]) --j;
for (int j = 19; u != v; u = p[u][j], v = p[v][j])
while (j > 0 && p[u][j] == p[v][j]) --j;
return u;
}
//------Graph
struct Edge
{
int v;
Edge *next;
} g[MAXN*4], *header[MAXN];
int LinkSize;
void AddEdge(const int x, const int y)
{
Edge* const node = g+(LinkSize++);
node->v = y;
node->next = header[x];
header[x] = node;
}
//------Main Program
Node* first[MAXN];
int n, m, t, lastans, trees, w[MAXN];
struct Tree
{
int size, u;
vector<Node*> root;
Tree() { }
Tree(const int v): size(0), u(v), root() { }
void DFS(const int, Tree* const);
void Initialize();
} tree[MAXN], *treeroot[MAXN];
vector<int> v;
void Tree::DFS(const int u, Tree* const _change_to = 0)
{
root.push_back(NewArchive(root.back(), w[u], +1)), first[u] = root.back();
if (_change_to) treeroot[u] = _change_to;
for (int i = 1; i < 20; ++i) p[u][i] = p[p[u][i-1]][i-1];
deep[u] = deep[p[u][0]]+1;
for (Edge *e = header[u]; e; e = e->next)
if (e->v != p[u][0])
{
p[e->v][0] = u;
DFS(e->v, _change_to);
}
root.push_back(NewArchive(root.back(), w[u], -1));
}
void Tree::Initialize()
{
root.push_back(first[0]);
DFS(u);
}
char c;
void BuildTree(const int u, Tree* const tr)
{
++tr->size;
treeroot[u] = tr;
w[u] = std::lower_bound(v.begin(), v.end(), w[u])-v.begin()+1;
for (Edge *e = header[u]; e; e = e->next)
if (e->v != p[u][0])
{
deep[e->v] = 1;
p[e->v][0] = u;
BuildTree(e->v, tr);
}
}
void solve()
{
scanf("%d%d%d", &n, &m, &t);
trees = lastans = LinkSize = 0, _memory_top = _memory, _n = n;
v.clear();
memset(header, 0, sizeof(header));
memset(deep, 0, sizeof(deep));
for (int i = 1; i <= n; ++i)
{
scanf("%d", w+i);
v.push_back(w[i]);
}
for (int i = 0, x, y; i < m; ++i)
{
scanf("%d%d", &x, &y);
AddEdge(x, y);
AddEdge(y, x);
}
std::sort(v.begin(), v.end());
v.resize(std::unique(v.begin(), v.end())-v.begin());
for (int i = 1; i <= n; ++i)
if (!deep[i])
{
tree[trees] = Tree(i);
BuildTree(i, tree+(trees++));
}
first[0] = Build(1, n);
++_memory_top;
for (int i = 0; i < trees; ++i)
tree[i].Initialize();
for (int i = 0, x, y, k; i < t; ++i)
{
scanf("\n%c%d%d", &c, &x, &y);
x ^= lastans, y ^= lastans;
if (c == 'Q')
{
scanf("%d", &k);
k ^= lastans;
const int lca = GetLCA(x, y), plca = p[lca][0];
lastans = Query(first[x], first[y], first[lca], first[plca], k);
lastans = v[lastans-1];
printf("%d\n", lastans);
}
else
{
if (treeroot[x]->size > treeroot[y]->size) std::swap(x, y);
treeroot[y]->size += treeroot[x]->size;
treeroot[y]->root.clear();
AddEdge(x, y);
AddEdge(y, x);
p[x][0] = y;
treeroot[y]->root.push_back(first[y]);
treeroot[y]->DFS(x, treeroot[y]);
}
}
}
int main()
{
scanf("%*d");
solve();
}
|