CF323C.Two permutations

提高+/省选-

通过率:0%

时间限制:6.00s

内存限制:512MB

AC君温馨提醒

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

题目描述

You are given two permutations p and q, consisting of n elements, and m queries of the form: _l_1, _r_1, _l_2, _r_2 (_l_1 ≤ _r_1; _l_2 ≤ _r_2). The response for the query is the number of such integers from 1 to n, that their position in the first permutation is in segment [_l_1, _r_1] (borders included), and position in the second permutation is in segment [_l_2, _r_2] (borders included too).

A permutation of n elements is the sequence of n distinct integers, each not less than 1 and not greater than n.

Position of number v (1 ≤ v ≤ n) in permutation _g_1, _g_2, ..., g__n is such number i, that g__i = v.

给你两个由 $ n $ 个元素组成的排列 $ p $ 和 $ q $,以及 $ m $ 个查询,每个查询形如:$ l_1,\ r_1,\ l_2,\ r_2 $(满足 $ l_1 \leq r_1 ;; l_2 \leq r_2 $)。对每个查询的回答是:满足以下条件的整数 $ v $(其中 $ 1 \leq v \leq n $)的个数:

  • $ v $ 在第一个排列中的位置落在区间 [l1, r1][l_1,\ r_1] 内(含端点),
  • 且 $ v $ 在第二个排列中的位置落在区间 [l2, r2][l_2,\ r_2] 内(含端点)。

一个 $ n $ 元排列是指由 $ n $ 个互不相同的整数组成的序列,其中每个整数均不小于 $ 1 $ 且不大于 $ n $。

数字 $ v (( 1 \leq v \leq n $)在排列 $ g_1,\ g_2,\ \dots,\ g_n $ 中的位置定义为满足 $ g_i = v $ 的下标 $ i $。

输入格式

The first line contains one integer n (1 ≤ n ≤ 106), the number of elements in both permutations. The following line contains n integers, separated with spaces: _p_1, _p_2, ..., p__n (1 ≤ p__i ≤ n). These are elements of the first permutation. The next line contains the second permutation _q_1, _q_2, ..., q__n in same format.

The following line contains an integer m (1 ≤ m ≤ 2·105), that is the number of queries.

The following m lines contain descriptions of queries one in a line. The description of the i-th query consists of four integers: a, b, c, d (1 ≤ a, b, c, d ≤ n). Query parameters _l_1, _r_1, _l_2, _r_2 are obtained from the numbers a, b, c, d using the following algorithm:

  1. Introduce variable x. If it is the first query, then the variable equals 0, else it equals the response for the previous query plus one.
  2. Introduce function f(z) = ((z - 1 + x) mod n) + 1.
  3. Suppose _l_1 = min(f(a), f(b)), _r_1 = max(f(a), f(b)), _l_2 = min(f(c), f(d)), _r_2 = max(f(c), f(d)).

第一行包含一个整数 nn(1≤n≤1061 \leq n \leq 10^6),表示两个排列中元素的个数。
接下来一行包含 nn 个整数,以空格分隔:p1, p2, …, pnp_1,\,p_2,\,\dots,\,p_n(1≤pi≤n1 \leq p_i \leq n),即第一个排列的元素。
下一行以相同格式给出第二个排列 q1, q2, …, qnq_1,\,q_2,\,\dots,\,q_n。

随后一行包含一个整数 mm(1≤m≤2⋅1051 \leq m \leq 2\cdot10^5),表示查询的个数。

接下来 mm 行每行描述一个查询。第 ii 个查询的描述由四个整数 a, b, c, da,\,b,\,c,\,d(1≤a, b, c, d≤n1 \leq a,\,b,\,c,\,d \leq n)组成。查询参数 l1, r1, l2, r2l_1,\,r_1,\,l_2,\,r_2 由 a, b, c, da,\,b,\,c,\,d 按如下算法得到:

  1. 引入变量 xx:若为第一个查询,则 x=0x = 0;否则 xx 等于上一个查询的答案加一。
  2. 定义函数 f(z)=((z−1+x) mod n)+1f(z) = ((z - 1 + x) \bmod n) + 1。
  3. 令 l1=min⁡(f(a), f(b))l_1 = \min(f(a),\,f(b)),r1=max⁡(f(a), f(b))r_1 = \max(f(a),\,f(b)),l2=min⁡(f(c), f(d))l_2 = \min(f(c),\,f(d)),r2=max⁡(f(c), f(d))r_2 = \max(f(c),\,f(d))。

输出格式

Print a response for each query in a separate line.

对每个查询,输出一行响应。

输入输出样例

  • 输入#1

    3
    3 1 2
    3 2 1
    1
    1 2 3 3

    输出#1

    1
  • 输入#2

    4
    4 3 2 1
    2 3 4 1
    3
    1 2 3 4
    1 3 2 1
    1 4 2 3

    输出#2

    1
    1
    2

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

首页