Codeforces Round #182 (Div. 2) / 302A Eugeny and Array(模拟)

本文介绍 CodeForces 平台上题目 302A 的解题思路与实现方法,该题涉及数组操作及区间查询,通过判断是否能重新排列数组中的元素使指定区间的和为零。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Eugeny and Array
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Eugeny has array a = a1, a2, ..., an, consisting of n integers. Each integer ai equals to -1, or to 1. Also, he has m queries:

  • Query number i is given as a pair of integers liri (1 ≤ li ≤ ri ≤ n).
  • The response to the query will be integer 1, if the elements of array a can be rearranged so as the sum ali + ali + 1 + ... + ari = 0, otherwise the response to the query will be integer 0.

Help Eugeny, answer all his queries.

Input

The first line contains integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n integers a1, a2, ..., an (ai = -1, 1). Next mlines contain Eugene's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).

Output

Print m integers — the responses to Eugene's queries in the order they occur in the input.

Sample test(s)
input
2 3
1 -1
1 1
1 2
2 2
output
0
1
0
input
5 5
-1 1 1 1 -1
1 1
2 3
3 5
2 5
1 5
output
0
1
0
1
0

写第一遍没看清题意,样例2看不懂,后来发现我确实要补补英语了。。(不过这句话确实很容易漏掉)

原文有这么一句话:

" if the elements of array a can be rearranged ..."(如果序列a中元素经过重新排列后…)


完整代码:

/*171ms,0KB*/

#include<cstdio>

int main()
{
	int n, m, a;
	int c1 = 0, c2 = 0;
	int l, r;
	scanf("%d%d", &n, &m);
	while (n--)
	{
		scanf("%d", &a);
		if (a > 0)
			++c1;
		else
			++c2;
	}
	while (m--)
	{
		scanf("%d%d", &l, &r);
		a = r - l + 1;///第一次交的时候这里写反了,WA4,罪过,罪过。。。
		if (a & 1)
			puts("0");
		else
		{
			a >>= 1;
			puts(c1 < a || c2 < a ? "0" : "1");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值