CF1117G.Recursive Queries
省选/NOI-
通过率:0%
时间限制:4.00s
内存限制:256MB
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.
给你一个排列 p1,p2,…,pn。你需要回答 q 个查询。每个查询是一个数对 (li,ri),你需要计算 f(li,ri)。
记 ml,r 为子段 pl,pl+1,…,pr 中最大值的位置。
那么,当 l≤r 时,f(l,r)=(r−l+1)+f(l,ml,r−1)+f(ml,r+1,r);否则 f(l,r)=0。
输入格式
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.
第一行包含两个整数 n 和 q(1≤n≤106,1≤q≤106)——分别为排列 p 的长度和查询次数。
第二行包含 n 个两两不同的整数 p1,p2,…,pn(1≤pi≤n,当 i=j 时 pi=pj)——即排列 p。
第三行包含 q 个整数 l1,l2,…,lq —— 各查询的左端点。
第四行包含 q 个整数 r1,r2,…,rq —— 各查询的右端点。
保证对所有查询均有 1≤li≤ri≤n。
输出格式
Print q integers — the values f(li,ri) for the corresponding queries.
输出 q 个整数——即各查询对应的 f(li,ri) 的值。
输入输出样例
输入#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.
查询的描述:
- 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。
输入解题思路,AI测评打分。不知道怎么写?