#P5386. [Cnoi2019] 数字游戏

[Cnoi2019] 数字游戏

Problem 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