Yellow_and_Strong @ 2022-10-06 09:50:59
rt,自测应该是查询排名的问题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int MAXN = 1e5 + 1e2;
int n, m;
int a[MAXN];
int res_and_last, ans;
struct tree
{
int ls, rs, size, val, cnt, rank;
}t[MAXN];
int root, sum;
inline int read()
{
int x = 0; char ch = getchar();
while ( !isdigit(ch) ) ch = getchar();
while ( isdigit(ch) ) { x = x * 10 + (ch - '0'); ch = getchar(); }
return x;
}
inline void push_up (int p) { t[p].size = t[t[p].ls].size + t[t[p].rs].size + t[p].cnt; }
void l_rotate (int &p)
{
int tmp = t[p].rs;
t[p].rs = t[tmp].ls;
t[tmp].ls = p;
t[tmp].size = t[p].size;
push_up (p);
p = tmp;
}
void r_rotate (int &p)
{
int tmp = t[p].ls;
t[p].ls = t[tmp].rs;
t[tmp].rs = p;
t[tmp].size = t[p].size;
push_up (p);
p = tmp;
}
void insert (int &p, int k)
{
if (!p)
{
p = ++ sum;
t[p].size = 1;
t[p].val = k; t[p].cnt = 1;
t[p].rank = rand();
return;
}
t[p].size ++;
if (k == t[p].val) t[p].cnt ++;
else if (k < t[p].val)
{
insert (t[p].ls, k);
if (t[t[p].ls].rank < t[p].rank) r_rotate (p);
}
else
{
insert (t[p].rs, k);
if (t[t[p].rs].rank < t[p].rank) l_rotate (p);
}
}
bool del (int &p, int k)
{
if (!p) return false;
if (k == t[p].val)
{
if (t[p].cnt > 1) { t[p].cnt --; t[p].size --; return true; }
if (!t[p].ls or !t[p].rs) { p = t[p].ls + t[p].rs; return true; }
if (t[t[p].ls].rank < t[t[p].rs].rank) { r_rotate(p); del(p, k); return true; }
else { l_rotate(p); del(p, k); return true; }
}
else if (k < t[p].val)
{
bool succ = del (t[p].ls, k);
if (succ) t[p].size --;
return succ;
}
else
{
bool succ = del (t[p].rs, k);
if (succ) t[p].size --;
return succ;
}
}
int query_rank (int p, int k)
{
if (!p) return 0;
if (k == t[p].val) return t[t[p].ls].size + 1;
else if (k < t[p].val) return query_rank (t[p].ls, k);
else return t[t[p].ls].size + t[p].cnt + query_rank (t[p].rs, k);
}
int query_num (int p, int k)
{
if (!p) return 0;
if (k <= t[t[p].ls].size) return query_num (t[p].ls, k);
else if (k > t[t[p].ls].size + t[p].cnt) return query_num (t[p].rs, k - t[t[p].ls].size - t[p].cnt);
else return t[p].val;
}
void query_pre (int p, int k)
{
if (!p) return;
if (k > t[p].val) { res_and_last = t[p].val; query_pre(t[p].rs, k); }
else query_pre (t[p].ls, k);
}
void query_sub (int p, int k)
{
if (!p) return;
if (k < t[p].val) { res_and_last = t[p].val; query_sub(t[p].ls, k); }
else query_sub (t[p].rs, k);
}
void work()
{
n = read(); m = read();
for (register int i = 1; i <= n; i ++)
{ a[i] = read(); insert(root, a[i]); }
while (m --)
{
int opt, x;
opt = read(); x = read(); x ^= res_and_last;
//cout << x << endl;
if (opt == 1) insert (root, x);
else if (opt == 2) del (root, x);
if (opt <= 2) continue;
if (opt == 3) res_and_last = query_rank (root, x);
else if (opt == 4) res_and_last = query_num (root, x);
else if (opt == 5) query_pre (root, x);
else if (opt == 6) query_sub (root, x);
ans ^= res_and_last;
//cout << res_and_last << endl << ans << endl;
}
printf ("%d\n", ans);
}
int main()
{
work();
return 0;
}
by Yellow_and_Strong @ 2022-10-06 10:04:00
叫上去 16 分,有 WA 有 RE
by Yellow_and_Strong @ 2022-10-06 10:11:04
代码改改交到普通版上也能切
by xingke233 @ 2022-10-06 10:19:39
@shanqixiuziji
int query_rank (int p, int k)
{
// if (!p) return 1;
if (k == t[p].val) return t[t[p].ls].size + 1;
else if (k < t[p].val) return query_rank (t[p].ls, k);
else return t[t[p].ls].size + t[p].cnt + query_rank (t[p].rs, k);
}
by xingke233 @ 2022-10-06 10:20:21
@shanqixiuziji
排名为小于的数加1,不存在的数排名为1
by Yellow_and_Strong @ 2022-10-06 10:22:05
@xingke233 已过,谢谢 dalao,已关注
by Yellow_and_Strong @ 2022-10-06 10:22:32
此贴结