首页 > 其他分享 >[CF1707E] Replace

[CF1707E] Replace

时间:2024-01-17 13:55:07浏览次数:26  
标签:le 询问 样例 Replace leq CF1707E query rightarrow

Replace

题面翻译

题目描述

给定一个长为 \(n\) 的序列 \(a_1,\ldots,a_n\),其中对于任意的 \(i\) 满足 \(1 \leq a_i \leq n\)。

定义一个二元组函数如下:

\[f((l,r))=(\min\{a_l,\ldots,a_r\},\max\{a_l,\ldots,a_r\})(l \leq r) \]

你需要回答 \(q\) 次询问,每次给定 \((l_i,r_i)\),问其最少经过多少次 \(f\) 的调用(即 \((l,r) \rightarrow f((l,r))\))使得 \((l_i,r_i)\) 变成 \((1,n)\),若无解请输出 -1

输入格式

第一行包含两个整数 \(n,q(1 \leq n,q \leq 10^5)\),表示序列长度和询问次数。

第二行包含 \(n\) 个整数 \(a_i(1 \leq a_i \leq n)\),表示序列 \(a\)。

接下来 \(q\) 行每行两个整数 \(l_i,r_i(1 \leq l_i \leq r_i \leq n)\),表示每组询问。

输出格式

对每一个询问,输出其最小次数,或无解输出 -1

样例解释

第一个样例中,\(a=\{2,5,4,1,3\}\)。

对于第一个询问,\((4,4) \rightarrow (1,1) \rightarrow (2,2) \rightarrow (5,5) \rightarrow (3,3) \rightarrow (4,4) \rightarrow \cdots\),故无解。

对于第二个询问,已经有 \((l_i,r_i)=(1,n)\),需要 \(0\) 次。

对于第三个询问,\((1,4) \rightarrow (1,5)\),需要 \(1\) 次。

对于第四个询问,\((3,5) \rightarrow (1,4) \rightarrow (1,5)\),需要 \(2\) 次。

对于第五个询问,\((4,5) \rightarrow (1,3) \rightarrow (2,5) \rightarrow (1,5)\),需要 \(3\) 次。

对于第六个询问,\((2,3) \rightarrow (4,5) \rightarrow (1,3) \rightarrow (2,5) \rightarrow (1,5)\),需要 \(4\) 次。

题目描述

You are given an integer array $ a_1,\dots, a_n $, where $ 1\le a_i \le n $ for all $ i $.

There's a "replace" function $ f $ which takes a pair of integers $ (l, r) $, where $ l \le r $, as input and outputs the pair \(f((l, r))=\left(\min\{a_l,a_{l+1},\dots,a_r\},\max\{a_l,a_{l+1},\dots,a_r\}\right)\).

Consider repeated calls of this function. That is, from a starting pair \((l, r)\) we get $ f((l, r)) $, then $ f(f((l, r))) $, then $ f(f(f((l,r)))) $, and so on.

Now you need to answer $ q $ queries. For the $ i $-th query you have two integers $ l_i $ and $ r_i $ \((1\le l_i\le r_i\le n)\). You must answer the minimum number of times you must apply the "replace" function to the pair \((l_i,r_i)\) to get $ (1, n) $, or report that it is impossible.

输入格式

The first line contains two positive integers $ n $ , $ q $ ( $ 1\le n,q\le 10^5 $ ) — the length of the sequence $ a $ and the number of the queries.

The second line contains $ n $ positive integers $ a_1,a_2,\ldots,a_n $ ( $ 1\le a_i\le n $ ) — the sequence $ a $ .

Each line of the following $ q $ lines contains two integers $ l_i $ , $ r_i $ ( $ 1\le l_i\le r_i\le n $ ) — the queries.

输出格式

For each query, output the required number of times, or $ -1 $ if it is impossible.

样例 #1

样例输入 #1

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

样例输出 #1

-1
0
1
2
3
4

