-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path502.cpp
74 lines (61 loc) · 1.65 KB
/
502.cpp
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
/**
* @file 502.cpp
* @author Macesuted ([email protected])
* @date 2022-11-09
*
* @copyright Copyright (c) 2022
*
*/
#include <bits/stdc++.h>
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
bool mem1;
#define maxn 1000005
#define maxm 2000005
typedef pair<uint64_t, int> pUi;
uint64_t a[maxn], pow3[41], pre[maxm];
pUi rec[maxm];
uint64_t Xor3(uint64_t x, uint64_t y) {
uint64_t ans = 0;
for (int i = 0; i < 40; i++) ans += (x % 3 + y % 3) % 3 * pow3[i], x /= 3, y /= 3;
return ans;
}
void solve(void) {
int n, m, lastans = 0;
cin >> n >> m;
mt19937_64 rnd(114514);
for (int i = 1; i <= n; i++) a[i] = rnd() % pow3[40], rec[i] = {a[i], i}, rec[i + n] = {Xor3(a[i], a[i]), i};
sort(rec + 1, rec + 2 * n + 1);
for (int i = 1; i <= m; i++) {
int u, p;
cin >> u >> p, u ^= lastans, p ^= lastans;
pre[i] = Xor3(pre[p], a[u]);
if (!pre[i])
cout << (lastans = -1) << endl;
else {
int p = lower_bound(rec + 1, rec + 2 * n + 1, pUi{pre[i], 0}) - rec;
if (p == 2 * n + 1 || rec[p].first != pre[i])
cout << (lastans = -2) << endl;
else
cout << (lastans = rec[p].second) << endl;
}
}
return;
}
bool mem2;
int main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
#ifdef LOCAL
cerr << "Memory Cost: " << abs(&mem1 - &mem2) / 1024. / 1024. << "MB" << endl;
#endif
pow3[0] = 1;
for (int i = 1; i < 41; i++) pow3[i] = pow3[i - 1] * 3;
int _ = 1;
while (_--) solve();
#ifdef LOCAL
cerr << "Time Cost: " << clock() * 1000. / CLOCKS_PER_SEC << "MS" << endl;
#endif
return 0;
}