problems / 179

Subscription Status Manager

easy design hashing simulation 1000 ms · 256 MB

Subscription Status Manager

A subscription service manages user accounts with three possible states: active, inactive, and suspended. You must process events that update subscriptions and answer queries about user statuses.

A user is active if they have a valid subscription (end date ≥ current day) and are not suspended. A user is inactive if they have no subscription or it has expired. A user is suspended if marked so by an administrator, regardless of their subscription status. When a user transitions between states, their change counter increments.

Input

The first line contains n (1 ≤ n ≤ 10^5), the number of operations. Each of the next n lines is one of:

  • SUBSCRIBE user_id days: user subscribes for days days from now (subscription ends at current_day + days)
  • RENEW user_id days: user renews their subscription, extending it by days days from their current end date
  • CANCEL user_id: user cancels their subscription
  • SUSPEND user_id: administrator suspends a user
  • UNSUSPEND user_id: administrator lifts a user's suspension
  • STATUS user_id: output the current status of the user (ACTIVE, INACTIVE, or SUSPENDED)
  • COUNT status: output how many users have the given status (ACTIVE, INACTIVE, or SUSPENDED)
  • CHANGED user_id: output how many times the user's status has changed

User IDs are positive integers up to 10^6. Days are positive integers up to 10^9. All users start as inactive with 0 changes.

  • The current day is always 0: time does not advance between operations.
  • Which users exist: a user exists from the first operation that names their id, whatever that operation is (STATUS and CHANGED included). COUNT counts only users that exist, not every possible id.

Output

For each STATUS, COUNT, or CHANGED query, print one line with the answer.

Notes

  • Suspending an already suspended user does nothing and does not count as a change.
  • Unsuspending a user who is not suspended does nothing and does not count as a change.
  • Only SUSPEND and UNSUSPEND change whether a user is suspended. SUBSCRIBE, RENEW and CANCEL update the subscription of a suspended user, who stays SUSPENDED.
  • When RENEW is used on a user with an expired subscription, it extends from the current day, not from the old end date.
  • Changes are counted whenever a user's visible status (ACTIVE, INACTIVE, or SUSPENDED) changes, not for internal subscription date changes alone.

Example 1

Input

9
SUBSCRIBE 1 10
STATUS 1
COUNT ACTIVE
SUSPEND 1
STATUS 1
CHANGED 1
UNSUSPEND 1
STATUS 1
CHANGED 1

Output

ACTIVE
1
SUSPENDED
2
ACTIVE
3

Example 2

Input

12
SUBSCRIBE 2 5
RENEW 2 5
CANCEL 2
SUBSCRIBE 2 3
COUNT INACTIVE
COUNT ACTIVE
CHANGED 2
SUBSCRIBE 3 100
SUSPEND 3
COUNT SUSPENDED
COUNT ACTIVE
UNSUSPEND 3

Output

0
1
3
1
1

Hints · reveal one at a time if you're stuck

Hint 1

Each user needs only a subscription end day and a suspension flag, and SUSPENDED overrides everything else. Which fields determine the visible status after each operation?

Hint 2

Store per-user state in arrays indexed by user id, and keep a running count per status so COUNT is O(1). A user exists from the first operation that names them, and a change counts only when the visible status actually differs.

report an issue Prefer your own editor? sooks prep 179 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.