HAOI2008 排名系統

2022-08-03 02:09:10 字數 1728 閱讀 6786

設計一種資料結構,支援給指定點插入元素並覆蓋先前的元素、查詢某一點的排名、輸出從任意排名之後的10名。

採用splay實現

關於讀入的字串,我們可以雜湊然後將雜湊值丟到map裡,這樣就可以給每乙個字串乙個編號,方便在splay上操作。

關於插入和刪除以及查詢操作,都是splay的基本操作,在此不再贅述。

關於輸出答案,我們這樣思考:根據bst的性質,當前點的右子樹的值一定大於當前點,左子樹的值一定小於當前點,也就意味著我們對這棵樹進行一次中序遍歷就可以得到乙個有序序列。那麼我們輸出答案也是如此,先在右子樹中遞迴輸出,在輸出當前節點,再進入左子樹,同時開乙個計數器記錄當前輸出了多少答案即可。

為了維護splay的隨機性,我們每執行200次操作就隨機乙個節點並將它延伸到樹根。

1 #include 2

using

namespace

std;

3 typedef unsigned long

long

ull;

4const

int inf = 2147483647;5

const ull mod =212370440130137957ll;

6 inline int

read()

13while

(isdigit(c))

17return ret *op;18}

19 inline ull rethash(char *aa)

26 inline int retnum(char *aa)

33 map int>hash;

34int n, root, tot, len[250010

];35

struct

splay a[250010

];38

char

in[20], name[250010][20

];39

void update(int

now)

42void connect(int x, int fa, int

op)

46void rotate(int

x) 55

void splay(int

from, int

to)

62if (to == 0) root = from;63

}64void insert(int val, int

x) 75 fa =now;

76 now = a[now].ch[val >a[now].val];77}

78}79int query(int

x) 88}89

}90void del(int

x) 100

void print(int now, int &sum)

110if (a[now].ch[0]) print(a[now].ch[0

], sum);

111}

112int

main()

124else

129}

130else

if (isdigit(in[1

]))

138else

143if (i % 200 == 0) splay(rand() % tot + 1, 0

);144

}145

return0;

146 }

ac code

BZOJ1056 HAOI2008 排名系統

bzoj1056 haoi2008 排名系統 排名系統通常要應付三種請求 上傳一條新的得分記錄 查詢某個玩家的當前排名以及返回某個區段內的排名 記錄。當某個玩家上傳自己最新的得分記錄時,他原有的得分記錄會被刪除。為了減輕伺服器負擔,在返回某個區 段內的排名記錄時,最多返回10條記錄。第一行是乙個整數...

HAOI 2008 硬幣購物

硬幣購物一共有 4 44 種硬幣。面值分別為 c1,c2,c 3,c4 c 1,c 2,c 3,c 4 c1 c2 c3 c4 某人去商店買東西,去了 tot totto t 次。每次帶 dij d di j 枚 cij c ci j 硬幣,買 s is i si 的價值的東西。請問每次有多少種付款...

HAOI2008 硬幣購物

時間限制 c c 1秒,其他語言2秒 空間限制 c c 262144k,其他語言524288k 64bit io format lld 題目描述 硬幣購物一共有4種硬幣。面值分別為c1,c2,c3,c4。某人去商店買東西,去了tot次。每次帶di枚ci硬幣,買si的價值的東西。請問每次有多少種付款方...