A. Eugeny and Array

來源:互聯網
上載者:User
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 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.

Sample test(s)input
2 31 -11 11 22 2
output
010
input
5 5-1 1 1 1 -11 12 33 52 51 5
output
01010

解題說明:題目中給定一個只由-1,1組成的數列,在給定起始位置和結束位置後,問在可以對數列中數字進行任意排列的情況下能否讓從給定的起始位置到結束位置之間的數字之和為0. 做法是先統計出數列中1和-1的個數,然後判斷起始位置和結束位置中間包含多少個數字,如果為奇數那和肯定不為0,如果為偶數就判斷數列中1和-1的個數能否達到該數的一半,只有一半1和一半-1時才能確保加起來為0.

#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <string>#include <algorithm>using namespace std;int main(){int i,n,m;int l,r;int a,pos,neg;pos=neg=0;scanf("%d %d",&n,&m);for(i=0;i<n;i++){scanf("%d",&a);if(a>0){pos++;}else{neg++;} } if(pos>neg) { pos=neg; } for(i=0;i<m;i++) {scanf("%d %d",&l,&r);l=r-l+1; if (l%2==1){printf("0\n");}else if (pos>=l/2){printf("1\n");}else{printf("0\n");} } return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.