样例 #2

样例输入 #2

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

样例输出 #2

5
1
3

样例 #3

样例输入 #3

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

样例输出 #3

-1
-1
0

提示

In the first example, $ n=5 $ and $ a=[2,5,4,1,3] $ .

For the first query: $ (4,4)\to(1,1)\to(2,2)\to(5,5)\to(3,3)\to(4,4)\to\ldots $ , so it's impossible to get $ (1,5) $ .

For the second query, you already have $ (1,5) $ .

For the third query: $ (1,4)\to(1,5) $ .

For the fourth query: $ (3,5)\to(1,4)\to(1,5) $ .

For the fifth query: $ (4,5)\to(1,3)\to(2,5)\to(1,5) $ .

For the sixth query: $ (2,3)\to(4,5)\to(1,3)\to(2,5)\to(1,5) $ .

有一个关键结论: \(f(l,r)=\bigcup\limits_{i=l}^{r-1}f(i,i+1)\)

证明是显然的。

注意到 \(f(1,n)=[1,n]\),所以一个区间增到 \([1,n]\) 后不会再变短。具有一定的单调性。

考虑倍增。看着上面的结论,推广出 \(f^k(l,r)=\bigcup\limits_{i=l}^{r-1}f^k(i,i+1)\)(归纳法证明)。所以现在要对于所有 \(2^k\),求出 \(f^{2^k}(l,r)\)。求并是可以 ST 表的,用 ST 表维护倍增的过程即可。\(f^{2^k}(l,r)=f^k(f^k(l,r))\)。

复杂度 \(O(nlog^2n+qlog n)\),空间 \(O(nlog^2n)\)

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,q,a[N],l,r,lg[N];
struct STbiao{
	int st[30][N];
	void init()
	{
		for(int i=1;i<=lg[n];i++)
			for(int j=1;j+(1<<i)-1<n;j++)
				st[i][j]=max(st[i-1][j],st[i-1][j+(1<<i-1)]);
	}
	int ask(int l,int r)
	{
		int k=lg[r-l+1];
		return max(st[k][l],st[k][r-(1<<k)+1]);
	}
}g[30],h[30];
int main()
{
	scanf("%d%d",&n,&q);
	for(int i=2;i<=n;i++)
		lg[i]=lg[i>>1]+1;
	for(int i=1;i<=n;i++)
		scanf("%d",a+i);
	for(int i=1;i<n;i++)
	{
		g[0].st[0][i]=-min(a[i],a[i+1]);
		h[0].st[0][i]=max(a[i],a[i+1]);
	}
	g[0].init(),h[0].init();
	for(int i=1;i<=20;i++)
	{
		for(int j=1;j<n;j++)
		{
			int l=-g[i-1].st[0][j],r=h[i-1].st[0][j];
			if(l==r)
				g[i].st[0][j]=-a[l],h[i].st[0][j]=a[l];
			else
				g[i].st[0][j]=g[i-1].ask(l,r-1),h[i].st[0][j]=h[i-1].ask(l,r-1);
		}
		g[i].init(),h[i].init();
	}
	while(q--)
	{
		scanf("%d%d",&l,&r);
		if(l==1&&r==n)
			puts("0");
		else if(l==r)
			puts("-1");
		else
		{
			int ans=0,p,q;
			for(int i=20;~i;--i)
				if(~g[i].ask(l,r-1)||h[i].ask(l,r-1)^n)
					p=-g[i].ask(l,r-1),q=h[i].ask(l,r-1),ans|=1<<i,l=p,r=q;
			p=-g[0].ask(l,r-1),q=h[0].ask(l,r-1);
			if(p==1&&q==n)
				printf("%d\n",ans+1);
			else
				puts("-1");
		}
	}
}

标签:le,询问,样例,Replace,leq,CF1707E,query,rightarrow
From: https://www.cnblogs.com/mekoszc/p/17969861

