← 返回 airbnb 的题目列表Multi-Level Banking System
类型:online_judge
Implement a simplified banking system that processes operations in timestamp order. The system has four levels, and every later level must preserve all earlier functionality.
Timestamps are strictly increasing non-negative integers. Before processing each operation, execute every scheduled payment whose execution time is no later than that operation's timestamp.
Input format
The first line contains Q, the number of operations. Each following line is one operation.
Level 1: Accounts, deposits, and transfers
CREATE timestamp accountId
Create an account. Print false if accountId is already active; otherwise create it with balance 0 and print true.
DEPOSIT timestamp accountId amount
Deposit positive integer amount into an active account. Print the new balance, or null if the account does not exist.
TRANSFER timestamp fromId toId amount
Transfer positive integer amount from fromId to toId.
Both accounts must be active, distinct, and the sender must have sufficient funds.
On success, update balances, add amount to the sender's total outgoing amount, and print the sender's new balance. Otherwise print null.
Level 2: Spending leaderboard
TOP timestamp n
Print up to n currently active accounts with the largest total outgoing amount.
Outgoing amount includes successful transfer amounts sent by the account and successful scheduled payments, but not deposits.
Sort by outgoing amount descending, then by accountId lexicographically ascending.
Format: id1(total1), id2(total2), .... Print an empty line if no active account exists.
Level 3: Scheduled payments
SCHEDULE timestamp accountId amount delay
Create a scheduled debit for accountId, to execute at timestamp + delay.
If the account does not exist, print null. Otherwise assign globally increasing IDs payment1, payment2, ... and print the new ID.
Payments due at the same time execute in creation order.
At execution time, debit the account and increase its outgoing total if it is still active and has enough balance. Otherwise, the payment fails and is discarded.
CANCEL timestamp accountId paymentId
Cancel a payment only if it has not executed or been canceled and is currently owned by accountId.
Print true on success and false otherwise.
Level 4: Account merging and historical balance
MERGE timestamp targetId sourceId
Merge sourceId into targetId; both must be distinct active accounts.
On success, move the source balance into the target, add source outgoing total to target outgoing total, reassign all pending source payments to the target, deactivate the source, and print the target's resulting balance.
Print null if either account is inactive/nonexistent or the IDs are equal.
QUERY timestamp accountId queryTime
Return the balance of accountId at queryTime, after all operations processed at that time.
Print null if the account did not exist at queryTime, or had already been merged and deactivated before that time; otherwise print its balance at that time.
Print one result for every input operation. amount <= 10^9 and Q <= 2 * 10^5.
Example
Input
8
CREATE 1 alice
CREATE 2 bob
DEPOSIT 3 alice 100
TRANSFER 4 alice bob 30
TOP 5 2
SCHEDULE 6 bob 50 2
QUERY 7 bob 50
TOP 8 2
Output
true
true
100
70
alice(30), bob(0)
payment1
130
bob(50), alice(30)
Example
Input
8
CREATE 1 alice
CREATE 2 bob
DEPOSIT 3 alice 100
TRANSFER 4 alice bob 30
TOP 5 2
SCHEDULE 6 bob 50 2
QUERY 7 bob 50
TOP 8 2
Output
true
true
100
70
alice(30), bob(0)
payment1
130
bob(50), alice(30)