[ABC265D] Iroha and Haiku (New ABC Edition)

Problem Statement

There is a sequence $A=(A_0,\ldots,A_{N-1})$ of length $N$.
Determine if there exists a tuple of integers $(x,y,z,w)$ that satisfies all of the following conditions:

  • $0 \leq x < y < z < w \leq N$
  • $A_x + A_{x+1} + \ldots + A_{y-1} = P$
  • $A_y + A_{y+1} + \ldots + A_{z-1} = Q$
  • $A_z + A_{z+1} + \ldots + A_{w-1} = R$

Constraints

  • $3 \leq N \leq 2\times 10^5$
  • $1 \leq A_i \leq 10^9$
  • $1 \leq P,Q,R \leq 10^{15}$
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

$N$ $P$ $Q$ $R$
$A_0$ $A_1$ $\ldots$ $A_{N-1}$

Output

If there exists a tuple that satisfies the conditions, print Yes; otherwise, print No.


Sample Input 1

10 5 7 5
1 3 2 2 2 3 1 4 3 2

Sample Output 1

Yes

$(x,y,z,w)=(1,3,6,8)$ satisfies the conditions.


Sample Input 2

9 100 101 100
31 41 59 26 53 58 97 93 23

Sample Output 2

No

Sample Input 3

7 1 1 1
1 1 1 1 1 1 1

Sample Output 3

Yes
预处理前缀和,然后枚举 $w$,如果存在 $s_z=s_w-r$,$s_y=s_w-r-q$,$s_x=s_w-r-p-q$,那么由于 $s$ 具有严格单调性,$x< y < z$,所以如果存在,答案就为 Yes.
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
int n,a[N],k;
long long p,q,r,s[N],lst(-1);
map<long long,int>t;
int can(long long x)
{
	return t.find(x)!=t.end();
}
int main()
{
	scanf("%d%lld%lld%lld",&n,&p,&q,&r);
	t[0]=1;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),s[i]=s[i-1]+a[i],t[s[i]]=1;
	for(int i=1;i<=n;i++)
	{
		if(can(s[i]-r)&&can(s[i]-q-r)&&can(s[i]-p-q-r))
		{
			printf("Yes");
			return 0;
		}
	}
	printf("No");
}
posted @ 2022-09-29 18:38  灰鲭鲨  阅读(44)  评论(0编辑  收藏  举报