Eugeny and Array(水题,注意题目描述即可)

本文介绍了一个算法问题,即如何响应针对由-1和1组成的数组的查询,判断是否可以通过重新排列数组元素使得指定区间内元素的和为0。文章提供了一段C++代码实现,通过计算数组中-1和1的数量来快速判断查询结果。

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

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 m lines 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.

Examples

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

代码

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>


using namespace std;

int main() {
	int n,m;
	int num[200005];
	cin>>n>>m;
	int sum1=0,sum2=0;
	for(int t=0; t<n; t++) {
		scanf("%d",&num[t]);
		if(num[t]==-1) {
			sum1++;
		} else {
			sum2++;
		}
	}
	int l,r;
	for(int t=0; t<m; t++) {
		scanf("%d%d",&l,&r);
		if((r-l)%2==0)
			printf("0\n");
		else if((r-l)%2!=0) {
			if((r-l+1)/2<=sum1&&(r-l+1)/2<=sum2)
				printf("1\n");
			else
				printf("0\n");
		}
	}



	return 0;
}

 

转载于:https://www.cnblogs.com/Staceyacm/p/10781943.html

Welcome to Ubuntu 24.04.2 LTS (GNU/Linux 6.8.0-60-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/pro System information as of Wed May 28 02:10:15 UTC 2025 System load: 0.0 Processes: 176 Usage of /: 1.9% of 386.42GB Users logged in: 1 Memory usage: 1% IPv4 address for eth0: 185.199.52.219 Swap usage: 0% IPv6 address for eth0: 2a02:4780:12:27e7::1 Expanded Security Maintenance for Applications is not enabled. 0 updates can be applied immediately. 14 additional security updates can be applied with ESM Apps. Learn more about enabling ESM Apps service at https://ubuntu.com/esm Last login: Wed May 28 02:08:30 2025 from 169.254.0.1 root@srv771551:~# ^C root@srv771551:~# which tabby # 应输出安装路径 tabby --version Command 'tabby' not found, did you mean: command 'ktabby' from deb kgames (2.3-1) Try: apt install <deb name> root@srv771551:~# sudo snap install tabby error: snap "tabby" not found root@srv771551:~# wget https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.deb --2025-05-28 02:14:56-- https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.deb Resolving github.com (github.com)... 20.207.73.82 Connecting to github.com (github.com)|20.207.73.82|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/49d65aef-dda0-40ec-8f6c-bb2f59b7b2ef?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021457Z&X-Amz-Expires=300&X-Amz-Signature=16929604a9e7523d5674ba06f7675fd4c04bf70e8b3d21ddf89c85c14d2a1c45&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.deb&response-content-type=application%2Foctet-stream [following] --2025-05-28 02:14:57-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/49d65aef-dda0-40ec-8f6c-bb2f59b7b2ef?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021457Z&X-Amz-Expires=300&X-Amz-Signature=16929604a9e7523d5674ba06f7675fd4c04bf70e8b3d21ddf89c85c14d2a1c45&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.deb&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.108.133, 185.199.110.133, 185.199.109.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.108.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 90647356 (86M) [application/octet-stream] Saving to: 'tabby-1.0.209-linux-x64.deb' tabby-1.0.209-linux-x64.deb 100%[=================================================================================================================================>] 86.45M 6.40MB/s in 13s 2025-05-28 02:15:11 (6.84 MB/s) - 'tabby-1.0.209-linux-x64.deb' saved [90647356/90647356] root@srv771551:~# sudo apt-get install gdebi Reading package lists... Done Building dependency tree... Done Reading state information... Done gdebi is already the newest version (0.9.5.7+nmu7). gdebi set to manually installed. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. root@srv771551:~# sudo gdebi tabby-*.deb /usr/bin/gdebi:113: SyntaxWarning: invalid escape sequence '\S' c = findall("[[(](\S+)/\S+[])]", msg)[0].lower() Reading package lists... Done Building dependency tree... Done Reading state information... Done Reading state information... Done This package is uninstallable Dependency is not satisfiable: gconf2 root@srv771551:~# sudo gdebi tabby-*.deb /usr/bin/gdebi:113: SyntaxWarning: invalid escape sequence '\S' c = findall("[[(](\S+)/\S+[])]", msg)[0].lower() Reading package lists... Done Building dependency tree... Done Reading state information... Done Reading state information... Done This package is uninstallable Dependency is not satisfiable: gconf2 root@srv771551:~# wget https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.AppImage --2025-05-28 02:16:02-- https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.AppImage Resolving github.com (github.com)... 20.207.73.82 Connecting to github.com (github.com)|20.207.73.82|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/6838174f-aae8-46a8-875f-4adc31805938?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021602Z&X-Amz-Expires=300&X-Amz-Signature=366b06d105ad23c750d2b24dfa63c80757d01eaaa1a2827052e5bdca604a9001&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.AppImage&response-content-type=application%2Foctet-stream [following] --2025-05-28 02:16:02-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/6838174f-aae8-46a8-875f-4adc31805938?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021602Z&X-Amz-Expires=300&X-Amz-Signature=366b06d105ad23c750d2b24dfa63c80757d01eaaa1a2827052e5bdca604a9001&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.AppImage&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.109.133, 185.199.108.133, 185.199.111.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.109.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 134111099 (128M) [application/octet-stream] Saving to: 'tabby-1.0.209-linux-x64.AppImage' tabby-1.0.209-linux-x64.AppImage 100%[=================================================================================================================================>] 127.90M 7.66MB/s in 19s 2025-05-28 02:16:22 (6.67 MB/s) - 'tabby-1.0.209-linux-x64.AppImage' saved [134111099/134111099] root@srv771551:~# wget https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.AppImage --2025-05-28 02:16:26-- https://github.com/Eugeny/tabby/releases/download/v1.0.209/tabby-1.0.209-linux-x64.AppImage Resolving github.com (github.com)... 20.207.73.82 Connecting to github.com (github.com)|20.207.73.82|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/6838174f-aae8-46a8-875f-4adc31805938?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021602Z&X-Amz-Expires=300&X-Amz-Signature=366b06d105ad23c750d2b24dfa63c80757d01eaaa1a2827052e5bdca604a9001&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.AppImage&response-content-type=application%2Foctet-stream [following] --2025-05-28 02:16:26-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/77213120/6838174f-aae8-46a8-875f-4adc31805938?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=releaseassetproduction%2F20250528%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20250528T021602Z&X-Amz-Expires=300&X-Amz-Signature=366b06d105ad23c750d2b24dfa63c80757d01eaaa1a2827052e5bdca604a9001&X-Amz-SignedHeaders=host&response-content-disposition=attachment%3B%20filename%3Dtabby-1.0.209-linux-x64.AppImage&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.110.133, 185.199.111.133, 185.199.109.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.110.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 134111099 (128M) [application/octet-stream] Saving to: 'tabby-1.0.209-linux-x64.AppImage.1' tabby-1.0.209-linux-x64.AppImage.1 100%[=================================================================================================================================>] 127.90M 84.0MB/s in 1.5s 2025-05-28 02:16:29 (84.0 MB/s) - 'tabby-1.0.209-linux-x64.AppImage.1' saved [134111099/134111099] root@srv771551:~# chmod +x *.AppImage root@srv771551:~# which tabby # 应输出安装路径 tabby --version Command 'tabby' not found, did you mean: command 'ktabby' from deb kgames (2.3-1) Try: apt install <deb name> root@srv771551:~# ^C root@srv771551:~# ^C
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值