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
| #include <bits/stdc++.h>
using namespace std;
using LL = long long;
struct segTree {
shared_ptr<segTree> lson, rson;
LL tag;
segTree() : tag(0) {};
void update(int l, int r, LL val, int L, int R) {
if(l <= L && R <= r) {
tag += val;
return;
}
int mid = (L + R) / 2;
if(l <= mid) {
if(!lson) lson = make_shared<segTree>();
lson->update(l, r, val, L, mid);
}
if(r > mid) {
if(!rson) rson = make_shared<segTree>();
rson->update(l, r, val, mid + 1, R);
}
}
LL query(int pos, int L, int R) {
LL ans = tag;
if(L != R) {
int mid = (L + R) / 2;
if(pos <= mid && lson) {
ans += lson->query(pos, L, mid);
}
if(pos > mid && rson) {
ans += rson->query(pos, mid + 1, R);
}
}
return ans;
}
};
void solve() {
int n, q;
cin >> n >> q;
int attr_id = 0, last = 0;
map<int, pair<int, int>> LR {{0, {0, -1}}, {n, {1, 0}}, {n + 1, {n + 1, -1}}};
function<int(int)> get_attr = [&] (int x) {
return LR.lower_bound(x)->second.second;
};
vector<LL> value {0};
shared_ptr<segTree> root = make_shared<segTree>();
while(q--) {
int op;
cin >> op;
if(op == 1) {
int x, c;
cin >> x >> c;
x = ((x - 1) ^ last) % n + 1;
c = ((c - 1) ^ last) % ((n - 1) / 2) + 1;
x = max(x, c + 1);
x = min(x, n - c);
int l = x - c, r = x + c;
pair<int, pair<int, int>> tmp;
for(auto it = LR.lower_bound(l); it->second.first <= r; it++) {
int ll = it->second.first, rr = it->first;
if(ll < l) {
tmp = {l - 1, make_pair(ll, it->second.second)};
ll = l;
}
if(rr > r) {
it->second.first = r + 1;
rr = r;
}
root->update(ll, rr, value[it->second.second], 1, n);
}
LR.erase(LR.lower_bound(l), LR.upper_bound(r));
if(tmp.first) {
LR.insert(tmp);
}
LR[r] = {l, ++attr_id};
value.push_back(0);
} else if(op == 2) {
int x, y;
cin >> x >> y;
x = ((x - 1) ^ last) % n + 1;
y = ((y - 1) ^ last) % n + 1;
auto it = LR.lower_bound(y);
int l = it->second.first, r = it->first;
int attr_x = get_attr(x), attr_y = it->second.second;
root->update(l, r, value[attr_y] - value[attr_x], 1, n);
it->second.second = attr_x;
// merge segment with same attr
vector<int> del_key;
if(prev(it)->second.second == attr_x) {
it->second.first = prev(it)->second.first;
del_key.push_back(prev(it)->first);
}
if(next(it)->second.second == attr_x) {
next(it)->second.first = it->second.first;
del_key.push_back(it->first);
}
for(int key : del_key) {
LR.erase(key);
}
} else if(op == 3) {
int x, v;
cin >> x >> v;
x = ((x - 1) ^ last) % n + 1;
value[get_attr(x)] += v;
} else {
int x;
cin >> x;
x = ((x - 1) ^ last) % n + 1;
LL ans = root->query(x, 1, n) + value[get_attr(x)];
cout << ans << "\n";
last = ans & 0x3FFFFFFF;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--) {
solve();
}
return 0;
}
|