-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic-array.cpp
More file actions
75 lines (57 loc) · 1.32 KB
/
dynamic-array.cpp
File metadata and controls
75 lines (57 loc) · 1.32 KB
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
#include <cmath>
#include <cstdio>
#include <map>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
class DynamicArray {
private:
int N;
int Q;
int lastans;
map<int, vector<int> > collection;
int index_(int x) {
return (x^this->lastans) % N;
}
public:
DynamicArray(int _N, int _Q) {
this->N = _N;
this->Q = _Q;
this->lastans = 0;
}
void insert(int x, int y) {
int idx = this->index_(x);
auto it = this->collection.find(idx);
if (it != this->collection.end()) {
(*it).second.push_back(y);
} else {
this->collection[idx] = vector<int>(1, y);
}
}
int query(int x, int y) {
int idx = this->index_(x);
auto it = this->collection.find(idx);
if (it != this->collection.end()) {
vector<int> &a = (*it).second;
lastans = a[y % a.size()];
return lastans;
} else {
return -1;
}
}
};
int main() {
int N, Q;
cin >> N >> Q;
DynamicArray da(N, Q);
for (int i = 0; i<Q; i++) {
int command, x, y;
cin >> command >> x >> y;
if (command == 1) {
da.insert(x, y);
} else {
cout << da.query(x, y) << endl;
}
}
}