CF:Eugeny and Array

来源:互联网 发布:java绘图教程 编辑:程序博客网 时间:2024/06/08 10:13
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 31 -11 11 22 2
output
010
input
5 5-1 1 1 1 -11 12 33 52 51 5
output
01010
解题报告:
大概的意思是先给出一个串a(1or-1),然后给出m行l,r两个数,然后重新对a进行排列,如果al+..........+ar=0;输出1否则输出0;
思路,如果r-l+1是奇数,那么数列和肯定不能为0,如果是偶数的话判断一下-1或者1的数量是否够用。
参考代码:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cmath>
using namespace std;
int main(){
   // freopen("in.txt","r",stdin);
   int n,m,i,j,l,r,t,n1,m1,count,m_min;
   while(cin>>n>>m)
   {
     l=0;
     r=0;
     n1=0;
     m1=0;
     for(i=0;i<n;i++)
     {
         cin>>t;
         if(t==-1)
        n1++;
        else
        m1++;
     }
     for(i=0;i<m;i++)
     {
         cin>>l>>r;
         count=r-l+1;


       if(count%2!=0)
       {
     cout<<"0"<<endl;
       continue;
       }


       m_min=min(n1,m1);
       if(count/2<=m_min)
       cout<<"1"<<endl;
       else
       cout<<"0"<<endl;
     }


   }
    return 0;
}
原创粉丝点击