相关文章

  • 无涯教程-Java 正则 - String replaceAll(String replacement)函数
    java.util.regex.Matcher.replaceAll(Stringreplacement)方法使用给定的替换字符串替换与该模式匹配的每个子序列。StringreplaceAll-声明publicStringreplaceAll(Stringreplacement)replacement  - 替换字符串。StringreplaceAll-返回值通过用替换字符串替......
  • 无涯教程-Java 正则 - Matcher static String quoteReplacement(String s)函数
    java.time.Matcher.quoteReplacement(Strings)方法返回指定字符串的文字替换字符串。staticStringquoteReplacement-声明publicstaticStringquoteReplacement(Strings)s  - 要被字符串化的字符串。staticStringquoteReplacement-返回值文字字符串替换。......
  • 无涯教程-Java 正则 - Matcher appendReplacement(StringBuffer sb, String replacem
    java.time.Matcher.appendReplacement(StringBuffersb,Stringreplacement)方法实现了附加和替换操作。MatcherappendReplacement-声明publicMatcherappendReplacement(StringBuffersb,Stringreplacement)sb           - 目标字符串缓冲区......
  • C# Replace:一个熟悉而又陌生的替换
    C#Replace:一个熟悉而又陌生的替换 阅读目录前言一、String.Replace()的几个重载1、Replace(Char,Char)2、String.Replace(String,String) 3、Replace(String,String,StringComparison)4、Replace(String,String,Boolean,CultureInfo)二、Regex.Replace......
  • Configuration 'compile' is obsolete and has been replaced with 'implementati解决
    AndroidStudio更新到3.1.2编译之前的项目直接抛出下面的异常,这让我很是头疼,经过一翻查找发现是我们配置文件中的API已经过期,我对过期的API进行修改就Over了1、异常显示Configuration‘compile’isobsoleteandhasbeenreplacedwith‘implementation’and‘api’.It......
  • ReplaceGoogleCDN替换打不开的网页资源
    插件安装地址:ChromeFirefoxEdge背景在日常的网络浏览中,我们经常访问各种网站,其中包括大量使用了GoogleCDN(ContentDeliveryNetwork)的网页。虽然GoogleCDN在提供稳定、高效的内容分发方面表现出色,但在某些情况下,由于网络限制或其他原因,我们可能会遇到加载缓慢或无法访......
  • [AGC016D] XOR Replace 题解
    题目链接点击打开链接题目解法很有思维难度的一道题首先考虑简化操作(或者说用一种比较好的方法表示)假设我们选择交换的位置为\(x\),不难发现,操作等价于交换\(sumxor\)和\(x\)于是,有解的条件就好判了,即\(\{b_i\}\subseteq\{a_i\}\bigcap\{x\}\)操作可以理解为路径,即我......
  • 无涯教程-Java - String replaceFirst(String regex, String replacement)函数
    使用replacement替换第一个匹配的字符串。StringreplaceFirst-语法publicStringreplaceFirst(Stringregex,Stringreplacement)这是参数的详细信息-regex       -此字符串要匹配的正则表达式。replacement -将替换找到的表达式的字符串。String......
  • string.replace()与removeprefix() 和 removesuffix()的区别 字符串技巧
    string.replace(),removeprefix()和removesuffix()是Python中的字符串方法,它们都用于修改字符串,但是它们的功能和使用方式有所不同:string.replace(old,new[,count]):这个方法会将字符串中的old子串替换为new子串。如果提供了可选参数count,则只替换前count个old子串¹......
  • 使用router.replace解决路由跳转问题
    需求:A页面跳转到B页面,B页面带参跳转到C页面,C页面点击确定带参跳转回B页面。但是C页面点击返回按钮可返回到B页面,B页面点击返回按钮可返回到A页面。即A->B(带参)<->C(带参)在Vue3中,如果全部使用router.push带参跳转,则返回时路由跳转会变得很混乱。解决方法:B和C页面的相互跳转全部使......