CF1117G.Recursive Queries
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given a permutation p1,p2,…,pn . You should answer q queries. Each query is a pair (li,ri) , and you should calculate f(li,ri) .
Let's denote ml,r as the position of the maximum in subsegment pl,pl+1,…,pr .
Then f(l,r)=(r−l+1)+f(l,ml,r−1)+f(ml,r+1,r) if l≤r or 0 otherwise.
输入格式
The first line contains two integers n and q ( 1≤n≤106 , 1≤q≤106 ) — the size of the permutation p and the number of queries.
The second line contains n pairwise distinct integers p1,p2,…,pn ( 1≤pi≤n , pi=pj for i=j ) — permutation p .
The third line contains q integers l1,l2,…,lq — the first parts of the queries.
The fourth line contains q integers r1,r2,…,rq — the second parts of the queries.
It's guaranteed that 1≤li≤ri≤n for all queries.
输出格式
Print q integers — the values f(li,ri) 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:
- f(2,2)=(2−2+1)+f(2,1)+f(3,2)=1+0+0=1 ;
- f(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 ;
- f(1,4)=(4−1+1)+f(1,2)+f(4,4)=4+3+1=8 ;
- f(2,4)=(4−2+1)+f(2,2)+f(4,4)=3+1+1=5 ;
- f(1,1)=(1−1+1)+0+0=1 .