luogu#P9726. [EC Final 2022] Magic

[EC Final 2022] Magic

题目描述

Warning: Unusual memory limit!

You are given a sequence a0,,a2na_0,\ldots,a_{2n}. Initially, all numbers are zero.

There are nn operations. The ii-th operation is represented by two integers li,ril_i, r_i (1li<ri2n,1in1\le l_i < r_i\le 2n, 1\le i\le n), which assigns ii to ali,,ari1a_{l_i},\ldots,a_{r_i-1}. It is guaranteed that all the 2n2n integers, l1,l2,,ln,r1,r2,,rnl_1,l_2,\ldots, l_n, r_1, r_2, \ldots, r_n, are distinct.

You need to perform each operation exactly once, in arbitrary order.

You want to maximize the number of ii (0i<2n)(0\leq i< 2n) such that aiai+1a_i\neq a_{i+1} after all nn operations. Output the maximum number.

输入格式

The first line contains an integer nn (1n5×1031\le n\le 5\times 10^3).

The ii-th line of the next nn lines contains a pair of integers li,ril_i, r_i (1li<ri2n1\le l_i < r_i\le 2n). It is guaranteed that all the 2n2n integers, l1,l2,,ln,r1,r2,,rnl_1,l_2,\ldots, l_n, r_1, r_2, \ldots, r_n, are distinct.

输出格式

Output one integer representing the answer in one line.

题目大意

  • 你有一个序列 a0,a1,a2a2na_0,a_1,a_2\dots a_{2n},初始全为 00
  • 给定 nn 个区间赋值操作,第 ii 个操作 (li,ri)(1li,ri2n)(l_i,r_i)(1\le l_i,r_i\le 2n) 表示把区间 [li,ri)[l_i,r_i) 全部赋值为 ii保证所有 li,ril_i,r_i 互不相同
  • 你可以指定一个执行操作的顺序,最大化 i=02n1[aiai+1]\sum_{i=0}^{2n-1}[a_i\ne a_{i+1}],输出这个最大值。
  • 1n5×1031\le n\le 5\times 10^3注意空间限制
5
2 3
6 7
1 9
5 10
4 8

9