#P5386. [Cnoi2019] 数字游戏

    ID: 4268 远端评测题 3000~7000ms 125MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2019莫队O2优化块状链表,块状数组,分块

[Cnoi2019] 数字游戏

Description

Given a permutation π\pi of 1n1 \sim n, and qq queries. Each query contains an integer quadruple (l,r,x,y)( l, r, x, y ), asking how many integer pairs (u,v)( u, v ) satisfy:

  • luvrl \le u \le v \le r;
  • and for all uiv\forall u \le i \le v, we have xπiyx \le \pi_i \le y.

Input Format

The first line contains two integers nn and qq.

The second line contains nn integers, representing π\pi.

In the next qq lines, each line contains one query quadruple.

Output Format

Output qq lines, where each line is the answer to one query.

4 1
1 2 3 4
1 4 2 4
6

Hint

Subtask 1 (3434 points): 1n,q3×1041 \le n, q \le 3 \times 10^4.

Subtask 2 (6666 points): 1n,q2×1051 \le n, q \le 2 \times 10^5.

Translated by ChatGPT 5