目录

P5338-[TJOI2019]甲苯先生的滚榜

P5338-[TJOI2019]甲苯先生的滚榜

题目:

题目描述:

甲苯先生在制作一个 Online Judge,他发现做比赛的人们很关心自己的排名(显而易见)。

在 ACM 赛制的比赛中,如果通过题目数量不相等,则通过题目数量多的人排名更靠前;
如果通过题目数量相等,则罚时更少的人排名更高。

甲苯先生想让大家帮忙设计一个程序,每次有人通过之后,就告诉他排名在他的前面有多少人。
(不包括和他罚时题数都相同的同学)

输入格式:

第一行输入一个整数 $T$ 表示测试样例的个数。

对于每一个样例,输入三个整数 $m, n, \text{seed}$。
$m$ 表示参赛总人数(编号 $1 \sim m$),$n$ 表示一共有 $n$ 次 AC(假设 AC 已经去重,即不存在相同人的相同题目提交)。
$\text{seed}$ 表示生成数据的种子。

接下来要求同学们使用之下的函数生成数据:

1
2
3
4
typedef unsigned int ui ;
ui randNum( ui& seed , ui last , const ui m){ 
    seed = seed * 17 + last ; return seed % m + 1; 
}

($\texttt{last}$ 为上一次输出的结果,在没有输出结果时 $\texttt{last} = 7$,多组数据时 $\texttt{last}$ 不需要重新赋值)
要求每次生成两个数据 $\texttt{Ria}, \texttt{Rib}$,表示第 $\texttt{Ria}$ 个人 AC 了一道题目,他的罚时为 $\texttt{Rib}$。
(也就是说 $\texttt{Ria}$ 的题目数量 $+1$,罚时长度 $+\texttt{Rib}$)。

要求一共生成 $n$ 组数据,代表一共有 $n$ 次提交。

对于所有数据,保证罚时总和不超过 $1.5\times 10^6$。

输出格式:

每次提交输出一行整数,表示在第 $\texttt{Ria}$ 个人 AC 后,比 $\texttt{Ria}$ 成绩高的有多少个选手。

样例:

样例输入1:

1
2
1
7 3 1

样例输出1:

1
2
3
0
1
0

思路:

实现:

  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;
}