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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
| // Problem: P5338 [TJOI2019]甲苯先生的滚榜
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P5338
// Memory Limit: 1 MB
// Time Limit: 10000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include "ybwhead/ios.h"
template <typename Tp>
void read(Tp &x)
{
x = 0;
char ch = 1;
int fh;
while (ch != '-' && (ch < '0' || ch > '9'))
ch = getchar();
if (ch == '-')
fh = -1, ch = getchar();
else
fh = 1;
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + ch - '0';
ch = getchar();
}
x *= fh;
}
const int INF = 0x3f3f3f3f;
const int maxm = 1000000 + 7;
int n, T, root;
unsigned seed, las = 7, m;
typedef unsigned int ui;
ui randNum(ui &seed, ui last, ui m)
{
seed = seed * 17 + last;
return seed % m + 1;
}
int dat[maxm], size[maxm], cnt[maxm], ac[maxm], tim[maxm];
int ch[maxm][2], tot;
int nowac[maxm * 10], nowtim[maxm * 10];
void pushup(int x)
{
size[x] = size[ch[x][0]] + size[ch[x][1]] + cnt[x];
}
int New(int a, int b)
{
dat[++tot] = rand(), size[tot] = cnt[tot] = 1, ac[tot] = a, tim[tot] = b;
return tot;
}
void build()
{
tot = 0;
root = New(INF, -INF);
ch[root][1] = New(-INF, INF);
pushup(root);
}
void rotate(int &id, int dir)
{
int tmp = ch[id][dir xor 1];
ch[id][dir xor 1] = ch[tmp][dir];
ch[tmp][dir] = id;
id = tmp;
pushup(ch[id][dir]);
pushup(id);
}
void insert(int &id, int a, int b)
{
if (!id)
{
id = New(a, b);
return;
}
if (a == ac[id] && b == tim[id])
cnt[id]++;
else
{
int d;
if (a > ac[id])
d = 0;
else if (a == ac[id])
{
if (b < tim[id])
d = 0;
else
d = 1;
}
else
d = 1;
insert(ch[id][d], a, b);
if (dat[id] < dat[ch[id][d]])
rotate(id, d xor 1);
}
pushup(id);
}
void remove(int &id, int a, int b)
{
if (!id)
return;
if (ac[id] == a && tim[id] == b)
{
if (cnt[id] > 1)
{
cnt[id]--;
pushup(id);
return;
}
if (ch[id][0] || ch[id][1])
{
if (!ch[id][1] || dat[ch[id][0]] > dat[ch[id][1]])
{
rotate(id, 1);
remove(ch[id][1], a, b);
}
else
{
rotate(id, 0);
remove(ch[id][0], a, b);
}
pushup(id);
}
else
id = 0;
return;
}
if (a > ac[id])
{
remove(ch[id][0], a, b);
}
else if (a == ac[id])
{
if (b < tim[id])
remove(ch[id][0], a, b);
else
remove(ch[id][1], a, b);
}
else
remove(ch[id][1], a, b);
pushup(id);
}
int get_rank(int id, int a, int b)
{
if (!id)
return 0;
if (a == ac[id] && b == tim[id])
return size[ch[id][0]] + 1;
if (a > ac[id])
return get_rank(ch[id][0], a, b);
else if (a == ac[id] && b < tim[id])
return get_rank(ch[id][0], a, b);
return size[ch[id][0]] + cnt[id] + get_rank(ch[id][1], a, b);
}
int TTTT;
int main()
{
srand(28910);
read(TTTT);
while (TTTT--)
{
build();
memset(ch, 0, sizeof(ch));
memset(nowac, 0, sizeof(nowac));
memset(nowtim, 0, sizeof(nowtim));
read(m);
read(n);
read(seed);
for (int i = 1; i <= n; i++)
{
int x, y;
x = randNum(seed, las, m), y = randNum(seed, las, m);
remove(root, nowac[x], nowtim[x]);
nowac[x]++, nowtim[x] += y;
// insert(root,nowac[x],nowtim[y]);
insert(root, nowac[x], nowtim[x]); //错误笔记:弄错x,y
// las=get_rank(root,nowac[x],nowtim[y])-2;
las = get_rank(root, nowac[x], nowtim[x]) - 2; //错误笔记:弄错x,y
printf("%d\n", las);
}
}
return 0;
}
|