codeforces-302A-Eugeny and Array

codeforces-302A-Eugeny and Array

                    time limit per test1 second     memory limit per test256 megabytes

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 li, ri (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.

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

题目链接:cf-302A

题目大意:给出一个序列a,问在a->l~r的范围内,是否总共为0。

题目思路:刚开始想用线段树,但发现样例2看不懂。结果。。 if the elements of array a can be rearranged (如果序列a中元素经过重新排列后…)直接暴力就可以了

以下是代码:

#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int a[200010];
int main(){
    int n,q;
    cin >> n >> q;
    int num1 = 0,num2 = 0;
    for (int i = 1; i <= n; i++)
    {
        scanf("%d",&a[i]);
        if (a[i] == 1) num1++;
        else num2++;
    }
    while(q--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        int ret = r - l + 1;
        if (ret % 2) printf("0\n");
        else
        {
            if (num1 >= ret / 2 && num2 >= ret / 2) printf("1\n");
            else printf("0\n");
        }   
    }
    return 0;
}

你可能感兴趣的:(302A)