CF1117G.Recursive Queries

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

You are given a permutation p1,p2,,pnp_1, p_2, \dots, p_n . You should answer qq queries. Each query is a pair (li,ri)(l_i, r_i) , and you should calculate f(li,ri)f(l_i, r_i) .

Let's denote ml,rm_{l, r} as the position of the maximum in subsegment pl,pl+1,,prp_l, p_{l+1}, \dots, p_r .

Then f(l,r)=(rl+1)+f(l,ml,r1)+f(ml,r+1,r)f(l, r) = (r - l + 1) + f(l, m_{l,r} - 1) + f(m_{l,r} + 1, r) if lrl \le r or 00 otherwise.

输入格式

The first line contains two integers nn and qq ( 1n1061 \le n \le 10^6 , 1q1061 \le q \le 10^6 ) — the size of the permutation pp and the number of queries.

The second line contains nn pairwise distinct integers p1,p2,,pnp_1, p_2, \dots, p_n ( 1pin1 \le p_i \le n , pipjp_i \neq p_j for iji \neq j ) — permutation pp .

The third line contains qq integers l1,l2,,lql_1, l_2, \dots, l_q — the first parts of the queries.

The fourth line contains qq integers r1,r2,,rqr_1, r_2, \dots, r_q — the second parts of the queries.

It's guaranteed that 1lirin1 \le l_i \le r_i \le n for all queries.

输出格式

Print qq integers — the values f(li,ri)f(l_i, r_i) for the corresponding queries.

输入输出样例

  • 输入#1

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

    输出#1

    1 6 8 5 1 
    

说明/提示

Description of the queries:

  1. f(2,2)=(22+1)+f(2,1)+f(3,2)=1+0+0=1f(2, 2) = (2 - 2 + 1) + f(2, 1) + f(3, 2) = 1 + 0 + 0 = 1 ;
  2. f(1,3)=(31+1)+f(1,2)+f(4,3)=3+(21+1)+f(1,0)+f(2,2)=3+2+(22+1)=6f(1, 3) = (3 - 1 + 1) + f(1, 2) + f(4, 3) = 3 + (2 - 1 + 1) + f(1, 0) + f(2, 2) = 3 + 2 + (2 - 2 + 1) = 6 ;
  3. f(1,4)=(41+1)+f(1,2)+f(4,4)=4+3+1=8f(1, 4) = (4 - 1 + 1) + f(1, 2) + f(4, 4) = 4 + 3 + 1 = 8 ;
  4. f(2,4)=(42+1)+f(2,2)+f(4,4)=3+1+1=5f(2, 4) = (4 - 2 + 1) + f(2, 2) + f(4, 4) = 3 + 1 + 1 = 5 ;
  5. f(1,1)=(11+1)+0+0=1f(1, 1) = (1 - 1 + 1) + 0 + 0 = 1 .
首页