#P4879. Calculating Ycz's Delight

    ID: 18120 Type: Default 1000ms 256MiB

Calculating Ycz's Delight

Calculating Ycz's Delight

Ycz has n childhood friends, each originally living in cities numbered from 1 to n arranged in a straight line. Initially, every city has a girl with a beauty value of 0. Over time, events occur that either change or replace these girls:

  • C x y: The girl in city x experiences a decrease in beauty. Formally, if her current beauty is B, it becomes \(B = B - y\). Note that \(y \ge 0\), but the resulting beauty might be negative.
  • I x y: A new girl appears in city x with beauty \(y\) (\(y\) can be negative). This new girl immediately replaces any girl previously in city x.
  • D x: The girl in the x-th occupied city is removed (i.e. Ycz breaks up with her). To determine which girl is removed, consider all cities in increasing order, and count only those cities that currently have a girl. If there are fewer than x girls, this operation is ignored.

Your task is to process m events in order and then compute Ycz's overall "delight" when he visits his remaining girls. The delight is defined as the sum of the beauty values of all girls who are still present. Note that a girl's beauty can be negative.

Note:

  1. Each city can have at most one girl at any time.
  2. The initial girls (childhood friends) are in all cities from 1 to n with an initial beauty of 0.
  3. An I event replaces the girl (if any) in city x immediately.
  4. A D event removes the x-th girl among the occupied cities (ordered by city number).

inputFormat

The first line contains two space-separated integers n and m (\(1 \leq n, m \leq 10^5\)), representing the number of cities and the number of events.

Each of the following m lines contains an event in one of the following formats:

  • C x y — decrease the beauty of the girl in city x by \(y\) (\(y \geq 0\)).
  • I x y — a new girl with beauty \(y\) (can be negative) appears in city x, replacing any existing girl.
  • D x — remove the girl in the x-th occupied city (in increasing order of city number). If there are fewer than x occupied cities, ignore this event.

Initially, each city from 1 to n has a girl with beauty 0.

outputFormat

Output a single integer representing the sum of the beauty values of all girls remaining after processing all events.

sample

5 5
I 3 10
C 1 5
D 2
C 3 2
I 1 -3
5