CF331B1.Shave Beaver!

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Smart Beaver has recently designed and built an innovative nanotechnologic all-purpose beaver mass shaving machine, "Beavershave 5000". Beavershave 5000 can shave beavers by families! How does it work? Very easily!

There are n beavers, each of them has a unique id from 1 to n. Consider a permutation _a_1, _a_2, ..., a__n of n these beavers. Beavershave 5000 needs one session to shave beavers with ids from x to y (inclusive) if and only if there are such indices _i_1 < _i_2 < ... < i__k, that _a__i_1 = x, _a__i_2 = x + 1, ..., a__i__k - 1 = y - 1, a__i__k = y. And that is really convenient. For example, it needs one session to shave a permutation of beavers 1, 2, 3, ..., n.

If we can't shave beavers from x to y in one session, then we can split these beavers into groups [x, _p_1], [_p_1 + 1, _p_2], ..., [p__m + 1, y] (x ≤ _p_1 < _p_2 < ... < p__m < y), in such a way that the machine can shave beavers in each group in one session. But then Beavershave 5000 needs m + 1 working sessions to shave beavers from x to y.

All beavers are restless and they keep trying to swap. So if we consider the problem more formally, we can consider queries of two types:

  • what is the minimum number of sessions that Beavershave 5000 needs to shave beavers with ids from x to y, inclusive?
  • two beavers on positions x and y (the beavers a__x and a__y) swapped.

You can assume that any beaver can be shaved any number of times.

聪明的海狸最近设计并制造了一台创新的纳米技术全能型海狸群体剃毛机——“Beavershave 5000”。Beavershave 5000 能够按家族为单位给海狸剃毛!它的工作原理非常简单!

共有 nn 只海狸,每只海狸拥有一个从 11 到 nn 的唯一编号。考虑这 nn 只海狸的一个排列 a1, a2, …, ana_1,\,a_2,\,\dots,\,a_n。当且仅当存在下标 i1<i2<⋯<iki_1 < i_2 < \dots < i_k,使得 ai1=xa_{i_1} = x,ai2=x+1a_{i_2} = x+1,…,aik−1=y−1a_{i_{k-1}} = y-1,aik=ya_{i_k} = y 时,Beavershave 5000 才能在**一次工作会话(session)**中完成对编号从 xx 到 yy(含端点)的所有海狸的剃毛。这确实非常方便。例如,对于排列 1, 2, 3, …, n1,\,2,\,3,\,\dots,\,n,只需一次会话即可完成全部剃毛。

若无法在一次会话中剃完编号从 xx 到 yy 的所有海狸,则可将这些海狸划分为若干连续区间:[x, p1], [p1+1, p2], …, [pm+1, y][x,\,p_1],\,[p_1+1,\,p_2],\,\dots,\,[p_m+1,\,y](其中 x≤p1<p2<⋯<pm<yx \le p_1 < p_2 < \dots < p_m < y),使得机器能在每次会话中恰好处理其中一个区间。此时,Beavershave 5000 需要 m+1m+1 次工作会话来完成对编号从 xx 到 yy 的所有海狸的剃毛。

所有海狸都坐立不安,不断尝试相互交换位置。因此,若更形式化地建模该问题,我们需支持两类查询:

  • Beavershave 5000 剃掉编号从 xx 到 yy(含端点)的所有海狸所需的最少会话次数是多少?
  • 位置 xx 与位置 yy 上的两只海狸(即 axa_x 与 aya_y)发生交换。

你可以假设任意一只海狸可被重复剃毛任意多次。

输入格式

The first line contains integer n — the total number of beavers, 2 ≤ n. The second line contains n space-separated integers — the initial beaver permutation.

The third line contains integer q — the number of queries, 1 ≤ q ≤ 105. The next q lines contain the queries. Each query i looks as p__i x__i y__i, where p__i is the query type (1 is to shave beavers from x__i to y__i, inclusive, 2 is to swap beavers on positions x__i and y__i). All queries meet the condition: 1 ≤ x__i < y__i ≤ n.

  • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem B1);
  • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems B1+B2).

Note that the number of queries q is limited 1 ≤ q ≤ 105 in both subproblem B1 and subproblem B2.

第一行包含一个整数 nn —— 河狸的总数,满足 2≤n2 \leq n。第二行包含 nn 个用空格分隔的整数 —— 初始的河狸排列。

第三行包含一个整数 qq —— 查询次数,满足 1≤q≤1051 \leq q \leq 10^5。接下来的 qq 行为各次查询。第 ii 次查询的形式为 pi xi yip_i\ x_i\ y_i,其中 pip_i 表示查询类型(pi=1p_i = 1 表示剃除位置从 xix_i 到 yiy_i(含端点)的所有河狸;pi=2p_i = 2 表示交换位于位置 xix_i 和 yiy_i 的两只河狸)。所有查询均满足条件:1≤xi<yi≤n1 \leq x_i < y_i \leq n。

  • 若要获得 30 分,需在约束条件 n≤100n \leq 100 下解决该问题(子问题 B1);
  • 若要获得 100 分,需在约束条件 n≤3⋅105n \leq 3 \cdot 10^5 下解决该问题(子问题 B1 + B2)。

注意:在子问题 B1 和子问题 B2 中,查询次数 qq 的限制均为 1≤q≤1051 \leq q \leq 10^5。

输出格式

For each query with p__i = 1, print the minimum number of Beavershave 5000 sessions.

对于每个满足 pi=1p_i = 1 的查询,输出 Beavershave 5000 所需的最少会话次数。

输入输出样例

  • 输入#1

    5
    1 3 4 2 5
    6
    1 1 5
    1 3 4
    2 2 3
    1 1 5
    2 1 5
    1 1 5

    输出#1

    2
    1
    3
    5

输入解题思路,AI测评打分。不知道怎么写?

首页