시간 제한메모리 제한제출정답맞힌 사람정답 비율
30 초 (추가 시간 없음) 2048 MB333100.000%

문제

A region consists of $N$ cities, numbered from $1$ to $N$. These cities may establish treaties, which indicate their willingness to collaborate with each other.

An alliance is a group of cities where each city is connected to every other city in the group through one or more treaties. The size of an alliance is the number of cities that belong to that alliance.

Each treaty has a length, representing the number of pages used to draft it. To reduce bureaucracy, cities will occasionally remove the longest active treaty (i.e., the one with the highest page count).

Beyond administrative matters, the cities also engage in dodgeball rounds between alliances. A dodgeball round follows the following rules:

  • Each alliance forms a team.
  • Alliances are paired up to play against each other.
  • If the number of alliances is odd, one team will play against an empty alliance of size $0$.
  • The unfairness score of a match between two alliances of sizes $A$ and $B$ is $|A-B|$.
  • The total unfairness score for a round is the sum of unfairness scores across all matches.

You must determine the minimum possible total unfairness score by optimally pairing the alliances.

쿼리

You must support $Q$ queries of the following types:

  • a: Add a treaty between cities $u$ and $v$ with $p$ pages.
  • r: Remove the longest treaty (the one with the highest page count). All page counts are unique.
  • d: Compute the minimum unfairness score for a dodgeball round based on the current alliances.

Queries of type d must be answered immediately upon request, before processing any further queries.

입력

The program should read:

  • Line 1: $N$ $Q$
  • Next $Q$ lines: one of
    • a $u$ $v$ $p$
    • r
    • d

출력

For each query of type d, the program should immediately output the corresponding answer before processing further queries. You must also flush the output immediately, e.g.:

  • C++: std::cout << std::endl;
  • Python: print("", flush=True)

제한

  • $1 \leq N \leq 100000$
  • $1 \leq Q \leq 500000$
  • $1 \leq p \leq 10^9$
  • $1 \leq u, v \leq N$
  • $u \neq v$ for each a query
  • No duplicate active treaty between $u$ and $v$
  • All page counts $p$ are unique

서브태스크

번호배점제한
19

$N \le 10, Q \le 20$

210

$N \le 2000, Q \le 4000$

36

At most 10 d queries

417

For each a query, $u+1 = v$

514

Treaties are created in increasing page count order

626

Treaties are created in decreasing page count order

718

No additional constraints

예제 입력 1

3 5
a 1 2 1
a 2 3 2
d
r
d

예제 출력 1

3
1

When the first dodgeball tournament is played, cities $1$, $2$, and $3$ are all in one alliance of size $3$, so they play against an empty alliance of size $0$, giving $|3-0| = 3$.

Then the treaty between cities $2$ and $3$ is removed. The alliances become $\{1,2\}$ and $\{3\}$, giving an unfairness score of $|2-1| = 1$.

예제 입력 2

6 10
a 2 3 10
a 1 2 5
a 3 4 8
d
r
d
a 4 5 1
a 3 6 7
r
d

예제 출력 2

4
0
2

In the first round, alliances have sizes $4$, $1$, and $1$, giving total unfairness $4$.

In the second round, alliances have sizes $2$, $2$, $1$, and $1$, giving total unfairness $0$.

In the third round, there are three alliances of size $2$, giving total unfairness $2$.

출처

Olympiad > Nordic Olympiad in Informatics > 2025 A번

  • 문제를 만든 사람: David Rasmussen Lolck

채점 및 기타 정보

  • 예제는 채점하지 않는다.
  • 이 문제의 채점 우선 순위는 3이다.